Laravel Aws Elastic-Beanstalk deployment with EbCommands - laravel

I am familiar with deployments using laravel apps to Aws Elastic Beanstalk, but today when trying to deploy another app (Laravel 5.7), I get this annoying exception error:
InvalidArgumentException - Please provide a valid cache path.
I have done my research, and I have tried the usual suggestions = creating folders under storage, and giving permissions, without success. Aws even tells me the following folders exist: storage/framework/[sessions, views, cache]
During deployment, everything runs perfectly, env file copied, migrations and seed is done, then deployment stops because of this error.
Here are the commands I run when deploying (ebCommands) :
commands:
01_update_composer:
command: export HOME=/root && export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update 1.0.0-alpha11
option_settings:
aws:elasticbeanstalk:container:php:phpini:
document_root: /public
memory_limit: 512M
users:
ec2-user:
groups:
- webapp
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/02_configure_environment_file.sh":
mode: "000777"
owner: root
group: root
content: |
#!/bin/bash
if [ ! -f /var/www/html/.env ] ; then
cp /var/www/html/.env.example /var/www/html/.env
fi
php /var/www/html/artisan migrate:refresh
php /var/www/html/artisan db:seed
mkdir -p /var/www/html/storage/framework/cache/data
mkdir -p /var/www/html/storage/framework/sessions
mkdir -p /var/www/html/storage/framework/views
mkdir -p /var/www/html/storage/app
mkdir -p /var/www/html/storage/logs
mkdir -p /var/www/html/bootstrap/cache
chmod -R 775 /var/www/html/bootstrap/cache
chmod -R 775 /var/www/html/storage
chmod -R 775 /var/www/html/storage/logs
php /var/www/html/artisan config:clear
php /var/www/html/artisan cache:clear
php /var/www/html/artisan config:cache
It should deploy and I should be able to see my website.
Instead, Aws logs show :
InvalidArgumentException : Please provide a valid cache path.
The website shows the same error message.
I have tried various things online, but I'm stuck !
It is not the first laravel app I'm deploying.
I do the deployment with CodePipeline and github automatically.
For me the issue is probably a permission issue...
Thank you !!

The error usually occurs when the storage folders are missing from the document root. You need to ensure the following folders are present:
./storage/framework/cache
./storage/framework/sessions
./storage/framework/views

Related

Unable to configure Laravel storage symlink

