XDEBUG调试配置
字数 835 2025-08-11 21:26:33
XDEBUG调试配置教程
环境准备
- 操作系统:Windows 10
- 集成环境:PHPStudy 8.1
- 代码编辑器:VSCode
- 调试类型:本地调试
配置步骤
1. 启用Xdebug扩展
- 打开PHPStudy控制面板
- 进入"PHP扩展"选项
- 勾选Xdebug扩展
2. 修改php.ini配置
找到PHPStudy中的php.ini文件,在Xdebug部分修改或添加以下配置:
[Xdebug]
zend_extension=C:/phpstudy_pro/Extensions/php/php7.3.4nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.auto_trace=Off
xdebug.trace_output_dir=C:/phpstudy_pro/Extensions/php_log/php7.3.4nts.xdebug.trace
xdebug.profiler_enable=Off
xdebug.profiler_output_dir=C:/phpstudy_pro/Extensions/php_log/php7.3.4nts.xdebug.profiler
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_host=localhost
xdebug.remote_port=9003
xdebug.remote_handler=dbgp
关键参数说明:
remote_enable=1:启用远程调试remote_autostart=1:自动启动调试会话remote_port=9003:Xdebug监听端口(默认9003)
3. VSCode配置
-
安装PHP Debug插件
- 在VSCode扩展市场中搜索并安装"PHP Debug"插件
-
修改setting.json
- 打开VSCode设置(JSON格式)
- 添加或修改以下配置:
(路径应为PHPStudy中PHP的实际安装路径){ "php.debug.executablePath": "C:/phpstudy_pro/Extensions/php/php7.3.4nts/php.exe" }
-
创建launch.json
- 在VSCode中打开PHPStudy的www目录下的项目文件
- 系统会提示创建launch.json文件
- 确保端口配置与Xdebug配置一致(默认9003)
4. 开始调试
- 在VSCode中点击"运行和调试"的开始按钮
- 通过浏览器访问项目功能触发断点
- 注意:无需浏览器插件,直接访问即可
- 实测Burp Suite的Repeater模块也可触发断点
验证调试
当断点被触发时,VSCode将显示堆栈信息,证明Xdebug调试功能已正常工作。
注意事项
- 确保PHPStudy服务已启动
- 检查防火墙设置,确保调试端口未被阻止
- 路径配置需根据实际安装位置调整
- PHP版本(本例为7.3.4)可能需要根据实际情况调整
常见问题
-
断点不触发
- 检查Xdebug扩展是否已正确加载
- 验证php.ini配置是否正确
- 确保VSCode和Xdebug使用相同端口
-
性能问题
- 调试完成后建议关闭Xdebug
- 生产环境不应启用Xdebug