使用Docker和WSL2在VSCode中调试PHP时的问题 [英] Issues when Debugging PHP in VSCode using Docker and WSL2

查看:579
本文介绍了使用Docker和WSL2在VSCode中调试PHP时的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows中使用VSCode + Docker多年了,并且设法拥有一个可以正常工作的开发环境而没有任何问题。

I've been working with VSCode + Docker in Windows for some years now, and managed to have a fully working dev environment without any issues.

最近我建立了一个新的开发环境与WSL2。使用带有WSL2容器的Docker Windows和带有远程连接到WSL的Windows上的VSCode,将我所有的项目,库,CLI等移至WSL。一切工作都非常顺利,我喜欢可以将所有东西分开的事实。

Recently i setup a new development environment with WSL2. Moved all my projects, libraries, CLIs, etc, into WSL, using Docker Windows with WSL2 containers and VSCode on Windows with remote connection to WSL. Everything is working very smoothly and i like the fact i can have everything separated.

但是最近我遇到了一个我无法解决的问题,我失去了调试PHP文件的能力。
我正在使用VSCode远程WSL扩展来在WSL中处理我的项目,但是当我尝试调试时,什么也没发生。

But recently i came across an issue that i'm unable to solve, i lost the ability to debug PHP files. I'm using VSCode Remote WSL extension to work on my projects inside WSL, but when i try to debug, nothing happens.

我在VSCode中有树调试设置,用于我使用的每个开发环境(Windows,MacOS和WSL)。除WSL之外的所有工作。当我尝试使用WSL进行调试时,实际上什么也没有发生,没有输出错误,没有调试控制台信息,什么都没有...

I have tree debugging settings in my VSCode for each dev environment that i use (Windows, MacOS and WSL). All work except for the WSL. When i try to debug with WSL, literally nothing happens, no output erros, no debug console information, nothing...

这是我的VSCode调试设置:

Here are my VSCode debug settings:

{
    "version": "0.2.0",
    "configurations": [{
            "name": "Listen for XDebug Win10",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true,
            "externalConsole": false,
            "pathMappings": {
                "/var/www/project-a/api": "\\\\wsl$\\Ubuntu\\home\\ubuntu\\PROJECTS\\project-a\\api",
            },
            "ignore": [
                "**/vendor/**/*.php"
            ]
        },
        {
            "name": "Listen for XDebug MacOS",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true,
            "externalConsole": false,
            "pathMappings": {
                "/var/www/project-a/api": "/Users/ricky/PROJECTS/project-a/api",
            },
            "ignore": [
                "**/vendor/**/*.php"
            ]
        },
        {
            "name": "Listen for XDebug WSL",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "log": true,
            "externalConsole": false,
            "pathMappings": {
                "/var/www/project-a/api": "/home/ubuntu/PROJECTS/project-a/api",
            },
            "ignore": [
                "**/vendor/**/*.php"
            ]
        },
    ]
}

我在做什么错误?有关如何解决此问题的任何想法?

What am i doing wrong? Any ideas on how to solve this issue?

###更新::我已将原来的正确答案更改为一个新的答案。尽管@ romain-prevost的解决方案有效,但我认为@dark的方法要简单得多:)

### UPDATE: I've changed the original right answer to a new one. Although @romain-prevost's solution worked, I think @dark's approach is wayyy much simpler :)

推荐答案

忘记其他答案了。它们正在工作,但在我看来太复杂了。

Forget about the other answers. They are working but too complicated in my opinion.

问题是,您无法连接到xdebug。

The problem is, that you can't connect to xdebug.

解决方案是告诉xdebug将remote_host设置为 host.docker.internal 。从那里的所有内容都可用于localhost。现在,您只需要在Visual Studio代码中通过主机名监听 localhost

The solution is to tell xdebug to set remote_host to host.docker.internal. Everything from there is available to localhost. Now you only have to listen inside Visual Studio Code to localhost via hostname.

Evoilà。
现在,您可以调试浏览器,在phpunit测试中或在命令行脚本中调用的内容。

Et voilà. Now you can debug things invoked by your browser, inside your phpunit tests or within your commandline scripts.

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/var/www/html/": "${workspaceRoot}"
            },
            "hostname": "localhost"
        }
    ]
}

php.ini

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9000

这篇关于使用Docker和WSL2在VSCode中调试PHP时的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