在 CentOS 上查看 PHP 错误日志,常见位置和方式取决于你使用的 PHP 运行方式(PHP-FPM / Apache + mod_php) 以及 PHP 配置。下面按常见场景说明。
php.ini 中的日志配置php -i | grep -E "error_log|log_errors"
或查看具体 php.ini 文件(路径可能不同):
php --ini
常见配置项:
log_errors = On
error_log = /var/log/php/error.log
/var/log/php-fpm/error.log
或按 pool 区分:
/var/log/php-fpm/www-error.log
查看:
tail -f /var/log/php-fpm/www-error.log
PHP 错误通常会写入 Apache 错误日志:
/var/log/httpd/error_log
查看:
tail -f /var/log/httpd/error_log
如果 php.ini 或站点配置中指定了路径:
error_log = /var/log/php_error.log
查看:
tail -f /var/log/php_error.log
php -i | grep error_log
输出示例:
error_log => /var/log/php-fpm/www-error.log
编辑 php.ini:
log_errors = On
error_log = /var/log/php/error.log
创建目录并设置权限:
mkdir -p /var/log/php
chown -R apache:apache /var/log/php # Apache
# 或
chown -R nginx:nginx /var/log/php # Nginx
chmod 755 /var/log/php
# PHP-FPM
systemctl restart php-fpm
# Apache
systemctl restart httpd
在 PHP 文件中加入:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
| 环境 | 日志位置 |
|---|---|
| PHP-FPM | /var/log/php-fpm/ |
| Apache | /var/log/httpd/error_log |
| Nginx + PHP-FPM | PHP-FPM 日志 |
| 自定义 | php.ini error_log |
如果你愿意,可以告诉我:
我可以给你 精确到路径的命令。