在 Ubuntu 上实时查看 PHP 日志,常用方法取决于 PHP 的运行方式(PHP-FPM / Apache / CLI) 以及 日志位置。下面按最常见情况说明。
日志通常在:
/var/log/php8.1-fpm.log
/var/log/php/error.log
或你 php-fpm.conf / www.conf 里配置的:
error_log = /var/log/php-fpm.log
php_admin_value[error_log] = /var/log/php/www-error.log
一般直接写进 Apache 错误日志:
/var/log/apache2/error.log
如果在 php.ini 里设置了:
error_log = /var/log/php/php-error.log
sudo tail -f /var/log/php8.1-fpm.log
如果是 Apache:
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/apache2/error.log | grep PHP
或:
sudo tail -f /var/log/php/error.log | grep -i error
检查:
php --ini
看 php.ini:
log_errors = On
error_log = /var/log/php/php-error.log
重启 PHP:
sudo systemctl restart php8.1-fpm
确保 Web 用户可写:
sudo chown www-data:www-data /var/log/php
sudo chmod 755 /var/log/php
error_log("test log");
php -S localhost:8000
错误直接输出到终端。
sudo tail -f /var/log/php/*.log
sudo less +F /var/log/php/error.log
按 Ctrl+C 暂停,F 继续。
如果你愿意,可以告诉我:
我可以帮你 精确定位并直接给命令。