温馨提示×

如何查看PHP错误日志CentOS

小樊
36
2026-08-04 06:16:50
栏目: 编程语言

CentOS 上查看 PHP 错误日志,常见位置和方式取决于你使用的 PHP 运行方式(PHP-FPM / Apache + mod_php) 以及 PHP 配置。下面按常见场景说明。


一、先确认 PHP 错误日志配置

1️⃣ 查看 php.ini 中的日志配置

php -i | grep -E "error_log|log_errors"

或查看具体 php.ini 文件(路径可能不同):

php --ini

常见配置项:

log_errors = On
error_log = /var/log/php/error.log

二、常见 PHP 错误日志位置(CentOS)

✅ 1. PHP-FPM(最常见,Nginx / 新系统)

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

或按 pool 区分:

/var/log/php-fpm/www-error.log

查看:

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

✅ 2. Apache + mod_php

PHP 错误通常会写入 Apache 错误日志:

/var/log/httpd/error_log

查看:

tail -f /var/log/httpd/error_log

✅ 3. 自定义 error_log

如果 php.ini 或站点配置中指定了路径:

error_log = /var/log/php_error.log

查看:

tail -f /var/log/php_error.log

三、快速定位 PHP 错误日志(推荐)

php -i | grep error_log

输出示例:

error_log => /var/log/php-fpm/www-error.log

四、如果日志不存在怎么办?

1️⃣ 开启错误日志

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

2️⃣ 重启服务

# 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

如果你愿意,可以告诉我:

  • ✅ CentOS 版本(7 / 8 / Stream)
  • ✅ Web 服务(Apache / Nginx)
  • ✅ PHP 版本

我可以给你 精确到路径的命令

0