在 CentOS 上排查 PHP 错误,通常是从日志位置 → 日志内容 → 常见错误类型一步步来。下面给你一套实用方法。
不同方式,日志位置不一样:
php -v
再看是:
查看 PHP-FPM 配置:
php-fpm -i | grep error_log
# 或
grep error_log /etc/php-fpm.conf
grep error_log /etc/php-fpm.d/www.conf
常见位置:
/var/log/php-fpm/error.log
/var/log/php-fpm/www-error.log
实时查看:
tail -f /var/log/php-fpm/error.log
Apache 错误日志里通常包含 PHP 错误:
/var/log/httpd/error_log
查看:
tail -f /var/log/httpd/error_log
查看 php.ini:
php --ini
常见文件:
/etc/php.ini
/etc/php.d/*.ini
搜索:
grep error_log /etc/php.ini
如果看到:
log_errors = On
error_log = /var/log/php_errors.log
那就是这个文件。
有些程序(WordPress、Laravel 等)自己写日志:
/var/www/html/xxx/wp-content/debug.log
/var/www/html/xxx/storage/logs/laravel.log
在 php.ini:
display_errors = On
error_reporting = E_ALL
重启服务:
systemctl restart php-fpm
# 或
systemctl restart httpd
⚠️ 生产环境不建议长期开启
error_reporting(E_ALL);
ini_set('display_errors', 1);
| 日志内容 | 含义 |
|---|---|
PHP Fatal error |
致命错误,脚本终止 |
PHP Warning |
警告,不终止 |
PHP Notice |
提示,通常变量未定义 |
Allowed memory size exhausted |
内存不足 |
Maximum execution time |
超时 |
Permission denied |
文件/目录权限问题 |
# 1. 看 PHP-FPM 日志
tail -f /var/log/php-fpm/error.log
# 2. 看 Web 服务日志
tail -f /var/log/nginx/error.log
tail -f /var/log/httpd/error_log
# 3. 看应用日志
ls /var/www/xxx/logs
getenforce
如果是 Enforcing,可临时:
setenforce 0
chown -R nginx:nginx /var/www
chmod -R 755 /var/www
如果你愿意,可以告诉我:
我可以直接帮你定位具体日志文件。