php-fpm 记录慢执行日志

有时候我们访问一个 php 的网站很慢,要想知道慢的原因,需要配置 php-fpm.conf,记录执行慢的日志:

[root@localhost ~]$ cat /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log

[www]
......                                           # 需要在进程池中定义
request_slowlog_timeout = 1                      # 执行超过多少秒就算慢执行
slowlog = /usr/local/php/var/log/www-slow.log    # 慢执行日志的路径
[root@localhost ~]$ /usr/local/php/sbin/php-fpm -t
[root@localhost ~]$ /etc/init.d/php-fpm reload

模拟慢执行:

[root@localhost ~]$ cat /usr/local/nginx/conf/vhost/test.com.conf 
server {
    listen 80;
    server_name www.test.com;
    index index.html index.html index.php;
    root /data/www;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
} 
[root@localhost ~]$ cat /data/www/slow.php 
<?php
echo "test slow log\n";
sleep(2);
echo "done\n";
?>
[root@localhost ~]$ curl -x 127.0.0.1:80 www.test.com/slow.php
test slow log
done

查看是否有慢执行日志:

[root@localhost ~]$ cat /usr/local/php/var/log/www-slow.log 

[21-Apr-2018 14:00:01]  [pool www] pid 87420         # 基础信息
script_filename = /data/www/slow.php                 # 哪一个php脚本执行慢
[0x00007f8c73f2b258] sleep() /data/www/slow.php:3    # 脚本的哪一行执行慢

 

 

 

 

 

 

 

 

      

posted @ 2019-01-31 11:52  孔雀东南飞  阅读(817)  评论(0编辑  收藏  举报