Visual Studio 扩展 PHP 调试未连接

Visual Studio Extention PHP Debug not connecting

我安装了 visual studio 代码 1.41.1 和 PHP 调试版本 - 1.13.0.

https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug

我 运行 PHP Lamp 堆栈 Ubuntu,我的 PHP 信息显示“xdebug xdebug 支持已启用 - 版本 2.9.0”

我的PHP.ini配置如下:

[XDebug]
zend_extension = /usr/lib/php/20170718/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_handler = dbgp
;xdebug.remote_host = 192.168.1.103
xdebug.remote_port=9000
xdebug.remote_log=/var/log/xdebug.log

我的launch.json如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "hostname": "192.168.1.23"
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000,
            "hostname": "192.168.1.23"
        }
    ]
}

我的服务器 运行 位于 192.168.1.23,我的 ide 位于 192.168.1.103 运行。

当我尝试“侦听 XDebug”或“启动当前打开的脚本”时 我收到以下错误:

Error: listen EADDRNOTAVAIL: address not available 192.168.1.23:9000
    at Server.setupListenHandle [as _listen2] (net.js:1209:19)
    at listenInCluster (net.js:1274:12)
    at doListen (net.js:1413:7)
    at processTicksAndRejections (internal/process/task_queues.js:84:9) {
  code: 'EADDRNOTAVAIL',
  errno: 'EADDRNOTAVAIL',
  syscall: 'listen',
  address: '192.168.1.23',
  port: 9000
}

我尝试将 xdebug.remote_connect_back 设置为 0 和 1 并使用 xdebug.remote_host = 192.168.1.103 明确指定远程主机 IP 或也将其注释掉

错误几乎立即在 Visual Studio 中触发,这表明它在本地被阻止。

查看我的日志文件:xdebug.log 我得到:

[2268] Log opened at 2020-01-08 12:39:09
[2268] I: Connecting to configured address/port: 192.168.1.103:9000.
[2268] E: Time-out connecting to client (Waited: 200 ms). :-(
[2268] Log closed at 2020-01-08 12:39:09

[2225] Log opened at 2020-01-08 13:09:37
[2225] I: Checking remote connect back address.
[2225] I: Checking header 'HTTP_X_FORWARDED_FOR'.
[2225] I: Checking header 'REMOTE_ADDR'.
[2225] W: Remote address not found, connecting to configured address/port: localhost:9000. :$
[2225] W: Creating socket for 'localhost:9000', poll success, but error: Operation now in pr$
[2225] E: Could not connect to client. :-(
[2225] Log closed at 2020-01-08 13:09:37

日志文件似乎写得很慢——我不确定是哪个事件触发了日志中的事件。我怀疑它是直接从我的远程主机访问网页,而不是通过 Visual Studio 代码。

我还设置了我的 windows defender 防火墙在端口 9000 上打开。

根据评论进行编辑。 192.168.1.103是windows10机器运行visual studio代码的IP地址。机器也是运行 Version 6.0.8 r130520 (Qt5.6.2) 虚拟机的IP地址是192.168.1.23.

我已将超时更新为 2000 毫秒。

我观察到,如果我关闭虚拟机,VSC 会尝试连接并继续尝试连接更长时间。然后虚拟机启动,它立即出错。哪个提示虚拟机阻塞了端口?

最后 - 是什么触发进入 xdebug.log。只需在网站上加载 PHP 文件即可触发输出记录器吗?我问的原因是我无法识别触发错误,但在时间增量之间打开日志文件,即今天对比昨晚,出现了各种错误?

机器和虚拟机都受到基于路由器的防火墙的保护,因此这不会是外部流量。

虚拟机正按预期在 windows 10 客户端计算机上运行。只是无法连接调试器。但是 PHP/MYSQL/ APACHE 都是 运行 正确的。

看起来其他东西已经在监听端口 9000,因此,VS 无法打开相同的端口进行监听。这很可能是PHP-FPM.

要解决此问题,请在 php.ini 中设置以下内容:

xdebug.remote_port=9003

并将 VS 配置(两次)更改为同一端口:

"port": 9003,

问题与防火墙阻止传入连接有关。我通过以下步骤解决了这个问题:

安装 UFW -

sudo apt-get install ufw
sudo ufw enable
sudo ufw allow 9000/tcp

在我的 Php.ini 中,我使用以下设置:

[dDebug]
zend_extension = /usr/lib/php/20190902/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_port=9000

最后,我的发布 Json 具有以下内容:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/var/www/html/YourApp": "${workspaceRoot}/"
              }

        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

JSON 假设 work-space 指向源数据的根并且它没有隐藏在子文件夹中。

最后,您需要添加 UFW 规则以远程访问您的 Web 服务器,因为安装后 UFW 默认不存在这些规则

sudo ufw app list
sudo ufw allow 'Apache Full'

我还使用该方法从我的应用列表中添加了其他规则。

我希望这能帮助像我这样在新启动的 Ubuntu 18.04.4 VM 上努力连接到 xDebug 的人。