温馨提示×

ubuntu php fpm怎样调试程序

小樊
61
2026-09-04 13:21:37
栏目: 编程语言

在 Ubuntu 上调试 PHP-FPM 程序,常见方式取决于你是想看错误日志、断点调试,还是性能分析。下面按实用场景给你一套完整方案。


一、先确认基本环境

php -v
php-fpm -v

查看 PHP-FPM 服务状态:

systemctl status php-fpm
# 或(Ubuntu 常见)
systemctl status php8.1-fpm

二、打开 PHP 错误显示(最基础)

1️⃣ 修改 php.ini

找到 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


三、查看 PHP-FPM 错误日志(非常重要)

PHP-FPM 日志位置

/etc/php/8.1/fpm/php-fpm.conf

常见配置:

error_log = /var/log/php8.1-fpm.log

查看日志:

tail -f /var/log/php8.1-fpm.log

PHP 脚本错误日志

/var/log/php8.1-fpm.log
# 或
/var/log/nginx/error.log

四、使用 Xdebug 断点调试(推荐)

1️⃣ 安装 Xdebug

apt install php-xdebug

或:

pecl install xdebug

2️⃣ 配置 Xdebug(FPM)

编辑:

/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

3️⃣ IDE 配置

  • PHPStorm:
    Settings → PHP → Debug → DBGp
  • VS Code:
    安装 PHP Debug 插件

浏览器访问即触发断点。


五、命令行快速调试(不依赖 Web)

php -f index.php

或带错误:

php -d display_errors=1 index.php

六、性能 / 慢脚本调试

1️⃣ PHP-FPM 慢日志

/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

2️⃣ Xdebug Profiler

xdebug.mode=profile
xdebug.output_dir=/tmp/xdebug

用 QCacheGrind / KCacheGrind 分析。


七、Nginx + PHP-FPM 常见问题排查

Nginx 配置示例

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

八、推荐调试组合(实战)

✅ 开发环境

  • Xdebug + PHPStorm
  • display_errors = On

✅ 生产环境

  • error_log
  • slowlog
  • 关闭 display_errors

如果你愿意,可以告诉我:

  • PHP 版本
  • Web 服务器(Nginx / Apache)
  • 是「页面空白」「500 错误」还是「想打断点」

我可以给你更精确的配置。

0 踩