在 Ubuntu 上排查 PHP 日志来源,通常要从 Web 服务器 + PHP 运行方式 + 应用自身日志 三个层面来看。下面按常见场景说明。
不同运行方式,日志位置不同:
php -v
/var/log/apache2/error.log
/var/log/apache2/access.log
查看 PHP 错误:
tail -f /var/log/apache2/error.log
/var/log/nginx/error.log
/var/log/nginx/access.log
先看 PHP 版本:
php -v
例如 PHP 8.1:
/var/log/php8.1-fpm.log
或:
/var/log/php-fpm.log
查看:
tail -f /var/log/php8.1-fpm.log
php --ini
常见路径:
/etc/php/8.1/cli/php.ini
/etc/php/8.1/fpm/php.ini
/etc/php/8.1/apache2/php.ini
查找日志配置:
grep -E "error_log|log_errors" /etc/php/8.1/fpm/php.ini
示例:
log_errors = On
error_log = /var/log/php_errors.log
如果没有设置,PHP 会写到 Web 服务器错误日志里。
比如日志里有:
PHP Warning: xxx in /var/www/html/index.php on line 20
直接看文件:
vim /var/www/html/index.php
storage/logs/laravel.log
实时看:
tail -f storage/logs/laravel.log
runtime/log/
tail -f /var/log/nginx/access.log
示例:
192.168.1.10 - - [time] "GET /index.php"
error_log(
"IP: " . $_SERVER['REMOTE_ADDR'] .
" URL: " . $_SERVER['REQUEST_URI']
);
# PHP 错误
tail -f /var/log/php_errors.log
# Nginx
tail -f /var/log/nginx/error.log
# PHP-FPM
tail -f /var/log/php8.1-fpm.log
# Apache
tail -f /var/log/apache2/error.log
如果你愿意,可以告诉我:
我可以直接帮你精确定位是哪一行代码、哪个请求来源。