22

I have setup Laravel homestead on a local OSX machine, everything seemed to be going smoothly until I tried to open example.app:8000 and got this error:

Error in exception handler: The stream or file "/home/vagrant/Code/example/app/storage/logs/laravel.log" could not be opened: failed to open stream: Protocol error in /home/vagrant/Code/example/bootstrap/compiled.php:8671

I followed the Laravel docs as well as a Laracast about setting up homestead, so I am not sure what would be causing this. I can see that /home/vagrant/Code/example/app/storage/logs/laravel.log doesn't exist, but I assume that is something that should be created automatically?

Josh Mountain
  • 1,888
  • 10
  • 34
  • 51

6 Answers6

77

All files and folders under app/storage should be writable by you and group www-data (the webserver).

Error in exception handler: The stream or file "laravel/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in laravel/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:77

If you get this error (or a similar error) in the browser when accessing your site, then the group www-data can't write to app/storage. If you get this error when you execute certain php artisan commands, then you (the user) can't write to app/storage. Therefore both you and the www-data group must have write permission.


To ensure the files and folders have the correct permissions:

  1. Go to the root of your Laravel installation (where composer.json and artisan live).

  2. Change the owning user and group, where yourusername is your username:

    sudo chown -R yourusername:www-data app/storage
    

    This recursively (-R) sets the user:group owners to yourusername:www-data in all files and folders from app/storage onward.

  3. Add the write permission for both you and the www-data group:

    sudo chmod -R ug+w app/storage
    

    This recursively (-R) adds (+) the write flag (w) to the user (u) and group (g) that own the files and folders from app/storage onward.

  4. Additionally, some suggest you may need to flush the application cache.

    php artisan cache:clear
    
  5. Finally, you may want to regenerate Composer's autoload files.

    composer dump-autoload
    
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • This is the best option, but giving 777 to storage, it work on that particular date but when new error is generate on other day, the log is create by www-data, so again permission error is thrown. But @Virtlink, is this safe to give write permission to www-data on production server also? Our code will be safe with this?? – Tarunn Jun 18 '15 at 09:55
  • 5
    I keep getting an illegal group name error when trying to change the owning users / group – Tyler Jul 18 '16 at 22:06
  • For my case the issue was SELinux. "sudo setenforce 0" solved it. – Dr. Ehsan Ali Jul 11 '17 at 09:44
  • We use a cPanel environment. Our web requests aren't run by www-data, they are run by the active user or the `nobody` user. So `chgrp www-data` doesn't work. – Jay Bienvenu Sep 21 '17 at 21:50
  • Is this solutions specific to linux? On Mac I get `chown: www-data: illegal group name` – Matt Feb 03 '18 at 22:06
  • thanks for the great explanation but this isnt any command ```php artisan dump-autoload``` – StealthTrails Dec 24 '18 at 11:47
  • The `php artisan dump-autoload` command seems to have been removed. Everything else works but that command is no longer in the artisan manual. – richard4s Jan 24 '19 at 07:33
  • One thing to add, make sure your `storage/logs/laravel.log` file is created before applying these changes. In my case that file was created after and therefore was not under the `www-data` group and resulted in the same errors. – Matt K Nov 30 '21 at 20:38
5

As for me I had changed my storage permission with chmod -R 777 storage and it's work well.

However, setting 777 permissions is incredibly dangerous and should not be done on any server other than your local machine.

I had checked for this configuration at here.

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
  • 6
    Again and again people keep telling that `777` is the answer to all but no and no and again no. Linux system did not built this kind of permission configuration for nothing. 777 does not mean jackpot, It just means that you are giving any user all permission to the files. Go to this brilliant answer written by marco https://askubuntu.com/a/20110/700927 and you will know what I mean. – Bikal Basnet Mar 10 '19 at 08:37
4

You need to run one of the following:

  • sudo chmod -R 644 app/storage
  • sudo chmod -R 755 app/storage
alexrussell
  • 13,856
  • 5
  • 38
  • 49
Chirag Shah
  • 1,463
  • 2
  • 18
  • 40
  • `sudo chmod -R 644 app/storage` took care of the error but now I just see a blank white page. When I try to edit `app/routes.php` I get a permission denied error. Did I mess up permissions for this whole thing somehow? – Josh Mountain Jun 05 '14 at 08:46
  • For More information please you reffere : http://stackoverflow.com/questions/23411520/error-laravel-log-could-not-be-opened or http://stackoverflow.com/questions/17020513/laravel-4-failed-to-open-stream-permission-denied – Chirag Shah Jun 05 '14 at 08:55
  • 23
    @ChiragShah it's a bit irresponsible to provide commands like this (especially involving permissions) with no explanation, and then when his system breaks you simply point him to other answers. Lazy. – Luke Nov 16 '16 at 04:24
2

If changing the file permissions as described in the other answers didn't work and you're using CentOS 7 or RHEL then the issue may be a security program called selinux. I struggled with all kinds of permissions and groups before noticing user3670777 answer to Laravel 4: Failed to open stream: Permission denied

Hope this helps others.

Drellgor
  • 371
  • 4
  • 12
0

If All Above suggestions not working disable your selinux and try again

-6

im pretty new on laravel. May be I could not explain it in depth but that command fixed my problem.

chmod -R 777 storage/
Jay
  • 151
  • 1
  • 13
  • 1
    As pointed out in other comments, this is a dangerous solution. A bit like taking the front door off your house so the postman can deliver a parcel. – charliefortune Nov 21 '19 at 12:20