在CentOS服務器上部署一個WordPress應用,以nginx作為Web服務器。為了使WordPress的文件上傳和更新功能正常,干脆直接將整個WordPress目錄的所有者設為nginx用戶(WordPress已對所有者配置好了寫權限),而不是給other用戶添加權限。
nginx啟動后,執行ps -ef | grep nginx,看到:
root 2462 1 0 21:39 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 2463 2462 0 21:39 ? 00:00:00 nginx: worker process
nginx 2464 2462 0 21:39 ? 00:00:00 nginx: worker process
說明nginx訪問文件時使用的用戶是nginx(Worker進程)。那么對WordPress目錄執行:
chown -R nginx:nginx wordpress/
嘗試上傳文件,卻發現仍然不能寫入。
嘗試給wordpress/wp-content/的other用戶添加寫權限,發現能正常寫入了,說明WordPress在寫文件時使用的用戶並不是nginx的用戶nginx。想到WordPress是一個PHP應用,nginx通過php-fpm來執行PHP腳本,而php-fpm又是以服務的形式在運行,因此猜想是因為執行寫入時php-fpm使用的用戶不是nginx。
執行ps -ef | grep php,看到:
root 707 1 0 19:16 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache 790 707 0 19:16 ? 00:00:00 php-fpm: pool www
apache 791 707 0 19:16 ? 00:00:00 php-fpm: pool www
apache 793 707 0 19:16 ? 00:00:00 php-fpm: pool www
apache 2175 707 0 21:22 ? 00:00:00 php-fpm: pool www
apache 2230 707 0 21:25 ? 00:00:00 php-fpm: pool www
apache 2235 707 0 21:25 ? 00:00:00 php-fpm: pool www
說明PHP寫入文件時使用的用戶是apache。那么對WordPress目錄執行:
chown -R apache:apache wordpress/
嘗試上傳,發現寫入正常了。
之前是在Ubuntu系統上測試這種方法,nginx用戶為www-data,沒有遇到這個問題,應該是php-fpm也使用了同樣的用戶,但在CentOS這里卻是不同的用戶,因而有此問題。
