事实上,我是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存储根本没有帮助。


当前回答

添加到composer.json

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

安装composer后

其他回答

对于所有在Laravel环境下的Centos 7用户,不需要禁用Selinux,只需要运行这些命令:

yum install policycoreutils-python -y # might not be necessary, try the below first

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/storage(/.*)?" # add a new httpd read write content to sellinux for the specific folder, -m for modify
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/laravel/bootstrap/cache(/.*)?" # same as the above for b/cache

restorecon -Rv /var/www/html/ # this command is very important to, it's like a restart to apply the new rules

最后,确保您的主机、ip和虚拟主机都正确用于远程访问。

Selinux的目的是限制对根用户的访问,所以只有必要的东西可以访问,至少从一般的概述来看,它是额外的安全,禁用它不是一个好的实践,有许多学习Selinux的链接,但在这种情况下甚至不需要它。

添加到composer.json

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

安装composer后

永远不要在你的服务器上使用777,但在你自己的机器上,有时我们需要做更多的775,因为

chmod -R 775 storage

意味着

7 - Owner can write
7 - Group can write
5 - Others cannot write!

如果你的服务器不是作为Vagrant运行,它将无法写入,所以你有两个选择:

chmod -R 777 storage

或者将组改为您的webserver用户,假设它是www-data:

chown -R vagrant:www-data storage

我不太想把我的文件夹权限改为777。下面是我解决这个问题的方法。

首先,我改变了在我的本地机器上运行web服务器的用户(我运行nginx,但原则适用于任何地方):

$> sudo vim /etc/nginx/nginx.conf
user <my_user> #inside nginx.conf
service nginx reload

之后,我在public/文件夹下创建了另一个index.php文件,以找出谁在运行我的php-fpm版本,以及我将在哪里更改它:

<?php
phpinfo();
?>

重新加载页面后,我发现www-data是用户(在环境部分下)。我还发现我运行的是php 7.1。我开始更改用户:

$> sudo vim /etc/php/7.0/fpm/pool.d/www.conf 
#Look for www-data or the following variables: user, group, listen.user, listen.group.

最后,我给文件夹的权限如下:

sudo chmod -R 775 ./storage/

现在,我确保我是文件夹的所有者通过使用简单的:

ls -al

如果您将服务器和php-fpm用户设置为自己,并且文件夹由root拥有,那么您将继续遇到这个问题。如果您将sudo laravel new <project>作为根目录,就会发生这种情况。在这种情况下,请确保在项目上使用递归chown命令更改user:group设置。在大多数默认情况下,www-data是服务器和php的主要设置,在这种情况下,要确保文件夹不在www-data的范围内。

我的项目安装在我的主目录下。Ubuntu 16.04和Laravel 5.5。

在Linux中

sudo chown -R www-data:root /var/www/name-project-Laravel
sudo chmod 755 /var/www/name-project-Laravel/storage