file_put_contents(meta/services.json): failed to open stream: Permission denied

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

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

回答1:

Suggestion from vsmoraes worked for me:

Laravel >= 5.4

php artisan cache:clear  chmod -R 777 storage/ composer dump-autoload 

Laravel < 5.4

php artisan cache:clear  chmod -R 777 app/storage  composer dump-autoload 


回答2:

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.



回答3:

For everyone using Laravel 5, Homestead and Mac try this:

mkdir storage/framework/views 


回答4:

some times SELINUX caused this problem; you can disable selinux with this command.

sudo setenforce 0 


回答5:

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.



回答6:

Problem solved

php artisan cache:clear sudo chmod -R 777 vendor storage 

this enables the write permission to app , framework, logs Hope this will Help



回答7:

For vagrant users, the solution is:

(in vagrant) php artisan cache:clear

(outside of vagrant) chmod -R 777 app/storage

(in vagrant) composer dump-autoload

Making sure you chmod in your local environment and not inside vagrant is important here!



回答8:

Try again with chmod -R 755 /var/www/html/test/app/storage. Use with sudo for Operation not permitted in chmod. Use Check owner permission if still having the error.



回答9:

If you have Laravel 5 and looking permanent solution , applicable both php artisan command line usage and Apache server use this:

sudo chmod -R 777 vendor storage
echo "umask 000" | sudo tee -a /etc/resolv.conf
sudo service apache2 restart

See detailed explanation here.



回答10:

As per Laravel 5.4 which is the latest as I am writing this, if you have any problem like this, you ned to change the permission. DO NOT LISTEN TO ANYONE WHO TELLS YOU TO SET 777 FOR ANY DIRECTORY. It has a security issue. Change the permission of storage folder like this

sudo chmod -R 775 storage 

Change bootstrap folder permission like this

sudo chmod -R 775 bootstrap/cache 

Now please make sure that you're executing both commands from your application directory. You won't face problems in future regarding permission. 775 doesn't compromise any security of your machine.



回答11:

Suggest the correct permission, if for Apache,

sudo chown -R apache:apache apppath/app/storage 


回答12:

FOR ANYONE RUNNING AN OS WITH SELINUX: The correct way of allowing httpd to write to the laravel storage folder is:

sudo semanage fcontext -a -t httpd_sys_rw_content '/path/to/www/storage(/.*)?' 

Then to apply the changes immediately:

sudo restorecon -F -r '/path/to/www/storage' 

SELinux can be a pain to deal with, but if it's present then I'd STRONGLY ADVISE you learn it rather than bypassing it entirely.



回答13:

I had the same issue and the below steps helped me fix the issue.

  1. Find out the apache user - created a test.php file in the public folder with the code

<?php echo exec('whoami'); ?>

And run the file from the web browser. It would give the apache user. In my case, it is ec2-user as I was using the aws with cronjob installed in /etc/cron.d/. It could be different user for others.

  1. Run the below command on the command line.

sudo chown -R ec2-user:<usergroup> /app-path/public

You need to identify and use the right "user" and "usergroup" here.



回答14:

Xampp for use:

cd /Applications/XAMPP/htdocs   chmod -R 775 test/app/storage 


回答15:

rm storage/logs/laravel.log   

solved this for me



回答16:

NEVER GIVE IT PERMISSION 777!

go to the directory of the laravel project on your terminal and write:

sudo chown -R your-user:www-data /path/to/your/laravel/project/ sudo find /same/path/ -type f -exec chmod 664 {} \; sudo find /same/path/ -type d -exec chmod 775 {} \; sudo chgrp -R www-data storage bootstrap/cache sudo chmod -R ug+rwx storage bootstrap/cache 

This way you're making your user the owner and giving privileges:
1 Execute, 2 Write, 4 Read
1+2+4 = 7 means (rwx)
2+4 = 6 means (rw)
finally, for the storage access, ug+rwx means you're giving the user and group a 7



回答17:

If using laradock, try chown -R laradock:www-data ./storage in your workspace container



回答18:

Any time I change app.php I get a permission denied writing bootstrap/cache/services.json so I did this to fix it:

chmod -R 777 bootstrap/cache/ 


回答19:

In my case solution was to change permission to app/storage/framework/views and app/storage/logs directories.



回答20:

If anyone else runs into a similar issue with fopen file permissions error, but is wise enough not to blindly chmod 777 here is my suggestion.

Check the command you are using for permissions that apache needs:

fopen('filepath/filename.pdf', 'r'); 

The 'r' means open for read only, and if you aren't editing the file, this is what you should have it set as. This means apache/www-data needs at least read permission on that file, which if the file is created through laravel it will have read permission already.

If for any reason you have to write to the file:

fopen('filepath/filename.pdf', 'r+'); 

Then make sure apache also has permissions to write to the file.

http://php.net/manual/en/function.fopen.php



回答21:

Just start your server using artisian

php artisian serve

Then access your project from the specified URL:



回答22:

I have the same issue when running vagrant on mac. solved the problem by changing the user of Apache server in https.conf file:

# check user for php [vagrant] ubuntu ~ $ php -i | grep USER USER => ubuntu $_SERVER['USER'] => ubuntu [vagrant] ubuntu ~ $  

Run apache under php user instead of user daemon to resolve file access issue with php

# change default apache user from daemon to php user sudo sed -i 's/User daemon/User ubuntu/g' /opt/lampp/etc/httpd.conf sudo sed -i 's/Group daemon/Group ubuntu/g' /opt/lampp/etc/httpd.conf 

now, php created cache file can be read and edit by apache without showing any access permission error.



回答23:

I had a similar problem. (Permission denied while the permissions where configured correct) with Laravel 5.2 and 5.5

The problem was that SELinux was enabled which prevent Apache to write files even with 777 mode. See Solve 500 response Laravel (Uncaught UnexpectedValueException: Laravel.log) for the question and answer.

Maybe this solves the problem for you also.



回答24:

After a lot of trial and error with directory permissions I ended up with an epiphany...there was no space left on the disk's partition. Just wanted to share to make sure nobody else is stupid enough to keep looking for the solution in the wrong direction.

In Linux you can use df -h to check your disk size and free space.



回答25:

Setting permission to 777 is definitely terrible idea!

... but

If you are getting permission error connected with "storage" folder that's what worked for me:

1) Set "storage" and its subfolders permission to 777 with

sudo chmod -R 777 storage/ 

2) In browser go to laravel home page laravel/public/ (laravel will create necessary initial storage files)

3) Return safe 775 permission to storage and its subfolders

sudo chmod -R 775 storage/ 


回答26:

This issue actually caused by different users who wants to write/read file but denied cause different ownership. maybe you as 'root' installed laravel before then you login into your site as 'laravel' user where 'laravel' the default ownership, so this is the actually real issue here. So when user 'laravel' want to read/write all file in disk as default, to be denied, cause that file has ownership by 'root'.

To solving this problem you can follow like this:

sudo chown -hR your-user-name /root /nameforlder

or in my case

sudo chown -hR igmcoid /root /sublaravel

Footnote:

  1. root as name first ownership who installed before
  2. your-user-name as the default ownership who actually write/read in site.
  3. namefolder as name folder that want you change the ownership.


回答27:

For LARAVEL 5, try to create cache, sessions and views folders in storage/framework with 777 permission.



回答28:

I have tried to give the 777 access to storage folder and it have work for me

1) go to your laravel root directory , (/var/www/html for me) and run the following command

chmod 777 -R storage 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!