在 Ubuntu 下查看 PHP 错误,常见有几种方式,取决于你是 命令行 PHP 还是 PHP + Web(Apache / Nginx),以及错误日志配置。
先确认 PHP 的错误日志在哪里:
php -i | grep "error_log"
php -i | grep "log_errors"
php -i | grep "display_errors"
常见输出:
display_errors => Off
log_errors => On
error_log => /var/log/php/error.log
/var/log/php/error.log
/var/log/php8.x-fpm.log
或:
/var/log/php8.1-fpm.log
/var/log/php8.2-fpm.log
查看:
tail -f /var/log/php8.1-fpm.log
/var/log/apache2/error.log
查看:
tail -f /var/log/apache2/error.log
Nginx 本身错误:
/var/log/nginx/error.log
PHP 错误仍看 PHP-FPM 日志。
CLI 通常 不写日志,直接输出到终端。
临时开启:
php -d display_errors=1 your_script.php
编辑 php.ini(先找路径):
php --ini
编辑:
sudo nano /etc/php/8.1/fpm/php.ini
设置:
display_errors = Off
log_errors = On
error_log = /var/log/php/php_error.log
创建日志文件:
sudo mkdir -p /var/log/php
sudo touch /var/log/php/php_error.log
sudo chown www-data:www-data /var/log/php/php_error.log
重启 PHP-FPM:
sudo systemctl restart php8.1-fpm
在 PHP 文件顶部加:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
⚠️ 生产环境不要开启 display_errors
# 1. 看 PHP-FPM 日志
tail -f /var/log/php*.log
# 2. 看 Web 服务日志
tail -f /var/log/nginx/error.log
tail -f /var/log/apache2/error.log
# 3. 看 PHP 配置
php -i | grep error
PHP Fatal error: Uncaught ErrorPermission deniedAllowed memory size exhaustedMaximum execution time exceeded如果你愿意,可以把:
发给我,我可以帮你精准定位。