查看错误日志
tail -f /var/log/apache2/error.log
tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm.log
开启PHP错误显示(开发环境)
编辑 php.ini
,设置:
error_reporting = E_ALL
display_errors = On
log_errors = On
error_log = /var/log/php_errors.log # 确保路径正确
重启PHP服务:sudo systemctl restart php-fpm
。
检查文件权限
确保PHP文件及目录权限正确:
sudo chown -R www-data:www-data /var/www/html # 用户组需与Web服务器一致
sudo find /var/www/html -type d -exec chmod 755 {} \;
sudo find /var/www/html -type f -exec chmod 644 {} \;
排查语法与依赖问题
php -l 文件名.php
检查语法错误。sudo apt-get install php-mysql
)。检查服务器配置
AllowOverride All
已启用(允许.htaccess覆盖)。mv .htaccess .htaccess.bak
。重启服务
修改配置后,重启Web服务器:
sudo systemctl restart apache2 # Apache
sudo systemctl restart nginx # Nginx
提示:生产环境中建议关闭 display_errors
,通过日志定位问题后修复,避免暴露敏感信息。若问题仍未解决,可结合Xdebug等工具进行代码级调试。