我使用的Apache Web服务器的所有者设置为_www:_www。我从来不知道文件权限的最佳实践是什么,例如当我创建新的Laravel 5项目时。

Laravel 5要求/storage文件夹是可写的。我发现了很多不同的方法来让它工作,我通常以递归地使它为777 chmod结束。但我知道这不是个好主意。

官方医生说:

Laravel可能需要配置一些权限:其中的文件夹 存储和供应商要求web服务器进行写访问。

这是否意味着web服务器也需要访问存储和供应商文件夹本身,还是只需要访问它们的当前内容?

我认为更好的是更改所有者而不是权限。我将所有Laravel的文件权限递归地更改为_www:_www,这使得网站正常工作,就像我将chmod更改为777一样。问题是,现在每次我想保存任何文件时,我的文本编辑器都会要求我输入密码,如果我试图在Finder中更改任何内容,比如复制一个文件,也会发生同样的情况。

解决这些问题的正确方法是什么?

改变chmod 更改文件的所有者以匹配 web服务器,也许设置文本编辑器(和Finder?)跳过 询问密码,或者让他们使用sudo 更改web服务器的所有者以匹配操作系统用户(我不这样做 知道后果) 其他的东西


当前回答

这是在2017年编写的,大约版本5.1~5.2。在Laravel的后续版本中使用此功能之前,请先了解一些常识。

我决定编写自己的脚本,以减轻建立项目的痛苦。

在项目根目录中运行以下命令:

wget -qO- https://raw.githubusercontent.com/defaye/bootstrap-laravel/master/bootstrap.sh | sh

等待引导完成,然后就可以开始了。

使用前请检查脚本。

其他回答

如前所述

你所需要做的就是把文件夹的所有权交给Apache:

但是我为chown命令添加了-R: sudo chown -R www-data:www-data /path/to/your/project/vendor sudo chown -R www-data:www-data /path/to/your/project/storage

sudo chown -R $USER:www-data your_directory_name_under_storage_app

它将为服务器用户和服务器提供文件访问所需的权限。

大多数文件夹应该是正常的“755”,文件应该是“644”

Laravel要求一些文件夹对web服务器用户是可写的。该命令适用于unix操作系统。

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

我已经在EC2实例上安装了laravel,并花了3天的时间来修复权限错误,最终修复了它。 所以我想和其他人分享这段经历。

user problem When I logged in ec2 instance, my username is ec2-user and usergroup is ec2-user. And the website works under of httpd user: apache: apache so we should set the permission for apache. folder and file permission A. folder structure first, you should make sure that you have such folder structure like this under storage storage framework cache sessions views logs The folder structure can be different according to the laravel version you use. my laravel version is 5.2 and you could find the appropriate structure according to your version.

B. permission At first, I see the instructions to set 777 under storage to remove file_put_contents: failed to open stream error. So i setup permission 777 to storage chmod -R 777 storage But the error was not fixed. here, you should consider one: who writes files to storage/ sessions and views. That is not ec2-user, but apache. Yes, right. "apache" user writes file (session file, compiled view file) to the session and view folder. So you should give apache to write permission to these folder. By default: SELinux say the /var/www folder should be read-only by the apache deamon.

因此,我们可以将selinux设置为0: setenforce 0

这可以暂时解决问题,但这使mysql无法工作。 所以这不是一个很好的解。

你可以用以下方法设置一个读写上下文到存储文件夹(记得setenforce 1来测试它)

chcon -Rt httpd_sys_content_rw_t storage/

这样你的问题就解决了。

别忘了这一点 作曲家更新 PHP工匠缓存:清除 这些命令将在之后或之前使用。 我希望你节省时间。 祝你好运。Hacken

这是在2017年编写的,大约版本5.1~5.2。在Laravel的后续版本中使用此功能之前,请先了解一些常识。

我决定编写自己的脚本,以减轻建立项目的痛苦。

在项目根目录中运行以下命令:

wget -qO- https://raw.githubusercontent.com/defaye/bootstrap-laravel/master/bootstrap.sh | sh

等待引导完成,然后就可以开始了。

使用前请检查脚本。