在 CentOS 上调试 PHP 错误,通常从开启错误显示/日志、查看日志、使用调试工具几个方面入手。下面按常见场景给你一套实用做法。
先找到 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);
tail -f /var/log/php_errors.log
tail -f /var/log/httpd/error_log
tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/error.log
error_logphp -l file.php
display_errors 没开chown -R nginx:nginx /var/www/html
chmod -R 755 /var/www/html
yum install php-xdebug
# 或
pecl install xdebug
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
如果你能贴出:
我可以直接帮你定位问题。