1. 程式人生 > >87.PHP配置

87.PHP配置

PHP配置

查看PHP配置文件所在位置
雖然PHP是以httpd一個模塊的形式存在的,但是PHP本身也有自己的配置文件。
查看PHP配置文件所在位置的命令為:

[root@zlinux ~]# /usr/local/php/bin/php -i | grep -i "Loaded Configuration file"
PHP Warning:  Unknown: It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC‘ for now, but please set date.timezone to select your timezone. in Unknown on line 0
Loaded Configuration File => /usr/local/php/etc/php.ini
第一行是warning警告信息,可以忽略,如想想取消則需要編輯php.ini,找到date.timezone設置為:

[root@zlinux ~]# vim /usr/local/php/etc/php.ini       //取消前面;號

date.timezone = Asia/Shanghai
再次執行就不會提示警告信息了。
然後訪問一個包含phpinfo函數的頁面,會顯示這樣:

二、PHP的disable_functions

由於phpinfo會顯示出服務器內LAMP架構內的很多軟件的配置文件等重要文件的信息,所以在生產環境下最好禁掉,防止被黑客獲取到系統內部php信息,造成損失。測試環境可以不禁。

[root@zlinux ~]# vim /usr/local/php/etc/php.ini       //搜索disable_functions,編輯成如下內容

disable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close,phpinfo

[root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful       //更改要重新加載下apache服務

三、配置error_log

PHP日誌非常重要,它是排查問題的重要手段。步驟如下:

[root@zlinux ~]# vim /usr/local/php/etc/php.ini
log_errors = On        //搜索log_errors
error_log = /var/log/php/ php_errors.log      //搜索log_error,改成這個,存放日誌的路徑
error_reporting = E_ALL & ~E_NOTICE     //搜索error_reporting改成這個
display_errors = Off                                     //改為Off

[root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful
display_errors=On/Off :設定是否顯示錯誤原因,需要註意的是,此處設置為off(防止用戶看到)後必須設置錯誤日誌,設定保存路徑,和錯誤日誌級別,否則將無法查找錯誤原因 。
log_errors=On/Off 開啟/關閉錯誤日誌
“error_log=/tmp/” 設定錯誤日誌的保存路徑。如果定義好路徑後無法生產日誌,此時需要檢查日誌文件所在目錄是否有寫(w)權限
“errorreporting =” 設定錯誤日誌級別,級別有:E ALL 、~E NOTICE 、~E STRICT 、~EDEPRECATED(可以自由組合)。生產環境使用:E ALL & ~E_ NOTICE就可以。
設置完php.ini之後,需要一些額外操作:

[root@zlinux ~]# mkdir /var/log/php
[root@zlinux ~]# chmod 777 /var/log/php/                     //保證PHP錯誤日誌所在目錄存在,並且權限可讀寫
[root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful
測試效果:

[root@zlinux ~]# curl -A "www" -x 192.168.204.128:80 linuxtest.com/indextest.php -I     //故意去掉phpinfo函數的括號
HTTP/1.0 500 Internal Server Error
Date: Wed, 07 Mar 2018 07:51:56 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Connection: close
Content-Type: text/html; charset=UTF-8
出現了500狀態碼,此時就可以查看PHP錯誤日誌了:

[root@zlinux php]# cat php_errors.log 
[07-Mar-2018 16:00:49 Asia/Shanghai] PHP Parse error:  syntax error, unexpected ‘?>‘ in /data/wwwroot/123test/indextest.php on line 3

四、配置open_basedir

open_basedir可將用戶訪問文件的活動範圍限制在指定的區域,通常是其家目錄的路徑,也 
可用符號"."來代表當前目錄。註意用open_basedir指定的限制實際上是前綴,而不是目錄名。

[root@zlinux php]# vim /usr/local/php/etc/php.ini

; open_basedir, if set, limits all file operations to the defined directory
; and below.  This directive makes most sense if used in a per-directory
; or per-virtualhost web server configuration file.
; http://php.net/open-basedir
open_basedir = /usr/local/wwwroot/123test:/tmp     //多個目錄用:隔開,這個說明PHP限制在這兩個目錄活動
測試是否只能在這倆目錄下活動:

[root@zlinux php]# curl -A "www" -x 192.168.204.128:80 ztest.com/openbasedir.php -I
HTTP/1.0 500 Internal Server Error
Date: Wed, 07 Mar 2018 08:14:04 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Connection: close
Content-Type: text/html; charset=UTF-8
//狀態碼500,查看日誌
[root@zlinux php]# cat php_errors.log 
[07-Mar-2018 16:00:49 Asia/Shanghai] PHP Parse error:  syntax error, unexpected ‘?>‘ in /data/wwwroot/123test/indextest.php on line 3
[07-Mar-2018 16:13:57 Asia/Shanghai] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/wwwroot/abctest/openbasedir.php) is not within the allowed path(s): (/usr/local/wwwroot/123test:/tmp) in Unknown on line 0
[07-Mar-2018 16:13:57 Asia/Shanghai] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 16:13:57 Asia/Shanghai] PHP Fatal error:  Unknown: Failed opening required ‘/data/wwwroot/abctest/openbasedir.php‘ (include_path=‘.:/usr/local/php/lib/php‘) in Unknown on line 0
[07-Mar-2018 16:14:04 Asia/Shanghai] PHP Warning:  Unknown: open_basedir restriction in effect. File(/data/wwwroot/abctest/openbasedir.php) is not within the allowed path(s): (/usr/local/wwwroot/123test:/tmp) in Unknown on line 0
[07-Mar-2018 16:14:04 Asia/Shanghai] PHP Warning:  Unknown: failed to open stream: Operation not permitted in Unknown on line 0
[07-Mar-2018 16:14:04 Asia/Shanghai] PHP Fatal error:  Unknown: Failed opening required ‘/data/wwwroot/abctest/openbasedir.php‘ (include_path=‘.:/usr/local/php/lib/php‘) in Unknown on line 0
//通過日誌可以看出,要訪問的頁面不在規定目錄下。
也可以給單個虛擬主機設置open_basedir:

87.PHP配置