LNMP架構二十六(php-fpm的open_basedir )
二十六、php-fpm的open_basedir
將 PHP 所能開啟的檔案限制在指定的目錄樹,包括檔案本身。本指令不受安全模式開啟或者關閉的影響。
當一個指令碼試圖用例如 fopen() 或者 gzopen() 開啟一個檔案時,該檔案的位置將被檢查。
當檔案在指定的目錄樹之外時 PHP 將拒絕開啟它。
所有的符號連線都會被解析,所以不可能通過符號連線來避開此限制。
因為如果伺服器管理多個網站,在php.ini裡定義多個open_basedir就不合適了,所以要麼在虛擬主機配置檔案裡面定義,要麼在php-fpm配置檔案裡面定義,我們可以針對不同的池子(pool)定義對應的open_basedir。
-
配置open_basedir
針對[www]的pool進行配置
[[email protected] ~]# vi /usr/local/php-fpm/etc/php-fpm.d/www.conf
......
php_admin_value[open_basedir]=/data/www/test.com:/tmp/ #這裡設定了兩個目錄
......
//新增上面一行配置重啟php-fpm:
[[email protected] ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done -
測試
[[email protected] ~]# curl -x127.0.0.1:80 test.com/1.txt
test open_basedir
注意: 若open_basedir定義錯誤,就會導致無法訪問,形成404狀態碼,輸出curl直接連會提示“No input file specified.”
-
定義錯誤日誌
display_errors = Off 正常情況下,在線上這個是off的,別人不能通過瀏覽器看到你的錯誤資訊,而是把你的錯誤資訊記錄到伺服器的某一個檔案裡
......
display_errors=Off //搜尋display_errors 看是否為off,不是的話改為off。;error_log = syslog
error_log = /usr/local/php-fpm/var/log/php_error.log //搜尋error_log,在下面新增一行,定義錯誤日誌路徑。搜尋error_reporting
註釋掉自帶的error_reporting
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL //定義錯誤日誌的級別。
......
儲存退出手動建立錯誤日誌檔案:
[[email protected] ~]# touch /usr/local/php-fpm/var/log/php_error.log
[[email protected] ~]# chmod 777 /usr/local/php-fpm/var/log/php_error.log
-
測試
為了方便測試,故意將/usr/local/php-fpm/etc/php-fpm.d/www.conf 中的open_basedir路徑寫錯,來方便測試
[[email protected] ~]# curl -x127.0.0.1:80 test.com/sleep.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Tue, 09 Jan 2018 16:57:28 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.1.6檢視錯誤日誌:
[[email protected] ~]# cat /usr/local/php-fpm/var/log/php_errors.log
[09-Jan-2018 16:57:28 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/wwwroot/test.com/sleep.php) is not within the allowed path(s): (/data/wwwroot/wwtest.com:/tmp/) in Unknown on line 0
[09-Jan-2018 16:57:28 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
//錯誤提示訪問有限制