温馨提示×

centos php日志怎么查看

小樊
40
2026-09-03 01:38:22
栏目: 编程语言

在 CentOS 上查看 PHP 日志,主要取决于 PHP 的运行方式(Apache / Nginx+PHP-FPM / CLI) 以及你的日志配置。下面按常见情况说明:


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

查看 php.ini 中的错误日志设置:

php --ini

通常路径类似:

  • /etc/php.ini
  • /etc/php.ini (PHP 5)
  • /etc/php.d/*.ini

查看日志相关配置:

grep -E "error_log|log_errors" /etc/php.ini

常见配置:

log_errors = On
error_log = /var/log/php_errors.log

如果没有设置 error_log,PHP 错误可能会写到:

  • Apache 错误日志
  • Nginx 错误日志
  • 或直接在页面输出(若 display_errors 开启)

二、不同运行方式查看日志

1️⃣ Apache + mod_php(CentOS 常见)

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

tail -f /var/log/httpd/error_log

或:

less /var/log/httpd/error_log

2️⃣ Nginx + PHP-FPM

PHP-FPM 错误日志

查看 www.conf

grep "error_log" /etc/php-fpm.d/www.conf

常见路径:

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

查看:

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

Nginx 错误日志

tail -f /var/log/nginx/error.log

3️⃣ PHP CLI(命令行脚本)

CLI 使用的 php.ini 可能不同:

php -i | grep "error_log"

CLI 日志一般也在:

/var/log/php_errors.log

或直接输出到终端


三、实时查看日志(推荐)

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

或:

tail -f /var/log/httpd/error_log

四、常见问题排查

PHP 不写日志?

  1. 确保:
    log_errors = On
    
  2. 确保日志文件存在且可写:
    touch /var/log/php_errors.log
    chown apache:apache /var/log/php_errors.log
    
  3. 重启服务:
    systemctl restart php-fpm
    systemctl restart httpd
    

五、快速总结

运行方式 日志位置
Apache /var/log/httpd/error_log
Nginx + PHP-FPM /var/log/php-fpm/error.log
自定义 php.inierror_log
CLI 终端或 php.ini 指定

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

  • CentOS 版本
  • PHP 版本
  • Apache 还是 Nginx

我可以给你精确路径和命令

0