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

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

Related

Setting a laravel storage directory permission by ebextentions

I'm working on elastic beanstalk exextentions. A storage-permission-denied error occurs every deployments and I a have to type command to resolve that. Does the code below(.extensions/chmod.config), prevent the error occur ?
container_commands:
01addpermission:
command: "chmod -R 755 /var/app/current/storage"
01clearcache:
command: "php /var/app/current config:cache"
The code sadly will not work. The reason is that container commands run when your app is in the staging folder, not in current folder:
The specified commands run as the root user, and are processed in alphabetical order by name. Container commands are run from the staging directory, where your source code is extracted prior to being deployed to the application server.
You can try to use relative paths:
container_commands:
01addpermission:
command: "chmod -R 755 ./storage"
02clearcache:
command: "php . config:cache"
The alternative is to use postdeploy platform hook which runs commands after you app is deployed:
Files here run after the Elastic Beanstalk platform engine deploys the application and proxy server

Laravel on ElasticBeanstalk 'log file permission denied' error keeps coming up even the permission is set in the .ebextensions config file

I am deploying my Laravel application to the ElasticBeanstalk environment. But I am having issue with laravel.log file permissions.
I deployed my application using "eb deploy" command. After I deployed, I access my application. But it is throwing the following error.
The stream or file "/var/app/current/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
To solve the issue, I ssh into the server and run the following command.
sudo -u root chmod 777 -R /var/app/current/storage/logs
The problem is solved. Now my application is working. But I deploy my application again running "be deploy" command. After the deployment, the issue popped up again. To solve the issue in a consistent way. I tried to run the command in the .ebextensions config file as follow.
container_commands:
01-migrations:
command: "php artisan migrate --force"
02-log-storage-permissions:
command: "sudo -u root chmod -R 777 /var/app/current/storage/logs/"
I could deploy my application. But the issue still persists. It seems like the command is not working. What is wrong with my configuration and how can I fix it?
I believe that this is because container_commands run when your application is in the staging folder. Thus, after you run 02-log-storage-permissions, your /var/app/current will be replaced anyway with the staging folder. So your chmod wont persist.
To rectify the issue, you can try one of the two options:
Use
02-log-storage-permissions:
command: "sudo -u chmod -R 777 ./storage/logs/"
to change the logs in staging folder.
use postdeploy hook to run your script:
after the Elastic Beanstalk platform engine deploys the application and proxy server.

How to access Laravel Folder in Google Cloud

I received an error on my Laravel App
/app/storage/logs/laravel-2020-02-20.log" could not be opened: failed to open stream: Permission denied
So I google the error, and I discovered it a permission error.
chmod -R 775 storage
chmod -R 775 bootstrap/cache
Apparently, this fixes the error, but anytime I tried to run the command in the cloud console. It keeps saying
No directory found.
Can someone advice me on how to access storage directory.
From the tags, I can tell it is GAE flex on GCP. Flex gives you SSH access, but it is not advisable to execute read/write operations directly on the Instances as they are preemptive - can be restarted anytime by GCP. So any command you execute will have to be repeated whenever a new instance starts, which will of course defeats the purpose of scalability on GAE flex.
Try this instead:
On the composer.json file in your project directory, add the follow to the scripts:
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"#php artisan optimize",
"chmod -R 755 storage bootstrap\/cache" ]
For more info, check this community link: https://cloud.google.com/community/tutorials/run-laravel-on-appengine-flexible

Laravel Aws Elastic-Beanstalk deployment with EbCommands

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

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

Resources