file_put_contents(meta/services.json): failed to open stream: Permission denied
I am new to Laravel. I was trying to open http://localhost/test/public/
and I got
Error in exception handler.
I googled around and changed the permission of storage directory using chmod -R 777 app/storage
but to no avail.
I changed debug=>true
in app.php
and visited the page and got Error in exception handler:
The stream or file "/var/www/html/test/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/html/test/bootstrap/compiled.php:8423
Then I changed the permissions of storage directory using the command chmod -R 644 app/storage
and the 'Error in exception handler' error was gone and a page is loaded. But in there I am getting this:
file_put_contents(/var/www/html/laravel/app/storage/meta/services.json): failed to open stream: Permission denied
Suggestion from vsmoraes worked for me:
Laravel >= 5.4
php artisan cache:clear
chmod -R 775 storage/
composer dump-autoload
Laravel < 5.4
php artisan cache:clear
chmod -R 775 app/storage
composer dump-autoload
NOTE: DO NOT DO THIS ON ANY REMOTE SERVER (DEV OR PRODUCTION)
When I asked this question, this was a problem on my localhost, running in a Virtual Machine. So I thought setting up a 777 was safe enough, however, folks are right when they say you should look for a different solution. Try 775 first
For googlers who has been facing this problem with Laravel 5.
This is a permission issue caused by different users trying to write at the same log file within the storage/logs
folder with different permissions.
What happens is your laravel config probably is setup to log errors daily and therefore your webserver (apache/nginx) might create this file under a default user depending on your environment it can be something like _www
on OSX or www-data
on *NIX systems, then the issue comes when you might have run some artisan commands and got some errors, so the artisan will write this file but with a different user because PHP on terminal is executed by a different user actually your login user, you can check it out by running this command:
php -i | grep USER
If your login user created that log file your webserver you will not be able to write errors in it and vice-versa because laravel writes log files with 655
permissions by default which only allows the owner to write in it.
To fix this temporary you have to manually give permissions for the group 664
to this file so both your login user and webserver user can write to that log file.
To avoid this issue permanently you may want to setup a proper permissions when a new file is create within the storage/logs
dir by inheriting the permissions from the directory this answer https://unix.stackexchange.com/a/115632 can help you to tackle with that.
You should not give 777 permissions. It's a security risk. To Ubuntu users, in Laravel 5, I sugest to change owner for directory storage recursively:
Try the follow:
sudo chown -R www-data:www-data storage
In Ubuntu based systems, www-data is apache user.
For everyone using Laravel 5, Homestead and Mac try this:
mkdir storage/framework/views