在 Ubuntu 上查看 PHP 日志,常用工具和方式取决于 PHP 的运行方式(Apache / Nginx + PHP-FPM / CLI) 以及 日志配置位置。下面按场景给你总结:
tail(最常用)实时查看日志:
tail -f /var/log/php_errors.log
查看最后 100 行:
tail -n 100 /var/log/php_errors.log
less适合大文件翻页查看:
less /var/log/php_errors.log
快捷键:
G 跳到末尾q 退出grep过滤错误:
grep -i error /var/log/php_errors.log
journalctl(systemd 日志)如果是 PHP-FPM:
journalctl -u php8.1-fpm -f
PHP 错误通常写入 Apache 错误日志:
/var/log/apache2/error.log
查看:
tail -f /var/log/apache2/error.log
PHP-FPM 有独立日志:
/var/log/php8.1-fpm.log
或:
/var/log/php-fpm/error.log
查看:
tail -f /var/log/php8.1-fpm.log
/var/log/nginx/error.log
PHP CLI 一般不走 Web 日志,错误直接输出到终端,或写入:
/var/log/php_errors.log
取决于 php.ini 配置。
php --ini
然后查看:
log_errors = On
error_log = /var/log/php_errors.log
如果是 FPM:
php-fpm8.1 -i | grep error_log
tail -f /var/log/nginx/error.log
tail -f /var/log/php8.1-fpm.log
tail -f /var/log/apache2/error.log
php script.php
# 或
tail -f /var/log/php_errors.log
如果你愿意,可以告诉我:
我可以帮你精确定位该看哪个日志文件。