2

I've been having some issues using an EC2 instance when I try and call CLI commands. I am using Laravel but I cant quite be sure if it is a Laravel issue or EC2.

When I ssh into my EC2 instance, I run the following laravel command to migrate my database:

php artisan migrate

And I get the following error:

PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message '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:107

I then run sudo php artisan migrate and get a totally different error:

[PDOException] SQLSTATE[HY000] [2002] No such file or directory

Then I run php artisan migrate again and it all works as expected!

Anyone have any ideas what is causing this behaviour or how I might fault find?

NOTE: EC2 is 64bit Amazon Linux 2016.03 v2.1.4 running PHP 5.5

GWed
  • 15,167
  • 5
  • 62
  • 99
  • These are almost certainly OS level errors. – Karen B Jul 21 '16 at 08:05
  • @KarenB thanks. Any idea what's causing it or how I can fault find? – GWed Jul 21 '16 at 08:06
  • Check your file permissions. It looks like whatever user the laravel process runs as can't write to its log path. – Karen B Jul 21 '16 at 08:07
  • but why would running as sudo (which also fails) and then running the command again result in no errors? It doesnt make sense – GWed Jul 21 '16 at 08:08
  • Does `/var/app/current/storage/logs/` exist? – Karen B Jul 21 '16 at 08:09
  • yes - running as sudo gets me past that error, but then i get an exception. Then running without sudo throws exception (see question). Im wondering if sudo maybe sets the correct file permissions on the log file which then gets us passed that error. But cant work out why i then get an exception thrown with sudo – GWed Jul 21 '16 at 08:11

2 Answers2

7
sudo chmod -R g+s storage
sudo chmod -R g+s bootstrap/cache

sudo chmod -R u+s storage
sudo chmod -R u+s bootstrap/cache

sudo find storage -type d -exec chmod 775 {} \;
sudo find storage -type f -exec chmod 664 {} \;


sudo find bootstrap/cache -type d -exec chmod 775 {} \;
sudo find bootstrap/cache -type f -exec chmod 664 {} \;

NOT RECOMMENDED: Change the permissions of bootstrap and storage to 777.

[ec2-user@ip-172-31-42-2 current]$ sudo chmod -R 777 storage
[ec2-user@ip-172-31-42-2 current]$ sudo  chmod -R 777 bootstrap
kunwar97
  • 775
  • 6
  • 14
1

The accepted answer here introduces some serious security risks - setting chmod to 777 allows anyone on your system to read, modify and execute anything inside those directories. If your storage folder contains uploads this could be particularly destructive. See this post for further details of why chmod 777 is bad.

This post provides a much safer solution, which is also included in the Laravel docs. The short version is that you should give ownership of your Laravel folder to Apache, then add your user to the Apache group (both are usually www-data:

sudo chown -R www-data:www-data /path/to/your/project/vendor
sudo chown -R www-data:www-data /path/to/your/project/storage

Then

sudo usermod -a -G www-data your-user-name
dave
  • 2,750
  • 1
  • 14
  • 22