Tired of searching :-(
Configuration:
AWS (Instance)
Ubuntu 14.04.6 LTS
Laravel 5.8
Project root -> Permission and ownership
Inside project root
php artisan storage:link
project/public
Browser console says:
The complete project is working fine, even I am able to upload as follows.
$request->file('picture')->store('profile_pictures', 'public')
You might want to try this.
cd /var/www/html/yourproject
chmod -R 755 storage
chown -R www-data:www-data storage
Thanks.

Laravel - AWS Beanstalk - Storage symlink not working (403 error)

I am using elastic beanstalk to deploy my laravel application. Everything is working fine except for my images as I need to create a symbolic link with storage to access it publicly.
P.S. Works fine on my local
My .ebextensions file is as follows -
commands:
composer_update:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: COMPOSER_HOME
value: /root
container_commands:
01-install_dependencies:
command: "php /usr/bin/composer.phar install"
cwd: "/var/app/ondeck"
02_storage_sym_link:
command: "php artisan storage:link"
cwd: "/var/app/ondeck"
leader_only: true
Below is the log from my ec2 instance to confirm that the command worked just fine and the link was created successfully.
[2019-04-21T15:47:16.899Z] INFO [21538] - [Application update symlink alt2#208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Test for Command 02_storage_sym_link] : Starting activity...
[2019-04-21T15:47:16.903Z] INFO [21538] - [Application update symlink alt2#208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Test for Command 02_storage_sym_link] : Completed activity. Result:
Completed successfully.
[2019-04-21T15:47:16.903Z] INFO [21538] - [Application update symlink alt2#208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Command 02_storage_sym_link] : Starting activity...
[2019-04-21T15:47:17.014Z] INFO [21538] - [Application update symlink alt2#208/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_Synchro/Command 02_storage_sym_link] : Completed activity. Result:
The [public/storage] directory has been linked.
The error I am getting is as follows which makes the images unavailable for public access.
Forbidden
You don't have permission to access /storage/blog/images/8Yhb4OZJQIKwlMGaGE803niTxyjfzNSVTj2BiPaP.gif on this server.
Any help guiding me to the right path is appreciated. Cheers!
EDIT 1 :
container_commands:
01-install_dependencies:
command: "php /usr/bin/composer.phar install"
cwd: "/var/app/ondeck"
02_storage_sym_link:
command: "ln -s storage/app/public public/storage"
cwd: "/var/app/ondeck"
03_give_ec2_user_perm_1:
command: "sudo chown -R ec2-user /var/app/current"
03_give_ec2_user_perm_2:
command: "sudo chmod -R 777 /var/app/current"
Tried creating the symlink manually plus gave permission to the ec2-user. But still no luck :(
Putting down an alternate option which does the job for people who are/might have a similar issue -
Use S3 for file storage in your Laravel application.
To make it happen -
Create a public S3 bucket.
Create an IAM user which has full access to
S3 bucket. (With the access key, the application will have
permissions to read and write in the S3 bucket.)
Update the config file filesystems.php to use the S3 bucket. (This config handles the storage config of the application.)
Refer the Laravel Doc for more info.
Thanks to #PranavanSp for his suggestion.
Container commands are run as root, that is why when you tried to run it as the ec2-user you couldn't. The ec2-user is not in the root user group.
Then when you create symlinks, try to do it in the actual app directory(current):
container_commands:
01-install_dependencies:
command: "php /usr/bin/composer.phar install"
cwd: "/var/app/ondeck"
02_storage_sym_link:
command: "ln -s storage/app/public public/storage"
cwd: "/var/app/current"
Or try to link directly as so:
container_commands:
01-install_dependencies:
command: "php /usr/bin/composer.phar install"
cwd: "/var/app/ondeck"
02_storage_sym_link:
command: "ln -s /var/app/ondeck/storage/app/public /var/app/current/public/storage"
EBS Files can be annoying to get right at first but worth it in the end. If this still doesn't work, maybe the user(appache I assume) that runs the server does not have access to that folder. To just quickly verify this just do a:
sudo chmod -R 755 /var/app/ondeck/storage/app/public

artisan storage:link killed in laravel

the broken link and the output of files
I created the folder name: "storage" in public directory then I try to run:
artisan storage:link
but it gives the message "Killed".
i want to save image in the folder storage/app/public/temporary
Any one has idea what should do?
You should adjust the file permissions. cd into your projects root folder and execute the following commands.
Change group to Apache
sudo chgrp -R www-data storage public
Change permissions
sudo chmod -R 775 storage public
Restart Apache
service apache2 restart

AWS code deploy after install whoami issues

I'm migrating away from Envoyer to AWS code deploy to auto-deploy my Laravel app.
So I added this to my afterInstall script:
cd /project directory
composer install
if [ -f artisan ]
then
php artisan migrate --force
fi
if [ -f artisan ]
then
php artisan config:cache
php artisan queue:restart
fi
But the deployment fails with this error
MessageScript at specified location: scripts/after-install.sh run as
user user failed with exit code 1 Log TailLifecycleEvent -
AfterInstall Script - scripts/after-install.sh [stderr]No passwd entry
for user 'user'
which is weird. The AMI instance I've created already has the default user login as well as ubuntu as sudoers:
grep -Po '^sudo.+:\K.*$' /etc/group
ubuntu,forge
So why is it asking for a password?
I had to change the runAs section in the appspec file
in the script specified in "location".
AfterInstall:
- location: scripts/after-install.sh
runas: forge // used to be 'user'

"Please provide a valid cache path" error in laravel

I duplicated a working laravel app and renamed it to use for another app. I deleted the vendor folder and run the following commands again:
composer self-update
composer-update
npm install
bower install
I configured my routes and everything properly however now when I try to run my app in my browser I get the following errors:
InvalidArgumentException in Compiler.php line 36: Please provide a
valid cache path.
ErrorException in Filesystem.php line 111:
file_put_contents(F:\www\example\app\storage\framework/sessions/edf262ee7a2084a923bb967b938f54cb19f6b37d):
failed to open stream: No such file or directory
I have never had this issue before, I do not know what is causing it neither do I know how to fix it, I have googled online for a solution but have found none so far.
Try the following:
create these folders under storage/framework:
sessions
views
cache
Now it should work
Try this:
php artisan cache:clear
php artisan config:clear
php artisan view:clear
The cause of this error can be traced from Illuminate\View\Compilers\Compiler.php
public function __construct(Filesystem $files, $cachePath)
{
if (! $cachePath) {
throw new InvalidArgumentException('Please provide a valid cache path.');
}
$this->files = $files;
$this->cachePath = $cachePath;
}
The constructor is invoked by BladeCompiler in Illuminate\View\ViewServiceProvider
/**
* Register the Blade engine implementation.
*
* #param \Illuminate\View\Engines\EngineResolver $resolver
* #return void
*/
public function registerBladeEngine($resolver)
{
// The Compiler engine requires an instance of the CompilerInterface, which in
// this case will be the Blade compiler, so we'll first create the compiler
// instance to pass into the engine so it can compile the views properly.
$this->app->singleton('blade.compiler', function () {
return new BladeCompiler(
$this->app['files'], $this->app['config']['view.compiled']
);
});
$resolver->register('blade', function () {
return new CompilerEngine($this->app['blade.compiler']);
});
}
So, tracing back further, the following code:
$this->app['config']['view.compiled']
is generally located in your /config/view.php, if you use the standard laravel structure.
<?php
return [
/*
|--------------------------------------------------------------------------
| View Storage Paths
|--------------------------------------------------------------------------
|
| Most templating systems load templates from disk. Here you may specify
| an array of paths that should be checked for your views. Of course
| the usual Laravel view path has already been registered for you.
|
*/
'paths' => [
resource_path('views'),
],
/*
|--------------------------------------------------------------------------
| Compiled View Path
|--------------------------------------------------------------------------
|
| This option determines where all the compiled Blade templates will be
| stored for your application. Typically, this is within the storage
| directory. However, as usual, you are free to change this value.
|
*/
'compiled' => realpath(storage_path('framework/views')),
];
realpath(...) returns false, if the path does not exist. Thus, invoking
'Please provide a valid cache path.' error.
Therefore, to get rid of this error, what you can do is to ensure that
storage_path('framework/views')
or
/storage/framework/views
exists :)
Run these command to create required directories:
cd storage/
mkdir -p framework/{sessions,views,cache}
chmod -R 775 framework
That's it!
Ensure the below folders in storage directory:
logs
framework
framework/cache
framework/cache/data
framework/sessions
framework/testing
framework/views
Below is a command-line snippet that does for you
cd storage
mkdir logs
mkdir framework
mkdir framework/cache && framework/cache/data
mkdir framework/sessions
mkdir framework/testing
mkdir framework/views
chgrp -R www-data ../storage
chown -R www-data ../storage
You can edit your readme.md with instructions to install your laravel app in other environment like this:
## Create folders
```
#!terminal
cp .env.example .env && mkdir bootstrap/cache storage storage/framework && cd storage/framework && mkdir sessions views cache
```
## Folder permissions
```
#!terminal
sudo chown :www-data app storage bootstrap -R
sudo chmod 775 app storage bootstrap -R
```
## Install dependencies
```
#!terminal
composer install
```
You need to create folders inside "framework". Please Follow these steps:
cd storage/
mkdir -p framework/{sessions,views,cache}
You also need to set permissions to allow Laravel to write data in this directory.
chmod -R 775 framework
chown -R www-data:www-data framework
Try the following:
create these folders under storage/framework:
sessions
views
cache/data
if still it does not work then try
php artisan cache:clear
php artisan config:clear
php artisan view:clear
if get an error of not able to clear cache. Make sure to create a folder data in cache/data
Start by clearing the cache
php artisan cache:clear
php artisan config:clear
php artisan view:clear
If it doesn't work, ensure the all the following folders are available
logs
framework
framework/cache
framework/sessions
framework/views
If none of the suggestions works, verify that the config file config/view.php is present. If not, you can get a copy of this file for the Laravel you're using and copy it to the project config folder.
Check if the following folders exists, if not create these folders.
storage/framework/cache
storage/framework/sessions
storage/framework/testing
storage/framework/views
My 2 cents
Remove everything inside storage and then do this:
> cd storage/
> mkdir -p framework/{sessions,views,cache}
> chmod -R 755 framework
// This last line depends on your user group settings so
// it may not be applicable to you.
> chown -R www-data:www-data framework
Worked for me =)
I solved the problem when I created framework folder inside storage folder and its subfolders sessions, views and cache.
Go to your cmd or terminal then type your project root path and after that type the following:
cd storage
mkdir framework
cd framework
mkdir sessions
mkdir views
mkdir cache
Go to your project root path back again and run composer update
After that artisan works perfectly.
Step 1、Create these folders
mkdir -p storage/{app,framework,logs}
mkdir -p storage/framework/{sessions,views,cache}
chmod -R 777 storage
Step 2、Clear cache/config/view
php artisan cache:clear
php artisan config:clear
php artisan view:clear
step 1 : php artisan storage:link
step 2 : create these folders inside storage folder
Ensure the below folders in storage directory:
logs
framework
framework/cache
framework/sessions
framework/views
It worked for me
Please run in terminal,
sudo mkdir storage/framework
sudo mkdir storage/framework/sessions
sudo mkdir storage/framework/views
sudo mkdir storage/framework/cache
sudo mkdir storage/framework/cache/data
Now you have to change permission,
sudo chmod -R 777 storage
Issue on my side(while deploying on localhost): there was views folder missing..
so
if you have don't have the framework folder the you 'll need to add folders.
but if already framework folder exist then make sure all above folders i.e
1. cache
2. session
3. views
exists in your framework directory.
If this happens on server:
sudo mkdir logs framework framework/cache framework/sessions framework/views
sudo chgrp -R www-data storage
sudo chmod -R ug+rwx storage
I solved this problem by adding this line in my index.php:
$app['config']['view.compiled'] = "storage/framework/cache";
Your storage directory may be missing, or one of its sub-directories. The storage directory must have all the sub-directories that shipped with the Laravel installation.
May be the storage folder doesn't have the app and framework folder and necessary permission. Inside framework folder it contains cache, sessions, testing and views. use following command this will works.
Use command line to go to your project root:
cd {your_project_root_directory}
Now copy past this command as it is:
cd storage && mkdir app && cd app && mkdir public && cd ../ && mkdir framework && cd framework && mkdir cache && mkdir sessions && mkdir testing && mkdir views && cd ../../ && sudo chmod -R 777 storage/
I hope this will solve your use.
I also had a similar case after copying a project on a production server. The public folder was accessed by Apache via a symbolic link.
For Apache or the PHP service, the path to the project has not changed, thus they used cached file paths that lead to the old project repository.
Restarting Apache and PHP service resolved the issue.
step 1 : php artisan storage:link
step 2 : create these folders inside storage folder
Ensure the below folders in storage directory:
logs
framework
framework/cache
framework/sessions
framework/views
It worked for me
This worked for me too
In my case, the config cache files are all missing in boostrap/cache... so solution to me is php artisan config:cache to re-create cache files at boostrap/cache.
Error :'Please provide a valid cache path.' error.
If these type error comes then the solution given below :-
please create data folder inside storage/framework/cache

Resources