工欲善其事必先利其器,xdebug是php开发的一个杀手级利器,vagrant是一款非常便捷的虚拟机管理工具,两者搭配有时会出现一些奇怪的问题,近期在重新配置开发环境时,出现xdebug连不上的异常情况,花了些时间才找到问题并解决,记录下解决问题的过程,希望能帮助到一些小伙伴
问题及解决过程
异常情况
xdebug配置后,客户端多次请求,vscode均无任何反应。检查服务端、客户端配置,确认没有差错;监测网络数据,只有初次请求数据,无结果返回
客户端/服务器配置信息
服务器是vagrant + ubuntu 18.04 + PHP 7.4,客户端是Windows 10,编辑器是vscode
初始xdebug配置如下
1 2 3 4 5 6 7 8
| [xdebug] xdebug.remote_enable = 1 xdebug.remote_autostart = 1 xdebug.remote_port = 9001 xdebug.remote_handler = "dbgp" xdebug.remote_mode = req xdebug.remote_connect_back = 1 xdebug.overload_var_dump=0
|
vscode配置launch.json如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "version": "0.2.0", "configurations": [ { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9001, "pathMappings": { "/data/example": "D:\\data\\example" }, "log": true } ] }
|
开启xdebug日志监测
1
| xdebug.remote_log=/tmp/xdebug.log
|
再次请求,获取日志数据如下
1 2 3 4 5 6 7
| [140114918627520] Log opened at 2019-09-04 11:49:00 [140114918627520] I: Checking remote connect back address. [140114918627520] I: Checking header 'HTTP_X_FORWARDED_FOR'. [140114918627520] I: Checking header 'REMOTE_ADDR'. [140114918627520] I: Remote address found, connecting to 192.168.33.1:9001. [140114918627520] E: Time-out connecting to client (Waited: 200 ms). :-( [140114918627520] Log closed at 2019-09-04 11:49:00
|
日志显示服务端有正常向客户端9001端口发起连接,但客户端无响应导致超时,检查客户端防火墙无异常
最后,在谷歌上找到一个类似的问题,发现问题与vagrant有关,具体问题描述见参考文档链接
解决方法
1 2 3
|
xdebug.remote_host=10.0.2.2
|
重启php-fpm,再次测试,vscode可连接xdebug,问题解决
参考文档
How to configure Xdebug in PhpStorm through Vagrant
xdebug Time-out connecting to client