問題描述
我看過 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。