Chuck Le Butt
Chuck Le Butt

Reputation: 48758

Laravel 5.5: laravel.log could not be opened: Permission denied

I'm trying to run a simple Laravel command line on my Elastic Beanstalk instance: php artisan queue:work

But I keep getting the following error:

In StreamHandler.php line 107:

The stream or file "/var/app/current/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied

I've tried every solution I can find on SO (except the chmod -R 777 advice that seems to trail this question everywhere).

I've tried deleting the existing laravel.log and then using touch to create a new one, and then making sure webapp is the owner.

I've also tried:

sudo chmod -R 755 /var/app/current/storage/
sudo chown -R webapp /var/app/current/storage/

When I list the logs directory, everything looks as I think it should:

-rwxr-xr-x 1 webapp webapp     0 Apr  4 14:38 laravel.log

The storage directory also looks fine:

drwxr-xr-x  6 webapp webapp   4096 Apr  3 19:33 storage

But I'm still getting the above error! Can anyone explain why (not just give a solution).

Thank you

Upvotes: 1

Views: 5253

Answers (3)

Dylan Pierce
Dylan Pierce

Reputation: 4668

When you log into an EBS instance via ssh, you're logged in as ec2-user.

I don't believe the ec2-user is part of the webapp group which is actually executing PHP & apache/nginx.

Try adding your ec2-user to the webapp group by creating an ebextension in the root of your Laravel project under .ebextensions/ec2user.config

users:
  ec2-user:
    groups:
      - webapp

Upvotes: 2

Yasii
Yasii

Reputation: 1842

Prove this is the problem by turning off selinux with the command

sudo setenforce 0

This should allow writing, but you've turned off added security server-wide. That's bad. Turn SELinux back

sudo setenforce 1

Then finally use SELinux to allow writing of the file by using this command

sudo chcon -R -t httpd_sys_rw_content_t storage

And you're off!

Upvotes: 0

Chuck Le Butt
Chuck Le Butt

Reputation: 48758

So the simple answer is that I was running the command as ec2-user. As a solution, I could either:

  1. Change the ownership of laravel.log to ec2-user
  2. Run the command as the owner (eg. sudo -u webapp php artisan queue:work)
  3. Switch to root with sudo su to see how it would be run during deployment (ie. as root)

Nothing was especially wrong.

Upvotes: 4

Related Questions