我正在运行nginx+php-fpm。有没有办法知道每个PHP进程在做什么?类似于apache中的扩展mod_status,我可以看到PID为x的apache进程正在处理URL y。我不确定PHP进程是否知道URL,但获取脚本路径和名称就足够了。
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
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.conf:
location /status {
include fastcgi_params;
fastcgi_pass unix:/var/lib/php/php-fpm.sock;
}
cgi命令行更方便:
SCRIPT_NAME=/status \
SCRIPT_FILENAME=/status \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect 127.0.0.1:9000
QUERY_STRING=full
才能返回 OP 寻找的内容。将 SCRIPT_FILENAME
更改为 /status?full
是行不通的。 - Isiuspm.status_path
以启用状态页面;此方法的优点是无需通过 Web 服务器公开。SCRIPT_NAME
和 SCRIPT_FILENAME
应与 php-fpm.conf
文件中的 pm.status_path
相同。最后一个参数 (127.0.0.1:9000
) 是连接到 FCGI 服务器,而不是 Web 服务器--它应该是同一 INI 文件中 listen
参数的内容。如果它是一个套接字,您可能需要使用 sudo
进行连接,在这种情况下还要使用 -E
告诉 sudo
将环境变量传递给 cgi-fcgi
。 - Wolfgang你可以使用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。
以上将获取脚本路径。要获取 URL,请查看您的 nginx / apache 访问日志。
顺便提一下,要查看系统调用和哪些调用需要最长时间:
sudo strace -c -f $(pidof php-fpm7.0 | sed 's/\([0-9]*\)/\-p \1/g')
等待一段时间,然后按下Ctrl-C键
strace -f $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')
。 - Sos.
/php-status
)传递给php-fpm。 - Marki555请求URI
将始终显示为/index.php
。实际的URL是通过REQUEST_URI
环境变量传递的,该变量不会显示在状态输出中。 - Glenn Schmidthttp://server.com/php-status?full
。那个"?full"参数就像一盏明灯,向我展示了我一直在寻找的有问题的URI/脚本。非常感谢你! - Giorgiolino