php file_put_contents(meta/services.json):无法打开流:权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23540083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file_put_contents(meta/services.json): failed to open stream: Permission denied
提问by vishnub1626
I am new to Laravel. I was trying to open http://localhost/test/public/
and I got
我是 Laravel 的新手。我试图打开http://localhost/test/public/
,我得到了
Error in exception handler.
异常处理程序出错。
I googled around and changed the permission of storage directory using chmod -R 777 app/storage
but to no avail.
我用谷歌搜索并更改了存储目录的权限,chmod -R 777 app/storage
但无济于事。
I changed debug=>true
in app.php
and visited the page and got Error in exception handler:
我改变debug=>true
在app.php
与访问该页面,并得到了在异常处理程序错误:
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
流或文件“/var/www/html/test/app/storage/logs/laravel.log”无法打开:无法打开流:权限在/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:
然后我使用命令更改了存储目录的权限,chmod -R 644 app/storage
并且“异常处理程序中的错误”错误消失了并加载了一个页面。但在那里我得到了这个:
file_put_contents(/var/www/html/laravel/app/storage/meta/services.json): failed to open stream: Permission denied
file_put_contents(/var/www/html/laravel/app/storage/meta/services.json):无法打开流:权限被拒绝
回答by ecairol
Suggestion from vsmoraesworked for me:
vsmoraes 的建议对我有用:
Laravel >= 5.4
Laravel >= 5.4
php artisan cache:clear
chmod -R 777 storage/
composer dump-autoload
Laravel < 5.4
Laravel < 5.4
php artisan cache:clear
chmod -R 777 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
当我问这个问题时,这是我在虚拟机中运行的本地主机上的问题。所以我认为设置 777 已经足够安全了,但是,当人们说您应该寻找不同的解决方案时,他们是对的。先试试775
回答by Adriano Rosa
For googlers who has been facing this problem with Laravel 5.
对于在 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.
这是由于不同用户尝试storage/logs
使用不同权限写入文件夹内的同一日志文件而导致的权限问题。
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:
发生的情况是您的 Laravel 配置可能设置为每天记录错误,因此您的网络服务器(apache/nginx)可能会在默认用户下创建此文件,具体取决于您的环境,它可能类似于_www
OSX 或www-data
*NIX 系统,然后是问题当您可能运行了一些 artisan 命令并遇到一些错误时出现,因此 artisan 将编写此文件但使用不同的用户,因为终端上的 PHP 由不同的用户执行,实际上是您的登录用户,您可以通过运行此命令来检查它:
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.
如果您的登录用户在您的网络服务器中创建了该日志文件,您将无法在其中写入错误,反之亦然,因为 Laravel655
默认使用权限写入日志文件,该权限仅允许所有者写入。
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.
要解决此临时问题,您必须手动授予664
该文件组的权限, 以便您的登录用户和网络服务器用户都可以写入该日志文件。
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/115632can help you to tackle with that.
为了永久避免此问题,您可能希望在storage/logs
目录中创建新文件时通过从目录继承权限来设置适当的权限此答案https://unix.stackexchange.com/a/115632可以帮助您解决那。
回答by RibeiroSt
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:
您不应授予 777 权限。这是一个安全风险。对于 Ubuntu 用户,在 Laravel 5 中,我建议递归更改目录存储的所有者:
Try the follow:
请尝试以下操作:
sudo chown -R www-data:www-data storage
In Ubuntu based systems, www-data is apache user.
在基于 Ubuntu 的系统中,www-data 是 apache 用户。
回答by Hans P
For everyone using Laravel 5, Homestead and Mac try this:
对于使用 Laravel 5、Homestead 和 Mac 的每个人,试试这个:
mkdir storage/framework/views
回答by mahrad
some times SELINUX caused this problem; you can disable selinux with this command.
有时 SELINUX 会导致此问题;您可以使用此命令禁用 selinux。
sudo setenforce 0
回答by user3470929
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
这将启用对应用程序、框架、日志的写入权限希望这会有所帮助
回答by Ibrahim W.
NEVER GIVE IT PERMISSION 777!
永远不要给它权限 777!
go to the directory of the laravel project on your terminal and write:
转到终端上 laravel 项目的目录并写入:
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
这样你就让你的用户成为所有者并授予权限:
1 执行,2 写入,4 读取
1+2+4 = 7 表示 (rwx)
2+4 = 6 表示 (rw)
最后,对于存储访问,ug +rwx 表示您给用户和组一个 7
回答by Brendan
For vagrant users, the solution is:
对于流浪用户,解决办法是:
(in vagrant) php artisan cache:clear
( 在流浪中) php artisan cache:clear
(outside of vagrant) chmod -R 777 app/storage
( 在 vagrant 之外) chmod -R 777 应用程序/存储
(in vagrant) composer dump-autoload
(在流浪者中)作曲家转储自动加载
Making sure you chmod in your local environment and not inside vagrant is important here!
确保您在本地环境中而不是在 vagrant 中 chmod 在这里很重要!
回答by Khay
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.
再试一次chmod -R 755 /var/www/html/test/app/storage
。Operation not permitted
在 chmod 中与 sudo 一起使用。如果仍然有错误,请使用检查所有者权限。
回答by Koushik Das
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
根据 Laravel 5.4,这是我写这篇文章时的最新版本,如果你有任何这样的问题,你需要更改权限。 不要听任何人告诉您为任何目录设置 777。它有一个安全问题。像这样更改存储文件夹的权限
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.
现在请确保您正在从应用程序目录执行这两个命令。您将来不会遇到有关许可的问题。775 不会危及您机器的任何安全性。