温馨提示×

ubuntu php日志如何排查

小樊
58
2026-09-24 19:24:34
栏目: 编程语言

在 Ubuntu 上排查 PHP 日志,通常可以从 PHP 自身错误日志、Web 服务器(Nginx/Apache)日志、应用框架日志 三个层面入手。下面按实用顺序说明。


一、确认 PHP 错误日志位置

1. 查看 php.ini 配置

php --ini

或

php -i | grep "Loaded Configuration File"

查看以下两项:

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

如果 error_log 为空,PHP 通常会把错误输出到 Web 服务器日志。


二、常见 PHP 日志位置(Ubuntu 默认)

PHP-FPM(最常见,Nginx + PHP-FPM)

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

例如:

/var/log/php8.1-fpm.log
/var/log/php8.1-fpm/www-error.log

Apache + mod_php

/var/log/apache2/error.log

Nginx + PHP-FPM

/var/log/nginx/error.log
/var/log/php*/www-error.log

三、实时查看日志(排查首选)

tail -f /var/log/php8.1-fpm/www-error.log

或:

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

访问页面,观察报错。


四、开启详细错误(调试用)

临时开启(不推荐生产)

编辑 php.ini:

display_errors = On
error_reporting = E_ALL

重启:

systemctl restart php8.1-fpm

五、常见错误类型与排查思路

1. 500 错误

看:

/var/log/nginx/error.log
/var/log/php*/www-error.log

常见原因:

  • PHP 语法错误
  • 权限不足
  • 内存超限

2. 空白页面

  • display_errors 没开
  • 错误被写入日志但未显示

解决:

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

3. 权限问题

Permission denied

检查:

ls -l /var/www/html
ps aux | grep php-fpm

PHP 运行用户(如 www-data)需有读/写权限。


4. 内存或超时

日志中常见:

Allowed memory size exhausted
Maximum execution time exceeded

调整:

memory_limit = 256M
max_execution_time = 120

六、框架日志(如 Laravel / ThinkPHP)

Laravel

storage/logs/laravel.log

ThinkPHP

runtime/log/

七、快速排查 checklist

# 1. 看 PHP 日志
tail -f /var/log/php*/www-error.log

# 2. 看 Web 服务日志
tail -f /var/log/nginx/error.log

# 3. 检查 PHP 是否运行
systemctl status php8.1-fpm

# 4. 检查语法
php -l index.php

如果你能提供:

  • Ubuntu 版本
  • PHP 版本
  • Web 服务器(Nginx / Apache)
  • 具体报错信息

我可以给你更精确的排查方案。

0 踩