Laravel 5.2 could not open laravel.log

20,635

Solution 1

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

Solution 2

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.

Solution 3

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!

Solution 4

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.

Solution 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.

Share:
20,635
Daniel Bejan
Author by

Daniel Bejan

Updated on April 05, 2020

Comments

  • Daniel Bejan
    Daniel Bejan about 4 years

    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?

    • sandeepsure
      sandeepsure about 8 years
      Update composer once.
    • Daniel Bejan
      Daniel Bejan about 8 years
      Already tried.. No luck..
    • naneri
      naneri about 8 years
      man delete the laravel.log file. Laravel will then generate a new one
    • Daniel Bejan
      Daniel Bejan about 8 years
      Tried.. That didn't work either... I posted a solution
    • NineCattoRules
      NineCattoRules over 6 years
      why database folder is set 777?
  • Risan Bagja Pradana
    Risan Bagja Pradana about 8 years
    I have updated my answer. I just realized that you are using the Laravel 5.2 version.
  • Daniel Bejan
    Daniel Bejan about 8 years
    Already tried.. Not working.. Check my answer for the solution I found
  • Lynn
    Lynn about 7 years
    Yes, but don't know why.
  • Alex
    Alex almost 7 years
    found link with more described the SElinux issue: stackoverflow.com/questions/30306315/…
  • Dazzle
    Dazzle over 6 years
    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?
  • Risan Bagja Pradana
    Risan Bagja Pradana over 6 years
    @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
  • NineCattoRules
    NineCattoRules over 6 years
    and what about security risks?
  • NineCattoRules
    NineCattoRules over 6 years
  • Risan Bagja Pradana
    Risan Bagja Pradana over 6 years
    Thanks, @NineCattoRules ! Just added a little info about security concern.
  • Muhammad Usama
    Muhammad Usama over 6 years
    Best solution for above problem ` chcon -R -t httpd_sys_rw_content_t storage `