我使用的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服务器的所有者以匹配操作系统用户(我不这样做 知道后果) 其他的东西


当前回答

我已经使用这个代码片段3年多了。


laravel new demo 

cd demo 

sudo find ./ -type f -exec chmod 664 {} \;    
sudo find ./  -type d -exec chmod 775 {} \;

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

php artisan storage:link

@realtebo的推荐看起来不错。我会试试的。

sudo find . -type d -exec chmod g+s {} \;  <----- NOTE THIS

其他回答

如前所述

你所需要做的就是把文件夹的所有权交给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

对于观看这个讨论的人来说,这是显而易见的....如果你给你的任何文件夹777权限,你就允许任何人读、写和执行....目录下的任何文件这意味着你给了任何人(任何黑客或恶意的人在整个世界)上传任何文件,病毒或任何其他文件的权限,然后执行该文件…

如果您正在将文件夹权限设置为777,则您已经打开了您的 任何能找到该目录的人的服务器。够清楚? ?:)

设置所有权和权限基本上有两种方法。要么你给自己所有权,要么你让web服务器成为所有文件的所有者。

web服务器作为所有者(大多数人这样做,以及Laravel文档的方式):

假设www-data(它可能是其他东西)是您的web服务器用户。

sudo chown -R www-data:www-data /path/to/your/laravel/root/directory

如果你这样做,webserver拥有所有的文件,也是组,你将会有一些问题,通过FTP上传文件或使用文件,因为你的FTP客户端将以你的身份登录,而不是你的webserver,所以把你的用户添加到webserver用户组:

sudo usermod -a -G www-data ubuntu

当然,这是假设你的服务器运行为www-data (Homestead默认),你的用户是ubuntu(如果你使用Homestead,它是流浪者)。

然后将所有目录设置为755,文件设置为644… SET文件权限

sudo find /path/to/your/laravel/root/directory -type f -exec chmod 644 {} \;    

设置目录权限

sudo find /path/to/your/laravel/root/directory -type d -exec chmod 755 {} \;

用户作为所有者

我更喜欢拥有所有的目录和文件(这让工作变得更容易),所以,去你的laravel根目录:

cd /var/www/html/laravel >> assuming this is your current root directory
sudo chown -R $USER:www-data .

然后我给自己和webserver权限:

sudo find . -type f -exec chmod 664 {} \;   
sudo find . -type d -exec chmod 775 {} \;

然后给web服务器读写存储和缓存的权限

无论你用哪种方式设置它,你都需要给web服务器读写权限,用于存储,缓存和任何其他web服务器需要上传或写入的目录(取决于你的情况),所以运行上面bashy的命令:

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

现在,你是安全的,你的网站工作,你可以相当容易地处理文件

在为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.

我已经在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

这招对我很管用:

cd [..LARAVEL PROJECT ROOT]
sudo find . -type f -exec chmod 644 {} \;
sudo find . -type d -exec chmod 755 {} \;
sudo chmod -R 777 ./storage
sudo chmod -R 777 ./bootstrap/cache/

只有当你使用npm (VUE,编译SASS等)时,才添加这个:

sudo chmod -R 777 ./node_modules/

它的作用:

将所有文件权限更改为644 将所有文件夹权限更改为755 对于存储和引导缓存(laravel用于创建和执行文件的特殊文件夹,外部不可用),将内部所有内容的权限设置为777 对于nodeJS可执行文件,与上面相同

注意:也许你不能,或者不需要,用sudo做前缀。这取决于你的用户权限、组等等。