14

I know there are a lot of questions on this topic but my issues is really weird that's why I decided to post. I have this error in /var/logs/apache/error.log

  [Tue Mar 01 07:26:51.435312 2016] [:error] [pid 8837] [client 127.0.0.1:37843] PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/personale/librarie-cor/storage/logs/laravel.log" could not be opened: 
failed to open stream: Permission denied' in /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:87\nStack trace:
\n#0 /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\\Handler\\StreamHandler->write(Array)
\n#1 /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Logger.php(289): Monolog\\Handler\\AbstractProcessingHandler->handle(Array)
\n#2 /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Logger.php(565): Monolog\\Logger->addRecord(400, Object(Symfony\\Component\\Debug\\Exception\\FatalErrorException), Array)
\n#3 /var/www/personale/librarie-cor/vendor/laravel/framework/src/Illuminate/Log/Writer.php(202): Monolog\\Logger->error(Object(Symfony\\Component\\Debug\\Exception\\FatalErrorException), Array)
\n#4 /var/www/personale/librarie-cor in /var/www/personale/librarie-cor/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 87

The thing is I already did chmod -R 777 storage/, here is a screenshot:

chmoded

How can I get rid of the error?

Daniel Bejan
  • 1,468
  • 1
  • 15
  • 39

6 Answers6

46

TLDR;

Run the following commands on your terminal

# Clear Laravel cache and the compiled classes
php artisan cache:clear
php artisan clear-compiled

# Change the storage and cache directories permission
sudo chmod -R 777 storage
sudo chmod -R 777 bootstrap/cache

# Regenerate the composer autoload file
composer dump-autoload

More thorough explanation

This usually happens because of the web server needs a write access to the storage and bootstrap/cache directories.

1. Check which user is being used by web server process

First, make sure that your webserver process is run by an account with limited privileges. Nginx and Apache usually will automatically create and use the less privileged www-data user and group. You can always use the ps command to check which user is being used by the running service:

ps aux | grep nginx

2. Set the owner of your project directory

Next, make sure that your Laravel project directory is owned by the same user and group who run the web server process. Suppose your web server is run by www-data and your project directory is located at /var/www/laravel, you can set the ownership like so:

sudo chown -R www-data:www-data /var/www/laravel

3. Give a write access to storage and cache directories

This is the IMPORTANT step, make sure you give the write permission both to storage and bootstrap/cache directories.

sudo chmod -R 775 /var/www/laravel/storage
sudo chmod -R 775 /var/www/laravel/bootstrap/cache

Still Not Working?

If the above steps are still not working, you can try to run the following commands in the shell:

# 1. Clear Laravel cache
php artisan cache:clear

# 2. Delete the compiled class
php artisan clear-compiled

# 3. Regenerate the composer autoload file
composer dump-autoload

Yet, it still not working?

For the last resource, try to set the permission to 777 which means any users will have the ability to read and write to the given directories.

sudo chmod -R 777 /var/www/laravel/storage
sudo chmod -R 777 /var/www/laravel/bootstrap/cache

Hope this help.

Risan Bagja Pradana
  • 4,494
  • 1
  • 20
  • 22
  • I have updated my answer. I just realized that you are using the Laravel 5.2 version. – Risan Bagja Pradana Mar 01 '16 at 05:57
  • I encountered this issue because I ran composer update in production. sudo chmod -R 777 storage worked for me but I dont know why. Can anyone explain? – Dazzle Sep 01 '17 at 11:46
  • @Dazzle That was because the web server/Laravel needs a write access to those two directories (for writing logs & caches). Doesn't always have to be `777`, it depends on the owner/group of those two directories. You'll find a short explanation here: [Laravel Configuration](https://laravel.com/docs/5.2#configuration) – Risan Bagja Pradana Sep 01 '17 at 12:42
  • 1
    Thanks, @NineCattoRules ! Just added a little info about security concern. – Risan Bagja Pradana Oct 02 '17 at 17:42
11

I had the same issue with Centos7 ... I tried EVERYTHING. Finally, I found a post on stackoverflow suggesting it could be selinux. I disabled selinux and it worked!

jacobfogg
  • 309
  • 2
  • 9
  • Yes, but don't know why. – Lynn May 04 '17 at 11:23
  • found link with more described the SElinux issue: http://stackoverflow.com/questions/30306315/laravel-5-laravel-log-could-not-be-opened-permission-denied – Alex May 19 '17 at 12:42
6

If you are in Centos, then disable SELinux enforcement

run below command to do this.

setenforce 0

In my case, this solution worked very well. Hope this will help.

5

U should try somethings like this-

php artisan cache:clear
php artisan clear-compiled

sudo chmod -R 777 storage/ -R
composer dump-autoload

U can go to this question for more details.

Community
  • 1
  • 1
Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161
2

I finally found a solution.. Seems like the problem had to do with PSR-4 and auto importing classes

So here's the solution for anyone else having this problem:

php artisan clear-compiled 
composer dump-autoload
php artisan optimize
php artisan cache:clear
Daniel Bejan
  • 1,468
  • 1
  • 15
  • 39
2

use this

chcon -R -t httpd_sys_rw_content_t storage