在Ubuntu中调试PHP错误可按以下步骤进行:
查看错误日志
/var/log/apache2/error.log/var/log/nginx/error.logphp.ini中查看error_log配置路径sudo tail -f /path/to/error.log 实时查看日志。开启错误显示(开发环境)
编辑php.ini(路径:/etc/php/{版本号}/{apache2|fpm|cli}/php.ini),设置:
display_errors = On error_reporting = E_ALL 重启Web服务器(Apache:sudo systemctl restart apache2;Nginx+PHP-FPM:sudo systemctl restart php{版本号}-fpm)。
使用Xdebug进行断点调试
sudo apt-get install php-xdebugphp.ini,添加:zend_extension=xdebug.so xdebug.mode=debug xdebug.client_host=127.0.0.1 xdebug.client_port=9003 代码与服务器配置检查
fastcgi_pass指向PHP-FPM)。系统资源监控
使用top或htop命令查看内存、CPU使用情况,排除资源不足导致的错误。
注意:生产环境需关闭display_errors,仅通过日志记录错误。