一、問題描述
今天遷移了wordpress站點(diǎn)至新服務(wù)器,結(jié)果上傳圖片出現(xiàn)“無法將上傳的文件移動(dòng)至wp-content/uploads”的提示,這是怎么回事,為什么會(huì)這樣。
報(bào)錯(cuò)如下:
2023/02/20 08:57:48 [error] 9861#9861: *79624 FastCGI sent in stderr: "PHP message: PHP Warning: file_put_contents(/usr/share/nginx/html/wordpress/wp-content/uploads/wpo/images/wpo_logo_small.png.webp): failed to open stream: Permission denied in /usr/share/nginx/html/wordpress/wp-content/plugins/wp-optimize/vendor/rosell-dk/webp-convert/src/Convert/Converters/BaseTraits/DestinationPreparationTrait.php on line 71
PHP message: PHP Warning: file_put_contents(/usr/share/nginx/html/wordpress/wp-content/uploads/wpo/images/wpo_logo_small.png.webp): failed to open stream: Permission denied in
/usr/share/nginx/html/wordpress/wp-content/plugins/wp-optimize/vendor/rosell-dk/webp-convert/src/Convert/Converters/BaseTraits/DestinationPreparationTrait.php on line 71
PHP message: PHP Warning: file_put_contents(/usr/share/nginx/html/wordpress/wp-content/uploads/wpo/images/wpo_logo_small.png.webp): failed to open stream: Permission denied in
/usr/share/nginx/html/wordpress/wp-content/plugins/wp-optimize/vendor/rosell-dk/webp-convert/src/Convert/Converters/BaseTraits/DestinationPreparationTrait.php on line 71
PHP message: PHP Warning: file_put_contents(/usr/share/nginx/html/wordpress/wp-content/uploads/wpo/images/wpo_logo_small.png.webp): failed to open stream: Permission denied in
/usr/share/nginx/html/wordpress/wp-content/plugins/wp-optimize/vendor/rosell-dk/webp-convert/src/Convert/Converters/BaseTraits/DestinationPreparationTrait.php on line 71
PHP message: PHP Warning: file_put_contents(/usr/share/nginx/html/wordpress/wp-content/uploads/wpo/images/wpo_logo_small.png.webp): failed to open stream: Permission denied in
/usr/share/nginx/html/wordpress/wp-content/plugins/wp-optimize/vendor/rosell-dk/webp-convert/src/Convert/Converters/BaseTraits/DestinationPreparationTrait.php on line 71
PHP message: PHP Warning: file_put_contents(/usr/share/nginx/html/wordpress/wp-content/uploads/wpo/images/wpo_logo_small.png.webp): failed to open stream: Permission denied in
/usr/share/nginx/html/wordpress/wp-content/plugins/wp-optimize/vendor
以上錯(cuò)誤日志中,主要的錯(cuò)誤信息為:
failed to open stream: Permission denied
即無法打開二進(jìn)制流(即上傳的圖片),權(quán)限被拒絕
出現(xiàn)此類問題時(shí),一定要查看錯(cuò)誤日志!?。?/strong>
nginx的錯(cuò)誤日志、php的錯(cuò)誤日志,系統(tǒng)的日志等等,需要仔細(xì)查看錯(cuò)誤信息,一定會(huì)給出相應(yīng)的錯(cuò)誤信息的,如果實(shí)在不行,可以開啟php的debug模式
二、問題解決
2.1 修改屬主屬組
- 新建nginx用戶組
2.1 新建nginx用戶組
useradd nginx -s /sbin/nologin -M
- 修改nginx配置文件
# vim /etc/nginx/nginx.conf
user nginx;
- 修改php-fpm配置文件
使用find命令找到www.conf文件
find / -name www.conf
修改php啟動(dòng)用戶和用戶組
# vim /etc/php-fpm/www.conf
user = nginx
group = nginx
- 重新加載nginx和php-rpm
systemctl restart nginx
systemctl restart php-rpm
- 修改wordpress 用戶和用戶組
進(jìn)入wordpress根目錄(判斷標(biāo)準(zhǔn)是包含wp-admin、wp-content、wp-includes三個(gè)目錄),并將根目錄下所有文件用戶組改成nginx。
修改文件屬主
chown -R nginx wordpress
修改文件用戶組
chgrp -R nginx wordpress
-R表示更改wordpress目錄及其下所有文件和目錄。
- 驗(yàn)證生效
ps aux | grep nginx
2.2 賦予所有權(quán)限
找到所在的文件目錄,設(shè)置文件夾權(quán)限為777即可。(該方式相對比較暴力)
chmod -R 777 /usr/share/nginx/html/wordpress
2.3 關(guān)閉防火墻及selinux
以上兩個(gè)問題是網(wǎng)上最多人提出的,但下面這個(gè)selinux的問題一定是你萬萬沒想到的。
在 CentOS 7 上,SELinux 默認(rèn)處于啟用狀態(tài)。SELinux 通過限制和定義服務(wù)器處理請求以及用戶與套接字、網(wǎng)絡(luò)端口和基本目錄交互的方式來提高服務(wù)器安全性。SELinux 可能會(huì)在訪問文件或目錄或?qū)⑽募蚰夸泴懭?DocumentRoot 時(shí)產(chǎn)生問題,
解決方法是 :關(guān)閉selinux
永久方法 – 需要重啟服務(wù)器
修改/etc/selinux/config文件中設(shè)置SELINUX=disabled ,然后重啟服務(wù)器。
臨時(shí)方法 – 設(shè)置系統(tǒng)參數(shù)
使用命令
setenforce 0
注:
setenforce 1 設(shè)置SELinux 成為enforcing模式 (開啟)
setenforce 0 設(shè)置SELinux 成為permissive模式(關(guān)閉)
2.4 設(shè)置selinux
出于安全原因,不建議禁用 SELinux,因?yàn)閷⒃试S整個(gè)系統(tǒng)受到損害。但是,我們可以更新SELinux 策略以允許在特定目錄上進(jìn)行讀寫。下面是設(shè)置 SELinux 策略以允許apache用戶讀取和寫入 wordpress 下特定目錄的完整命令集。文章來源:http://www.zghlxwxcb.cn/news/detail-701693.html
- 首先重新建立SELinux上下文
restorecon -Rv /usr/share/nginx/html/wordpress
- 更改 webroot 的所有者
chown -R nginx:nginx /usr/share/nginx/html/wordpress
- 更改基本權(quán)限
chmod -R g+w /usr/share/nginx/html/wordpress
chmod g+s /usr/share/nginx/html/wordpress
- 建立 SELinux 權(quán)限
# 將所有文件設(shè)為只讀
chcon -R -t httpd_sys_content_t /usr/share/nginx/html/wordpress
# 只允許寫入上傳目錄
chcon -R -t httpd_sys_rw_content_t /usr/share/nginx/html/wordpress/wp-content/uploads/
2.5 遠(yuǎn)程文件參數(shù)開啟
vim php.ini
設(shè)置 allow_url_fopen = On //該選項(xiàng)默認(rèn)已開啟,為on便是激活了 URL 形式的 fopen 封裝協(xié)議使得可以訪問 URL 對象文件等。
注:allow_url_fopen關(guān)閉時(shí)fopen和file_get_contents都不能打開遠(yuǎn)程文件。文章來源地址http://www.zghlxwxcb.cn/news/detail-701693.html
到了這里,關(guān)于踩了大坑:wordpress后臺 無法將上傳的文件移動(dòng)至wp-content的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!