转载自
http://www.hiceon.com/topic/how-to-enable-slow-logging-configuration-via-php-fpm-to-detect-slower-php-script-execution/
php-fpm慢日志slowlog设置可以让开发者很好的查找哪些php进程速度过慢而导致的网站问题,让开发者方便的找到问题的所在。该方法同样适用于排查nginx的500、502问题根源,当nginx收到如上错误码时,可以确定后端php-fpm解析php出了某种问题,比如,执行错误,执行超时。
php-fpm.conf的配置文件中有一个参数request_slowlog_timeout是这样描述的:
; The timeout for serving a single request after which a PHP backtrace will be ; dumped to the 'slowlog' file. A value of '0s' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0 ; request_slowlog_timeout = 0
当request_slowlog_timeout设为一个具体秒时request_slowlog_timeout =5,表示如果哪个脚本执行时间大于5秒,会记录这个脚本到慢日志文件中。
request_slowlog_timeout =0表示关闭慢日志输出。
慢日志文件位置默认在php的安装目录下的log文件夹中,可以通过修改slowlog = log/$pool.log.slow参数来指定。php-fpm慢日志的例子,慢日志会记录下进程号,脚本名称,具体哪个文件哪行代码的哪个函数执行时间过长。
[21-Nov-2013 14:30:38] [pool www] pid 11877 script_filename = /usr/local/nginx/html/www.quancha.cn/www/fyzb.php [0xb70fb88c] file_get_contents() /usr/local/nginx/html/www.quancha.cn/www/fyzb.php:2 [21-Nov-2013 14:15:23] ERROR: [pool www] 'slowlog' must be specified for use with 'request_slowlog_timeout'
request_slowlog_timeout 和 slowlog 需要同时设置,开启 request_slowlog_timeout 的同时需要开启 slowlog
[21-Nov-2013 14:16:27] ERROR: Unable to create or open slowlog(/usr/local/php/log/www.log.slow): No such file or directory (2)
慢日志路径需要手动创建,具体开启php-fpm慢日志步骤:
cd /usr/local/php vi etc/php-fpm.conf 去掉request_slowlog_timeout 、slowlog的前缀分号';',设置request_slowlog_timeout =5; :wq 保存退出 创建慢日志目录 mkdir log 重启php-fpm kill -INT `cat var/run/php-fpm.pid sbin/php-fpm
【实际操作】
1. cd /usr/local/php5/ 2. sed -i.$(date +%F) '/;slowlog/s/;slowlog/slowlog/g' etc/php-fpm.conf 3. sed -i '/;request_slowlog_timeout = 0/s/;request_slowlog_timeout = 0/request_slowlog_timeout = 5/g' etc/php-fpm.conf 4. mkdir log 5. 测试配置文件 # /usr/local/php5/sbin/php-fpm -t [29-Sep-2015 18:58:35] NOTICE: configuration file /usr/local/php5/etc/php-fpm.conf tes t is successful 6. 平滑重启 #/etc/init.d/php-fpm reload Reload service php-fpm done 7. 查看日志 [root@movie php5]# ll log/ total 0 -rw------- 1 root root 0 Sep 29 18:35 www.log.slow
其中慢查询日志的文件名是由slowlog = log/$pool.log.slow指定的。
默认文件名为$pool.log.slow,而变量pool可在php-fpm配置文件中指定。
119 ; Pool Definitions ;
120 ;;;;;;;;;;;;;;;;;;;;
121
122 ; Multiple pools of child processes may be started with different listening
123 ; ports and different management options. The name of the pool will be
124 ; used in logs and stats. There is no limitation on the number of pools which
125 ; FPM can handle. Your system will tell you anyway :)
126
127 ; Start a new pool named 'www'.
128 ; the variable $pool can we used in any directive and will be replaced by the
129 ; pool name ('www' here)
130 [www]
将130行的的[www]更改为[test],平滑重启后,即可看到poolname变为test,产生新的日志文件test.log.slow
[root@movie php5]# ll log/
total 0
-rw------- 1 root root 0 Sep 29 19:24 test.log.slow
-rw------- 1 root root 0 Sep 29 18:35 www.log.slow