gpt4 book ai didi

php - 如何为 Laravel 设置文件权限?

转载 作者:IT老高 更新时间:2023-10-28 11:42:49 27 4
gpt4 key购买 nike

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

Laravel 5 要求 /storage 文件夹是可写的。我找到了很多不同的方法来使它工作,我通常以递归方式使其 777 chmod 结束。我知道这不是最好的主意。

官方文档说:

Laravel may require some permissions to be configured: folders within storage and vendor require write access by the web server.

这是否意味着网络服务器也需要访问 storagevendor 文件夹本身,还是只需要访问它们当前的内容?

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

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

  1. 更改chmod
  2. 更改文件的所有者以匹配 Web 服务器,也许设置文本编辑器(和 Finder?)跳过 询问密码,或让他们使用 sudo
  3. 更改网络服务器的所有者以匹配 os 用户(我没有知道后果)
  4. 别的东西

最佳答案

对于任何查看此讨论的人来说都是显而易见的......如果你给你的任何文件夹 777 权限,你就允许任何人读取、写入和执行该目录中的任何文件......这意味着什么您已授予任何人(全世界的任何黑客或恶意人员)上传任何文件、病毒或任何其他文件的权限,然后执行该文件...

IF YOU ARE SETTING YOUR FOLDER PERMISSIONS TO 777 YOU HAVE OPENED YOURSERVER TO ANYONE THAT CAN FIND THAT DIRECTORY. Clear enough??? :)

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

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

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

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

if you do that, the webserver owns all the files, and is also the group, and you will have some problems uploading files or working with files via FTP, because your FTP client will be logged in as you, not your webserver, so add your user to the webserver user group:

sudo usermod -a -G www-data ubuntu

Of course, this assumes your webserver is running as www-data (the Homestead default), and your user is ubuntu (it's vagrant if you are using Homestead).

Then you set all your directories to 755 and your files to 644...SET file permissions

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

SET directory permissions

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

Your user as owner

I prefer to own all the directories and files (it makes working with everything much easier), so, go to your laravel root directory:

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

然后我给自己和网络服务器权限:

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

然后授予网络服务器读取和写入存储和缓存的权限

无论您设置哪种方式,您都需要为网络服务器授予存储、缓存以及网络服务器也需要上传或写入的任何其他目录的读写权限(取决于您的情况),因此请运行以下命令上面的害羞:

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

现在,您很安全,您的网站可以正常工作,并且您可以相当轻松地处理这些文件

关于php - 如何为 Laravel 设置文件权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com