xDebug 无法使用 docker、vscode 和 WSL 2

xDebug not working using docker, vscode and WSL 2

我不确定是什么问题,它就是不起作用。

路由似乎有效,我的 nginx conf 文件中有一个服务器名称。例如test.com。行得通。

我的项目在 ubuntu 的根目录中,而不是在挂载文件夹中。

我不确定还能尝试什么。

xdebug.ini

[XDebug]
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so

xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9002
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_host=soapboxtest.com
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log

launch.json

{
  "name": "Listen for XDebug",
  "type": "php",
  "request": "launch",
  "port": 9002,
  "log": true,
  "externalConsole": false,
  "pathMappings": {
    "/var/www": "${workspaceRoot}"
  },
  "ignore": [
    "**/vendor/**/*.php"
    ]
  },

请求 cookie

"XDEBUG_SESSION" => "VSCODE"

Docker 文件

FROM php:fpm-alpine3.11
...
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
...

这也发生在我身上,发现 XDEBUG 没有在寻找 Docker 守护进程主机。通过添加修复它:

"hostname": "0.0.0.0"

作为 VS Code launch.json 常规选项的一部分。

我花了很多天“扯头发”..我用docker.

  1. 在防火墙中添加规则以允许流量(taken/quoted 来自:https://github.com/microsoft/WSL/issues/4171#issuecomment-559961027
    如果这还不够清楚,请去那里查看屏幕截图)
  1. In windows Start menu, type Firewall and choose Advanced settings, then Inbound Rules
  2. select custom rule.
  3. Click Next until you get to Protocol and Ports in the left menu
  4. Select ICMPv4 as protocol type.
  5. Select Next until you end up in the Name section, enter any name and click Finish.

If you now try pinging your host ip from wsl2, it should work as expected.

For every other connection between your wsl2 and your host, you have to allow the inbound rules for private and public networks or if they don't exists manually create the rules for the corresponding UDP/TCP port, but be aware that this might impact your security, if you use your computer in public networks.

  1. .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9900,
            "pathMappings": {
                "/linux/path/in/docker":"${workspaceFolder}",
            }
        },
    ]  
}
  1. Docker 文件
RUN echo "zend_extension=/usr/lib/php/20190902/xdebug.so\n \
         xdebug.remote_enable=on\n \
         xdebug.remote_autostart=on \n \
         xdebug.remote_connect_back=on\n \
#         xdebug.remote_host=$HOST_IP\n \
#         xdebug.remote_log_level=9\n \
#         xdebug.remote_log=/var/www/mywebdir.com/xdebug.log \n \
         xdebug.remote_port=9900" > /etc/php/7.4/mods-available/xdebug.ini;

编辑: 过了一会儿,我又开始遇到问题。试试这些:

  • 在docker文件

    中取消注释上面的日志行
  • 尝试对您的 windows IP 地址进行硬编码,例如 192.168。0.x

    (取消注释 xdebug.remote_host=... 并注释掉 xdebug.remote_autostart=on)

    我的 xdebug 似乎已连接但随后立即关闭了连接,因为它必须是我的 windows ip。我的理解是 VSCode 响应在 windows 中运行的 xdebug 请求,因此调试数据包需要通过 WSL2。

  • 尝试记录级别 10。如果您发现 xdebug 找不到断点的匹配项,请检查 docker 中的 webroot 映射和 [=61 中的 pathMappings =].json

  • 检查你是否设置了断点(愚蠢但是是的)

在一位同事帮助我配置 Xdebug 以从 VSCode 侦听 WSL 远程环境后,我查找了一个选项,当您单击下方的 WSL 远程图标时会打开该选项IDE 左侧:

远程 WSL:显示日志

它打开一个 WSL 终端,它显示(在几个信息之间)WSL 正在使用的 IP 地址。将此 WSL IP 添加到 xdebug.remote_host 值,唯一剩下的就是从你的 Docker 容器中正确获取 Xdebug 运行,在你的 WSL2 环境中(来自 WSL Remote VSCode 扩展),正在调整 launch.json 文件中的应用程序路径。

为此,用鼠标右键单击 launch.json 文件选项卡,搜索并单击“复制路径”选项,然后将其粘贴到 pathMappings 设置中,就像这个示例(不要不要忘记调整删除相对于 launch.json 本身的信息的路径,它适用于您的应用程序路径!):

"pathMappings": {
      "/application": "paste here the path you've copied"
 }

试试吧,对我来说就像一个魅力。如果它也适合你,请为我的答案投票!