1
votes

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.

1
Is your app running as root? Did you try just deleting the log file and seeing if it will create a new one on its own?ahackney
@ahackney the app is running as ec2-user (used "ps aux | grep apache" to get the user). Deleted the file and it created a new one on it's own.Hrishit Patel
I've enabled the logs to be collected in tail logs and bundle logs, would that create any problem (suspecting as this is happening sporadically)?Hrishit Patel

1 Answers

1
votes

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.