温馨提示×

centos怎样调试php错误

小樊
45
2026-09-29 23:12:06
栏目: 编程语言

在 CentOS 上调试 PHP 错误,通常从开启错误显示/日志、查看日志、使用调试工具几个方面入手。下面按常见场景给你一套实用做法。


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

1. 修改 php.ini

先找到 php.ini 位置:

php --ini
# 或
php -i | grep "Loaded Configuration File"

常见路径:

  • /etc/php.ini
  • /etc/php.d/*.ini

编辑 php.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

二、临时在代码中开启(推荐测试用)

不想改全局配置时,在 PHP 文件最前面加:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

三、查看 PHP 错误日志(生产环境推荐)

1. PHP 错误日志

tail -f /var/log/php_errors.log

2. Web 服务日志

  • Apache:
tail -f /var/log/httpd/error_log
  • Nginx + PHP-FPM:
tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/error.log

四、常见错误类型及排查

1. 500 错误

  • 看 error_log
  • 检查 PHP 语法:
php -l file.php

2. 白屏(无输出)

  • display_errors 没开
  • 有致命错误(Fatal Error)

3. 权限问题

chown -R nginx:nginx /var/www/html
chmod -R 755 /var/www/html

五、使用 Xdebug(进阶调试)

1. 安装 Xdebug

yum install php-xdebug
# 或
pecl install xdebug

2. 配置 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

3. 用 IDE(VS Code / PHPStorm)断点调试


六、快速排查清单 ✅

  • [ ] display_errors 是否开启
  • [ ] error_log 是否有内容
  • [ ] PHP 版本是否匹配
  • [ ] 文件/目录权限是否正确
  • [ ] 是否用了 opcode 缓存(opcache)

如果你能贴出:

  • CentOS 版本
  • PHP 版本
  • Web 服务(Apache / Nginx)
  • 具体错误信息

我可以直接帮你定位问题。

0 踩