你这个问题比较宽泛,“Ubuntu 上 PHP 日志报错”通常有多种可能原因。下面我按常见分类 + 排查方法给你一个系统性的总结,你可以对照自己的情况。
典型日志:
PHP Parse error: syntax error
PHP Fatal error: Uncaught Error
PHP Notice / Warning
常见原因:
;、{} 不匹配排查:
php -l /path/to/file.php
查看 PHP 版本:
php -v
表现:
解决:
sudo vim /etc/php/8.1/cli/php.ini
确认:
display_errors = On
error_reporting = E_ALL
log_errors = On
重启服务:
sudo systemctl restart apache2
# 或
sudo systemctl restart php8.1-fpm
典型日志:
failed to open stream: Permission denied
原因:
/var/log/php/ 不存在www-data 用户无权写入解决:
sudo mkdir -p /var/log/php
sudo chown -R www-data:www-data /var/log/php
sudo chmod 755 /var/log/php
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
日志位置:
/var/log/apache2/error.log
常见问题:
sudo a2enmod php8.1
sudo systemctl restart apache2
日志位置:
/var/log/nginx/error.log
/var/log/php8.1-fpm.log
常见问题:
php-fpm 没启动sudo systemctl status php8.1-fpm
sudo systemctl restart php8.1-fpm
fastcgi 配置错误connect() failed: No such file or directory
典型错误:
Class 'mysqli' not found
解决:
sudo apt install php-mysql php-curl php-mbstring
sudo systemctl restart apache2
sudo tail -n 50 /var/log/apache2/error.log
# 或
sudo tail -n 50 /var/log/nginx/error.log
php -i | grep error_log
你可以直接把报错日志内容贴出来(哪怕一段),或者告诉我:
我可以帮你精确定位原因和给出解决方案。