My Laravel App on AWS EB always occurs Permission denied Error after Lambda function restarts it - laravel

On AWS, I've set CloudWatch events to trigger Lambda functions which stop/restart Elastic Beanstalk environment where my Laravel + docker-compose app sits in. However, after restarting, my Laravel app occurs an Error;
The stream or file "/var/www/html/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied
I can fix this error rather easily, by using eb cli. All I have to do is connect to the app, go into a docker container and run the following command;
chgrp -R www-data storage bootstrap/cache
chmod -R ug+rwx storage bootstrap/cache
Hoevever...do I have to do this every morning!?
Is there a way to avoid Permission denied Error after restarting? Or is there a way to automate the above two commands? If anyone could shed some lights on it, I'd really appreciate it.

Related

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.

"laravel.log could not be opened" error on Elastic Beanstalk

I've deployed a Laravel application on AWS ElasticBeanstalk (PHP 7.3 running on 64bit Amazon Linux/2.9.7).
The application runs fine when it does, but randomly throws following error for few requests.
PHP Fatal error: Uncaught UnexpectedValueException: The stream or
file "/var/app/current/storage/logs/laravel.log" could not be opened:
failed to open stream: Permission denied in
/var/app/current/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:110\nStack
trace:\n#0
/var/app/current/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(42):
Monolog\Handler\StreamHandler->write(Array)\n#1
/var/app/current/vendor/monolog/monolog/src/Monolog/Logger.php(323):
Monolog\Handler\AbstractProcessingHandler->handle(Array)\n#2
/var/app/current/vendor/monolog/monolog/src/Monolog/Logger.php(541):
Monolog\Logger->addRecord(400, 'Unhandled Excep...', Array)\n#3
/var/app/current/vendor/laravel/framework/src/Illuminate/Log/Logger.php(174):
Monolog\Logger->error('Unhandled Excep...', Array)\n#4
/var/app/current/vendor/laravel/framework/src/Illuminate/Log/Logger.php(87):
Illuminate\Log\Logger->writeLog('error', 'Unhandled Excep...',
Array)\n#5
/var/app/current/vendor/laravel/framework/src/Illuminate/Log/LogManager.php(54
in
/var/app/current/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php
on line 110
I dont understand why it throws error for some requests only and not others. Nonetheless i tried applying the permissions to the storage and bootstrap folders as suggested by few posts through .ebextensions as below but did not get it working.
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_make_storage_writable.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
echo "Making /storage and /bootstrap writeable..."
chmod -R 777 /var/app/current/storage
chmod -R 777 /var/app/current/bootstrap
Can someone please help me here? I'm new to Laravel and AWS both and very confused as to what might be wrong here.
The problem is your setting the owner to root:root and the apache user is ec2-user.
When you delete the existing log, the correct user will create the new log and it'll work as needed. You could also chown the files to the ec2-user and that would work too.

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

xampp ubuntu laravel The stream or file "/storage/logs/laravel log" could not be opened: failed to open stream: Permission denied

installed laravel on my xampp installation on ubuntu. opened it in browser. got the error:
The stream or file /storage/logs/laravel log could not be opened: failed to open stream: Permission denied
how to fix? (i've already found an answer, will post right now)
the problem is that the user under which the apache runs doesn't have access to the folder. in xampp on ubuntu, the user of main apache process is root. but apache workers run under daemon user. the solution is to grant daemon access to the /storage dir. (if you'll only grant to /storage/logs than you'll fix this error, but the same error will occur with sibling dirs. thus the storage itself folder:
sudo chown -R daemon /path-to-your-project/storage
now the daemon user has access to this folder (and you don't but do you need it?
if you have better solutions, please feel free to share them here!
Add to composer.json
"scripts": {
"post-install-cmd": [
"chgrp -R www-data storage bootstrap/cache",
"chmod -R ug+rwx storage bootstrap/cache"
]
}
Then run composer install or update solve your issue

Laravel and AWS Elastic Beanstalk - File Permissions

I've created a Laravel app and deployed it to an EC2 instance using Elastic Beanstalk. Everything seems to work except that PHP can't write to the storage directory so I get this error:
Error in exception handler: The stream or file "/var/app/current/site/app/storage/logs/laravel.log"
could not be opened: failed to open stream:
No such file or directory in /var/app/current/site/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84
This looks like a permissions problem to me, so I've tried using the instructions at http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html to set the permissions.
I have one file 01permissions.config in the .ebextensions directory:
commands:
storage_permissions:
command: chmod -R 755 $EB_CONFIG_APP_ONDECK/site/app/storage
I get the following errors in server logs:
[ERROR] Command storage_permissions (chmod -R 755 $EB_CONFIG_APP_ONDECK/site/app/storage) failed
[DEBUG] Command storage_permissions output: chmod: cannot access ‘/site/app/storage’: No such file or directory
Any ideas what's going on here?
The issue was that I had ignored the logs directory so it wasn't on the server at all. It's not that the server couldn't write to it, it's that it didn't exist.
The default permissions for an instance created by Elastic Beanstalk are:
File 664
Dir 775
They are deployed and owned by the Apache user.
For anyone else with this problem... this would have worked:
container_commands:
01storage_permissions:
command: "chmod -fR 755 /var/app/ondeck/app/storage"

Resources