事实上,我是laravel的新手,我正在尝试创建我的第一个项目。出于某种原因,我一直得到这个错误(我甚至还没有开始编码)

Error in exception handler: The stream or file "/var/www/laravel/app/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied in /var/www/laravel/bootstrap/compiled.php:8423

我读到这与权限有关,但chmod -R 775存储根本没有帮助。


当前回答

最大的人建议更改文件权限777或775,我认为这不是一个合适的方法来解决这个问题。您只需要更改存储和引导文件夹的所有权。

在下面的图片中,你可以看到我所有的文件/文件夹都在根用户下(除了存储和引导,因为我改变了所有权),但我是以管理员身份登录的(在改变所有权之前),这就是为什么它总是给予拒绝权限的原因。所以我需要把这两个文件夹的所有权改为管理员

我是怎么做的呢, 转到项目目录并运行以下命令。 sudo chown -R yourusername:www-data storage sudo chmod -R ug+w storage, sudo chown -R yourusername:www-data bootstrap, sudo chmod -R ug+w bootstrap

其他回答

我设法修复它,因为我只通过这个命令授予权限:

复制代码

sudo chmod -R 775 storage

解决方案是添加以下内容:

复制代码

sudo chmod -R ugo+rw storage

如果使用cmd

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache

如果你使用GUI

首先进入项目,右键单击存储,检查属性,然后进入Permissions选项卡

使用下面的代码更改权限

sudo chmod -R 777 storage

那么您的文件属性可能是

然后检查你的设置,并执行laravel命令,它将工作:)

在Laravel中,您应该在存储和缓存目录上设置ACL,以便web服务器用户可以对该目录进行读写。打开一个新终端,运行如下命令:

HTTPDUSER=$(ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1)

sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:$(whoami):rwX bootstrap/cache storage/

引用:

https://symfony.com/doc/3.4/setup/file_permissions.html#using-acl-on-a-system-that-supports-setfacl-linux-bsd

https://linux.die.net/man/1/setfacl

添加到composer.json

"scripts": {
    "post-install-cmd": [
          "chgrp -R www-data storage bootstrap/cache",
          "chmod -R ug+rwx storage bootstrap/cache"
     ]
}

安装composer后

执行以下命令,可以在starting of command时添加sudo,具体操作取决于系统:

chmod -R 775 storage/framework
chmod -R 775 storage/logs
chmod -R 775 bootstrap/cache