我使用的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服务器的所有者以匹配操作系统用户(我不这样做
知道后果)
其他的东西
出于明显的安全原因,存储和供应商文件夹的权限应该保持在775。
但是,您的计算机和服务器Apache都需要能够在这些文件夹中进行写操作。例如:当你运行像php artisan这样的命令时,你的计算机需要写入存储中的日志文件。
你所需要做的就是把文件夹的所有权交给Apache:
sudo chown -R www-data:www-data /path/to/your/project/vendor
sudo chown -R www-data:www-data /path/to/your/project/storage
然后需要将用户(由用户名引用)添加到服务器Apache所属的组中。像这样:
sudo usermod -a -G www-data userName
注意:通常情况下,组名是www-data,但在您的示例中,将其替换为_www
我已经在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
出于明显的安全原因,存储和供应商文件夹的权限应该保持在775。
但是,您的计算机和服务器Apache都需要能够在这些文件夹中进行写操作。例如:当你运行像php artisan这样的命令时,你的计算机需要写入存储中的日志文件。
你所需要做的就是把文件夹的所有权交给Apache:
sudo chown -R www-data:www-data /path/to/your/project/vendor
sudo chown -R www-data:www-data /path/to/your/project/storage
然后需要将用户(由用户名引用)添加到服务器Apache所属的组中。像这样:
sudo usermod -a -G www-data userName
注意:通常情况下,组名是www-data,但在您的示例中,将其替换为_www