温馨提示×

centos php配置如何调试

小樊
45
2026-10-11 16:26:05
栏目: 编程语言

在 CentOS 上调试 PHP,一般从 错误显示、日志、运行方式(FPM / CLI)、工具 几个方面入手。下面按常见场景说明。


一、开启 PHP 错误显示(开发环境)

1. 修改 php.ini

找到 php.ini(位置可能是):

php --ini
# 常见路径
/etc/php.ini
/etc/php.d/*.ini

修改或确认以下配置:

display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php_errors.log

重启服务:

# PHP-FPM
systemctl restart php-fpm

# Apache
systemctl restart httpd

# Nginx(一般配合 php-fpm)
systemctl restart nginx

二、查看 PHP 错误日志(推荐)

即使不显示错误,也要看日志:

tail -f /var/log/php_errors.log

若使用 PHP-FPM:

tail -f /var/log/php-fpm/error.log

三、CLI 模式调试(命令行 PHP)

直接运行脚本看错误:

php -d display_errors=1 your_script.php

查看 PHP 配置:

php -i | grep error

四、Xdebug 调试(断点调试,强烈推荐)

1. 安装 Xdebug

yum install php-pear php-devel gcc
pecl install xdebug

或在 CentOS 8+:

dnf install php-xdebug

2. 配置 Xdebug

编辑:

/etc/php.d/15-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

重启 PHP:

systemctl restart php-fpm

3. 配合 IDE

  • PHPStorm
  • VS Code(Xdebug 插件)

五、Nginx / Apache 相关调试

Nginx

查看错误:

tail -f /var/log/nginx/error.log

确认 fastcgi 配置正确:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Apache

tail -f /var/log/httpd/error_log

六、常见排错清单

  • PHP 不执行 → 看 php-fpm 是否运行
  • 502 错误 → php-fpm 挂了
  • 空白页 → display_errors=Off
  • 权限问题 → chown -R nginx:nginx / chmod

七、生产环境建议

display_errors = Off
log_errors = On
error_reporting = E_ALL & ~E_DEPRECATED

如果你能告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • PHP 运行方式(Apache / Nginx + FPM / CLI)
  • 具体报错现象(空白页 / 500 / 日志内容)

我可以给你更精确的调试方案。

0 踩