在 Ubuntu 上调试 PHP-FPM 程序,常见方式取决于你是想看错误日志、断点调试,还是性能分析。下面按实用场景给你一套完整方案。
php -v
php-fpm -v
查看 PHP-FPM 服务状态:
systemctl status php-fpm
# 或(Ubuntu 常见)
systemctl status php8.1-fpm
找到 PHP-FPM 使用的 ini:
php --ini
一般是:
/etc/php/8.1/fpm/php.ini
修改以下内容:
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
重启 PHP-FPM:
systemctl restart php8.1-fpm
⚠️ 生产环境不要开 display_errors
/etc/php/8.1/fpm/php-fpm.conf
常见配置:
error_log = /var/log/php8.1-fpm.log
查看日志:
tail -f /var/log/php8.1-fpm.log
/var/log/php8.1-fpm.log
# 或
/var/log/nginx/error.log
apt install php-xdebug
或:
pecl install xdebug
编辑:
/etc/php/8.1/fpm/conf.d/20-xdebug.ini
示例(远程调试):
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.idekey=PHPSTORM
重启:
systemctl restart php8.1-fpm
Settings → PHP → Debug → DBGp浏览器访问即触发断点。
php -f index.php
或带错误:
php -d display_errors=1 index.php
/etc/php/8.1/fpm/pool.d/www.conf
slowlog = /var/log/php-slow.log
request_slowlog_timeout = 5s
重启 FPM。
查看慢请求:
tail -f /var/log/php-slow.log
xdebug.mode=profile
xdebug.output_dir=/tmp/xdebug
用 QCacheGrind / KCacheGrind 分析。
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
检查:
nginx -t
systemctl restart nginx
✅ 开发环境
✅ 生产环境
如果你愿意,可以告诉我:
我可以给你更精确的配置。