我使用的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服务器的所有者以匹配操作系统用户(我不这样做
知道后果)
其他的东西
我有以下配置:
NGINX(运行用户:NGINX)
PHP-FPM
并正确应用权限@bgies建议在接受的答案。在我的案例中,问题是php-fpm配置的运行用户和组最初是apache。
如果你使用NGINX和php-fpm,你应该打开php-fpm的配置文件:
nano /etc/php-fpm.d/www.config
并将用户和组选项的值替换为一个NGINX配置工作;在我的例子中,两者都是nginx:
...
; Unix用户/进程组
; 备注:非必选用户。如果未设置组,则默认用户组
;将被使用。
; RPM: apache选择能够以httpd的方式访问某些目录
User = nginx
; RPM:在日志目录下保留一个允许写入的组。
Group = nginx
...
保存并重新启动nginx和php-fpm服务。
出于明显的安全原因,存储和供应商文件夹的权限应该保持在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
在为Laravel应用程序设置权限时,我们遇到了许多边缘情况。我们创建了一个单独的用户帐户(deploy),用于拥有Laravel应用程序文件夹并从CLI执行Laravel命令,并在www-data下运行web服务器。这导致的一个问题是,日志文件可能由www-data或deploy拥有,这取决于谁先写入日志文件,这显然阻止了其他用户将来写入它。
我发现唯一明智而安全的解决方案是使用Linux acl。这个解决方案的目标是:
To allow the user who owns/deploys the application read and write access to the Laravel application code (we use a user named deploy).
To allow the www-data user read access to Laravel application code, but not write access.
To prevent any other users from accessing the Laravel application code/data at all.
To allow both the www-data user and the application user (deploy) write access to the storage folder, regardless of which user owns the file (so both deploy and www-data can write to the same log file for example).
我们做到了以下几点:
All files within the application/ folder are created with the default umask of 0022, which results in folders having drwxr-xr-x permissions and files having -rw-r--r--.
sudo chown -R deploy:deploy application/ (or simply deploy your application as the deploy user, which is what we do).
chgrp www-data application/ to give the www-data group access to the application.
chmod 750 application/ to allow the deploy user read/write, the www-data user read-only, and to remove all permissions to any other users.
setfacl -Rdm u:www-data:rwx,u:deploy:rwx application/storage/ to set the default permissions on the storage/ folder and all subfolders. Any new folders/files created in the storage folder will inherit these permissions (rwx for both www-data and deploy).
setfacl -Rm u:www-data:rwX,u:deploy:rwX application/storage/ to set the above permissions on any existing files/folders.