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


当前回答

对于观看这个讨论的人来说,这是显而易见的....如果你给你的任何文件夹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.

Laravel 5.4文档说:

在安装Laravel之后,您可能需要配置一些权限。 存储和引导/缓存目录中的目录 你的web服务器应该可以写,否则Laravel将无法运行。如果你 正在使用Homestead虚拟机,这些权限应该 已经设置好了。

本页上有很多答案提到使用777权限。不要那样做。你会把自己暴露在黑客面前。

相反,请遵循其他人关于如何将权限设置为755(或更严格的权限)的建议。您可能需要通过在终端中运行whoami来确定应用程序以哪个用户的身份运行,然后使用chown -R更改某些目录的所有权。

这对我来说很管用:

cd /code/laravel_project
php artisan cache:clear
php artisan config:clear
sudo service php7.4-fpm stop
sudo service nginx stop

sudo chown -R $USER:www-data storage
sudo chown -R $USER:www-data bootstrap/cache
chmod -R 775 storage
chmod -R 755 bootstrap/cache
sudo service php7.4-fpm start && sudo service nginx start


灵感来自https://stackoverflow.com/a/45673457/470749


如果您没有权限使用sudo,因为许多其他答案需要…

您的服务器可能是一个共享主机,例如Cloudways。

(在我的例子中,我把我的Laravel应用程序克隆到我的第二台Cloudways服务器上,它不能完全工作,因为存储和引导/缓存目录的权限被弄乱了。)

我需要使用:

Cloudways平台>服务器>应用程序设置>重置权限

然后我可以在终端中运行php artisan cache:clear。

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

添加到composer.json

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

安装composer后