在Ubuntu中調試PHP錯誤可按以下步驟進行:
查看錯誤日志
/var/log/apache2/error.log
/var/log/nginx/error.log
php.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-xdebug
php.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
,僅通過日志記錄錯誤。