如何确定在PHP-FPM过程中正在执行哪个脚本

时间:2013-02-22 11:48:40

标签: fastcgi php

我正在运行nginx + php-fpm。有什么方法我怎么知道每个PHP进程在做什么?像apache中的扩展mod_status,我可以看到使用PID x的apache进程处理URL y。我不确定PHP进程是否知道URL,但获取脚本路径和名称就足够了。

4 个答案:

答案 0 :(得分:26)

经过一些谷歌搜索和浏览PHP.net错误跟踪系统后,我找到了解决方案。它自PHP 5.3.8或5.3.9起可用,但似乎没有记录。根据功能请求#54577,状态页面支持选项full,它将分别显示每个工作人员的状态。例如,URL将为http://server.com/php-status?full,示例输出如下:

pid:                  22816
state:                Idle
start time:           22/Feb/2013:15:03:42 +0100
start since:          10933
requests:             28352
request duration:     1392
request method:       GET
request URI:          /ad.php?zID=597
content length:       0
user:                 -
script:               /home/web/server.com/ad/ad.php
last request cpu:     718.39
last request memory:  1310720

答案 1 :(得分:11)

PHP-FPM有一个内置的状态监视器,虽然它不像mod_status那样详细。来自php-fpm配置文件/etc/php-fpm.d/www.conf(在CentOS 6上)

; The URI to view the FPM status page. If this value is not set, no URI will be
; recognized as a status page. By default, the status page shows the following
; information:
;   accepted conn    - the number of request accepted by the pool;
;   pool             - the name of the pool;
;   process manager  - static or dynamic;
;   idle processes   - the number of idle processes;
;   active processes - the number of active processes;
;   total processes  - the number of idle + active processes.
; The values of 'idle processes', 'active processes' and 'total processes' are
; updated each second. The value of 'accepted conn' is updated in real time.
; Example output:
;   accepted conn:   12073
;   pool:             www
;   process manager:  static
;   idle processes:   35
;   active processes: 65
;   total processes:  100
; By default the status page output is formatted as text/plain. Passing either
; 'html' or 'json' as a query string will return the corresponding output
; syntax. Example:
;   http://www.foo.bar/status
;   http://www.foo.bar/status?json
;   http://www.foo.bar/status?html
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set
;pm.status_path = /status

如果启用此功能,则可以将路径从nginx传递到PHP-FPM的套接字​​/端口,然后您可以查看状态页面。

nginx.conf:

location /status {

    include fastcgi_params;
    fastcgi_pass unix:/var/lib/php/php-fpm.sock;

}

答案 2 :(得分:5)

cgi命令行更方便:

SCRIPT_NAME=/status \
SCRIPT_FILENAME=/status \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect 127.0.0.1:9000

答案 3 :(得分:-1)

您可以使用strace实时显示正在运行的脚本 - 以及许多其他内容。它非常冗长,但它可以让您全面了解正在发生的事情:

# switch php-fpm7.0 for process you're using
sudo strace -f $(pidof php-fpm7.0 | sed 's/\([0-9]*\)/\-p \1/g')

以上将附加到php fpm的分叉进程。使用-p附加到特定的pid。

以上将获得脚本路径。要获取网址,您需要查看您的nginx / apache访问日志。

作为旁注,要查看系统调用以及哪些调用时间最长:

sudo strace -c -f $(pidof php-fpm7.0 | sed 's/\([0-9]*\)/\-p \1/g')

等一会儿,然后点击Ctr-C