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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15023540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:23:57  来源:igfitidea点击:

How to determine which script is being executed in PHP-FPM process

fastcgiphp

提问by Marki555

I am running nginx + php-fpm. Is there any way how can I know what is each of the PHP processes doing? Something like extended mod_status in apache, where I can see that apache process with PID x is processing URL y. I'm not sure if the PHP process knows the URL, but getting the script path and name will be sufficient.

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

回答by Marki555

After some googling hours and browsing PHP.net bug tracking system I have found the solution. It is available since PHP 5.3.8 or 5.3.9, but doesn't seem to be documented. Based on feature request #54577, the status page supports option full, which will display status of each worker separately. So for example the URL will be http://server.com/php-status?fulland sample output looks like:

经过一些谷歌搜索和浏览 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

回答by sjdaws

PHP-FPM has a built in status monitor, though it's not as details as mod_status. From the php-fpm config file /etc/php-fpm.d/www.conf(on CentOS 6)

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

If you enable this, you can then pass the path from nginx to your socket/port for PHP-FPM and you can view the status page.

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

nginx.conf:

nginx.conf:

location /status {

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

}

回答by diyism

cgi command line is more convinient:

cgi 命令行更方便:

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

回答by Peter Ajtai

You can use strace to show the scripts being run - and many other things - in real time. It's pretty verbose, but it can give you a good overall picture of what's going on:

您可以使用 strace 实时显示正在运行的脚本以及许多其他内容。它非常冗长,但它可以让您对正在发生的事情有一个很好的整体了解:

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

The above will attach to the forked processes of php fpm. Use -pto attach to a particular pid.

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

The above would get the scrip path. To get the urls, you would look at your nginx / apache access logs.

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

As a side note, to see the syscalls and which ones are taking longest:

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

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

Wait a while, then hit Ctr-C

稍等片刻,然后按 Ctr-C