Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 could not open laravel.log

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?

like image 560
Daniel Bejan Avatar asked Mar 01 '16 05:03

Daniel Bejan


People also ask

How do I give permission to storage in laravel?

Change all file permissions to 644. Change all folder permissions to 755. For storage and bootstrap cache (special folders used by laravel for creating and executing files, not available from outside) set permission to 777, for anything inside.

Can't open append mode Linux?

If you encountered an error that says “Could Not Be Opened In Append Mode: Failed To Open Stream: Permission Denied” such us the the image below, you can solve this by simply changing the permission access of certain file. The chmod 666 will allow all users to read and write but cannot execute the file.

Can laravel log delete?

There is very easy way to clear log with php artisan log:clear command using Laravel More Command Package. The above will deleted all old log data from /storage/logs/ directory.


2 Answers

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.

like image 80
Risan Bagja Pradana Avatar answered Sep 22 '22 13:09

Risan Bagja Pradana


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!

like image 37
jacobfogg Avatar answered Sep 22 '22 13:09

jacobfogg