在 Ubuntu 上监控 PHP 的运行和配置,通常可以从PHP 本身状态、PHP-FPM、Web 服务器、系统资源、日志几个层面来做。下面按常见场景给你一套实用方案。
php -i
# 或
php --ini
查看某个配置项:
php -i | grep memory_limit
<?php
phpinfo();
⚠️ 生产环境用完要删。
如果你用的是 Nginx + PHP-FPM。
编辑:
sudo vim /etc/php/8.1/fpm/pool.d/www.conf
确保以下配置开启:
pm.status_path = /status
ping.path = /ping
Nginx 配置:
location = /status {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
allow 127.0.0.1;
deny all;
}
重启:
sudo systemctl restart php8.1-fpm
访问:
curl http://127.0.0.1/status
可以看到:
sudo tail -f /var/log/php8.1-fpm.log
php.ini 中配置:
log_errors = On
error_log = /var/log/php_errors.log
# Nginx
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
ps aux | grep php-fpm
top -p $(pgrep php-fpm | tr '\n' ',')
sudo apt install htop
htop
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm-slow.log
tail -f /var/log/php-fpm-slow.log
php-fpm-exporter| 指标 | 说明 |
|---|---|
| active processes | 当前活跃 PHP 进程 |
| idle processes | 空闲进程 |
| request count | 请求总量 |
| slow requests | 慢请求数 |
| memory usage | 内存占用 |
| 500 / 502 错误 | 服务异常 |
如果你能告诉我:
我可以给你一套更精确的监控配置示例。