问题描述
我看过 here,但没有找到任何有关最佳文件权限的详细信息。我也看过一些关于 here too 的 WordPress’s form’s 问题,但任何建议 777 的人显然需要一点安全的教训。
总之我的问题是这个。我应该有以下权限:
-
根文件夹存储所有 WordPress 内容
-
wp-admin
-
wp-content
-
wp-includes
然后在这些文件夹中的所有文件?
最佳解决方案
当您设置 WP 时 (Web 服务器) 可能需要对文件进行写入访问。所以访问权限可能需要松动。
chown www-data:www-data -R * # Let Apache be owner
find . -type d -exec chmod 755 {} ; # Change directory permissions rwxr-xr-x
find . -type f -exec chmod 644 {} ; # Change file permissions rw-r--r--
安装完成后,您应该收紧访问权限,根据 Hardening WordPress,除了 wp-content 之外的所有文件都只能由您的用户帐户写入。 wp-content 也可以由 www-data 写入。
chown <username>:<username> -R * # Let your useraccount be owner
chown www-data:www-data wp-content # Let apache be owner of wp-content
也许你想更改 wp-content 中的内容。在这种情况下你可以
-
暂时用户使用
su改为 www-data, -
给 wp-content 组写访问 775 并加入组 www-data 或
-
给您的用户使用 ACLs 的文件夹的访问权限。
无论您做什么,请确保文件具有 www-data 的 rw 权限。
次佳解决方案
给 www-data 用户 (在这种情况下是 Web 服务器用户) 的所有 wp 文件的完全访问可能是危险的。所以不要这样做:
chown www-data:www-data -R *
当您安装或升级 WordPress 及其 plug-ins 时,可能会很有用。但是当你完成它不再是保持由 web 服务器拥有的 wp 文件的好主意。
它基本上允许 Web 服务器放置或覆盖您的网站中的任何文件。这意味着如果有人设法使用 Web 服务器 (或某些.php 脚本中的安全漏洞) 将一些文件放在您的网站中,则可能会接管您的站点。
为了保护您的网站免受此类攻击,您应该遵守以下规定:
All files should be owned by your user account, and should be writable by you. Any file that needs write access from WordPress should be writable by the web server, if your hosting set up requires it, that may mean those files need to be group-owned by the user account used by the web server process.
/The root WordPress directory: all files should be writable only by your user account, except .htaccess if you want WordPress to automatically generate rewrite rules for you.
/wp-admin/The WordPress administration area: all files should be writable only by your user account.
/wp-includes/The bulk of WordPress application logic: all files should be writable only by your user account.
/wp-content/User-supplied content: intended to be writable by your user account and the web server process.
Within
/wp-content/you will find:
/wp-content/themes/Theme files. If you want to use the built-in theme editor, all files need to be writable by the web server process. If you do not want to use the built-in theme editor, all files can be writable only by your user account.
/wp-content/plugins/Plugin files: all files should be writable only by your user account.
Other directories that may be present with
/wp-content/should be documented by whichever plugin or theme requires them. Permissions may vary.
来源和附加信息:http://codex.wordpress.org/Hardening_WordPress
第三种解决方案
对于那些在他们的主文件夹下有他们的 wordpress 根文件夹的人:
** Ubuntu /apache
-
将您的用户添加到 www-data 组:
信用 Granting write permissions to www-data group
你想在你的用户上调用 usermod 。那就是:
sudo usermod -aG www-data yourUserName
**假设存在 www-data 组
-
检查您的用户是否在
www-data组中:groups yourUserName
你应该得到如下的东西:
youUserName : youUserGroupName www-data
** youUserGroupName 通常类似于您的用户名
-
递归更改 wp-content 文件夹的组所有权,保留用户所有权
chown yourUserName:www-data -R youWebSiteFolder/wp-content/* -
将目录更改为 YouWebSiteFolder /wp-content /
cd youWebSiteFolder/wp-content -
递归更改文件夹和 sub-folders 的组权限以启用写权限:
find . -type d -exec chmod -R 775 {} ;
`/home /yourUserName /youWebSiteFolder /wp-content /’ 的模式从 0755(rwxr-xr-x) 更改为 0775(rwxrwxr-x)
-
递归更改文件和 sub-files 的组权限以启用写权限:
find . -type f -exec chmod -R 664 {} ;
结果应该如下所示:
WAS:
-rw-r--r-- 1 yourUserName www-data 7192 Oct 4 00:03 filename.html
CHANGED TO:
-rw-rw-r-- 1 yourUserName www-data 7192 Oct 4 00:03 filename.html
相当于:
chmod -R ug + rw 文件夹名称
权限将如 664 文件或 775 目录。
附:如果任何人在更新插件时遇到错误'could not create directory',请在您的域的根目录中执行:server@user:~/domainame.com$ sudo chown username:www-data -R wp-content 。假设:wp-config.php 具有 FTP credentials on LocalHost define('FS_METHOD','direct');
第四种方案
我设置权限为:
# Set all files and directories user and group to wp-user
chown wp-user:wp-user -R *
# Set uploads folder user and group to www-data
chown www-data:www-data -R wp-content/uploads/
# Set all directories permissions to 755
find . -type d -exec chmod 755 {} ;
# Set all files permissions to 644
find . -type f -exec chmod 644 {} ;
在我的情况下,我创建了一个特定的 WordPress 用户,它与 apache 默认用户不同,阻止从 Web 访问该用户所拥有的那些文件。
然后它允许 apache 用户处理上传文件夹,最后设置足够安全的文件和文件夹权限。
EDITED
如果您使用的是 W3C Total Cache,那么您还应该执行下列操作:
chmod 777 wp-content/w3tc-config
chmod 777 wp-content/cache
rm -rf wp-content/cache/config
rm -rf wp-content/cache/object
rm -rf wp-content/cache/db
rm -rf wp-content/cache/minify
rm -rf wp-content/cache/page_enhanced
然后它会工作!
EDITED
经过一段时间的开发 WordPress 网站,我建议每个环境不同的文件权限:
在生产中,我不会访问用户来修改文件系统,我只允许他们上传资源,并访问某些插件特定的文件夹进行备份等。但是,在 Git 下管理项目并使用部署密钥服务器,升级插件不是很好,也不是生产。我在这里离开生产文件设置:
# Set uploads folder user and group to www-data
chown www-data:www-data -R wp-content/uploads/
www-data:www-data = apache 或 nginx 用户和组
分段将共享相同的生产权限,因为它应该是它的克隆。
最后,开发环境将可以访问更新插件,翻译,一切…
# Set uploads folder user and group to www-data
chown www-data:www-data -R wp-content/
# Set uploads folder user and group to www-data
chown your-user:root-group -R wp-content/themes
# Set uploads folder user and group to www-data
chown your-user:root-group -R wp-content/plugins/your-plugin
www-data:www-data = apache 或 nginx 用户和组 your-user:root-group =您当前的用户和根组
这些权限将允许您访问在 themes 和 your-plugin 文件夹下开发,而无需许可。其余内容将由 Apache 或 Nginx 用户拥有,以允许 WP 管理文件系统。
在创建 git repo 之前,先运行以下命令:
# Set all directories permissions to 755
find . -type d -exec chmod 755 {} ;
# Set all files permissions to 644
find . -type f -exec chmod 644 {} ;
第五种方案
该文件的正确权限为 644 该文件夹的正确权限为 755
要更改权限,请使用终端和以下命令。
find foldername -type d -exec chmod 755 {} ;
find foldername -type f -exec chmod 644 {} ;
文件夹为 755,文件为 644 。
第六种方案
我认为以下规则建议使用默认 wordpress 网站:
-
对于 wp-content 中的文件夹,设置 0755 权限:chmod -R 0755 插件 chmod -R 0755 上传 chmod -R 0755 升级
-
让 apache 用户成为 wp-content 的上述目录的所有者:chown apache uploads chown apache upgrade chown apache plugins
第七种方案
它实际上取决于您计划使用的插件,因为某些插件会更改 wordpress 的根文档。但是通常我会推荐一下这样的 wordpress 目录。
这将分配”root”(或任何您正在使用的用户) 作为用户在每个单个文件/文件夹,R 表示递归,所以它不会停止在”html” 文件夹。如果您没有使用 R,那么它仅适用于”html” 目录。
sudo chown -R root:www-data /var/www/html
这将”wp-content” 的所有者/组设置为”www-data”,从而允许 Web 服务器通过管理面板安装插件。
chown -R www-data:www-data /var/www/html/wp-content
这将将”html” 文件夹 (包括子目录中的文件) 中的每个文件的权限设置为 644,因此外部人员无法执行任何文件,修改任何文件,组不能执行任何文件,修改任何文件,只有用户允许修改/读取文件,但是甚至用户也不能执行任何文件。这很重要,因为它阻止了”html” 文件夹中的任何类型的执行,由于 html 文件夹的所有者以及除了 wp-content 文件夹以外的所有其他文件夹都是”root”(或您的用户),www-data 无法修改 wp-content 之外的任何文件文件夹,因此即使 Web 服务器中存在任何漏洞,如果有人未经授权访问该站点,则除了插件外,它们不能删除主站点。
sudo find /var/www/html -type f -exec chmod 644 {} +
这将限制使用 rw-r —– 这些权限访问”wp-config.php” 到用户/组的权限。
chmod 640 /var/www/html/wp-config.php
如果一个插件或更新被抱怨,它无法更新,那么访问 SSH 并使用此命令,并通过管理面板向”www-data”(Web 服务器) 授予临时权限以进行更新/安装,然后还原回”root” 或您的用户一旦完成。
chown -R www-data /var/www/html
并且在 Nginx(与 apache 相同的过程) 来保护 wp-admin 文件夹免遭未经授权的访问和探测。即使您已安装 nginx,也需要使用 apache2-utils 来加密密码,如果您打算将更多用户添加到同一个文件中,则不需要 c 。
sudo apt-get install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd userName
现在访问这个位置
/etc/nginx/sites-available/
使用此代码来保护”wp-admin” 文件夹的密码,如果您尝试访问”wp-admin”,则会询问密码/用户名。通知,这里您使用包含加密密码的”.htpasswd” 文件。
location ^~ /wp-admin {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
index index.php index.html index.htm;
}
现在重新启动 nginx 。
sudo /etc/init.d/nginx restart
第八种方案
命令:
chown www-data:www-data -R *
find . -type d -exec chmod 755 {} ;
find . -type f -exec chmod 644 {} ;
哪里 ftp-user 是您用来上传文件的用户
chown -R ftp-user:www-data wp-content
chmod -R 775 wp-content
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。