一、查看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配置_第1张图片

二、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:

[root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf  //添加如下内容

php_admin_value open_basedir "/data/wwwroot/123test;:/tmp"

说明:
“php_admin_value”可以定义php.ini中的参数。使用该办法分别在每个虚拟主机设定相关的“open_basedir”即可!
在此开放“/tmp/”目录是为了使临时文件能正常写入。