在Ubuntu下进行PHP代码调试常用方法如下:
使用Xdebug扩展
sudo apt install php-xdebug
。php.ini
(路径:/etc/php/{版本}/cli/php.ini
或/etc/php/{版本}/apache2/php.ini
),添加:zend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=yes
```。
sudo systemctl restart apache2
(Apache)或sudo systemctl restart php{版本}-fpm
(PHP-FPM)。Settings > PHP > Servers
中添加服务器,设置断点后启动调试。使用内置错误报告
php.ini
中启用:display_errors=On
、log_errors=On
,并指定日志路径error_log=/var/log/php_errors.log
。error_log()
函数手动记录日志。使用日志库
composer require monolog/monolog
,在代码中记录日志到文件。命令行调试
xdebug
命令行工具(需配合xdebug
扩展)。API调试工具
注意:生产环境需关闭display_errors
,避免敏感信息泄露。