phpstrom debug 无法连接
问题标题】:Xdebug: [Step Debug] Could not connect to debugging clientXdebug:[Step Debug] 无法连接到调试客户端 【发布时间】:2021-02-28 20:28:45 【问题描述】:
我想尝试 Xdebug 3.0.0RC1 来探索发生了什么变化以及随之而来的新功能。我还在使用最新的 PhpStorm 2020.3 EAP,它支持 Xdebug 3,无需主要配置。下面是我对调试器的 PhpStorm 配置:
这是我为 Xdebug3 尝试过的配置:
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=host.docker.internal # here I tried several combinations like: "localhost", "127.0.0.1", "172.17.0.1"
xdebug.client_port=9001 # here I tried several ports 9003 included with no success
2
3
4
5
我也尝试过不添加 client_host/client_port
设置,但仍然失败。
我收到此错误:
Script php bin/console doctrine:cache:clear-metadata returned with error code 255
!! [17-Nov-2020 15:24:40 UTC] Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9001 (through xdebug.client_host/xdebug.client_port) :-(
!! [17-Nov-2020 15:24:41 UTC] PHP Fatal error: Method class@anonymous::__toString() must not throw an exception, caught Symfony\Component\DependencyInjection\Exception\AutowiringFailedException: in /var/www/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php on line 233
2
3
关于我的环境的一些信息:
- Fedora 33
- Docker 版本 19.03.13,构建 4484c46d9d
- PhpStorm 2020.3 EAP 构建 #PS-203.5784.36
这很奇怪(因为显然host.docker.internal
是我正在使用的 Docker 版本“不”支持的,但它可以工作)而且很奇怪,同时以下配置确实适用于 Xdebug 2,即使调试器正在监听一直传入连接:
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.remote_host=host.docker.internal
xdebug.remote_port=9000
2
3
4
5
我在这里缺少什么?
注意:我已经应用了Xdebug开发者here (opens new window)提供的解决方案。
【问题讨论】:
- 你的操作系统是什么?
host.docker.internal
在 Linux 上不受支持——仅 Windows 和 Mac——github.com/docker/for-linux/issues/264 (opens new window)。自 Docker v20 -- github.com/docker/for-linux/issues/264#issuecomment-714253414 (opens new window) 起将支持它。如果您想使用该主机名,则需要动态检测 IP 地址(上述链接有很多关于如何实现这一点的选项)。- @ReynierPM 您可能有其他选项(例如
xdebug.remote_connect_back = 1
-- Xdebug 2 相当于 Xdebug 的 3xdebug.discover_client_host = true
)。使用显式 IP 地址(硬编码).. 或动态检测。 - @LazyOne 它也不起作用 😦
Xdebug: [Step Debug] Could not connect to debugging client. Tried: 10.211.55.12:9001
- 刚刚花了整晚的时间在这上面工作。原来ufw已启用并且它阻塞了端口。
ufw allow 9003
已修复此问题,但可能应该通过 IP/设备将其锁定
标签: php (opens new window) phpstorm (opens new window) xdebug (opens new window)
【解决方案1】:
PHP 7.4 码头工人 PHPStorm 2020.1 Xdebug 3.1.0
使用 Dockerfile 在你的 docker 容器中安装 Xdebug
RUN pecl install xdebug-3.0.1 && docker-php-ext-enable xdebug
使用以下配置 php.ini:
[xdebug]
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = 1
2
3
4
转到 PHPStorm - 设置 - PHP - 调试 - Xdebug 并将端口设置为 9003(默认)
就是这样(:
如果您只想在需要时启用/禁用调试器:只需安装一个名为“Xdebug helper”的浏览器扩展,选择“调试”并从 php.ini 中删除“xdebug.start_with_request = yes”
[xdebug]
xdebug.mode = debug
xdebug.discover_client_host = 1
2
3
【讨论】:
- 我试过这个确切的配置,但我仍然得到
Could not connect to debugging client. Tried: 172.19.0.1:9003 (from REMOTE_ADDR HTTP header)
- 添加
xdebug.discover_client_host = 1
并仔细检查我的端口是解决方案,谢谢!
【解决方案2】:
对我有用的是将 start_with_request
从 yes 更改为 trigger
。
这对我有用:
xdebug.mode=debug
xdebug.start_with_request=trigger
xdebug.client_port=9003
2
3
【讨论】:
- 注意
xdebug.start_with_request
在xdebug.mode=debug
时自动设置为trigger
,所以不需要定义。xdebug.client_port=9003
也是 Xdebug 3 的默认值,因此也不需要。参考:xdebug.org/docs/all_settings#start_with_request (opens new window)xdebug.org/docs/all_settings#client_port (opens new window)
【解决方案3】:
我将开始非常感谢@LazyOne,他花了一些时间帮助我完成这个,直到我们让它工作。以下是我目前的配置,它运行良好:
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
xdebug.mode=debug
xdebug.client_port=9005
2
3
您还需要更新File | Settings | Languages & Frameworks | PHP | Servers
处的Xdebug 端口以反映新端口,但还需要启用监听Xdebug3 传入连接的选项。 (我相信它在 PhpStorm 2020.3 中默认启用)
这是一个后端项目的设置,中间没有浏览器,我没有尝试过,但对于那些,你可能需要:
xdebug.start_with_request=yes
还有File | Settings | Languages & Frameworks | PHP | Servers
配置好。
注意:我们发现主机启用了 IPv6,我禁用了它,此外,通过
Help > Edit Custom VM options
将以下设置添加到 IDE:-Djava.net.preferIPv4Stack=true
。将 IP4 设置添加到 IDE 后,我没有尝试重新启用 IPv6 并查看 Xdebug 3 是否仍在工作
【讨论】:
- 那么问题出在哪里? (除了不同的端口)?我的意思是它看起来和有问题的一样,今天早些时候删除主机也没有用 iirc
- @FaizanAkramDar 我想问题确实是主机。我今天尝试的只是使用它,但最近的尝试是没有它并且它有效。我可以恢复 IPv6,看看这是否也会导致任何问题。
【解决方案4】:
PHP 7.3 Docker(适用于 Mac) PhpStorm 2021.1
您可能不需要通过 PECL 安装它(这需要很长时间才能构建并且对我不起作用)。
我所做的只是将php7.3-xdebug
添加到我的apt-get install
命令并正确配置端口映射,如下所示:
在 Dockerfile 中添加: RUN apt-get install -y php7.3-xdebug
在 docker-compose.yml 中,我映射了一个 extra_host
(这是我的秘诀):
services:
web:
extra_hosts:
- "host.docker.internal:host-gateway"
2
3
4
在 php.ini 中:
[xdebug]
xdebug.mode=debug
xdebug.client_host=host.docker.internal
;optionals: (uncomment if you need them)
;xdebug.start_with_request=yes
;xdebug.discover_client_host=1
2
3
4
5
6
7
在 PhpStorm 中,我刚刚开始监听 9003 端口并根据需要配置服务器映射。
参考资料:
- XDebug 3 Documentation (opens new window)
- Milos Devacic post about xdebug + docker + vscode (opens new window)(帮助我找到了 extra_host 参数)
【讨论】:
- 我认为这里不需要
extra_hosts
。可能一次需要该映射(使用 linux 主机)。你为什么添加它?您的主机操作系统是什么? - @marcguyer 我正在使用 MacOs。
host.docker.internal
DNS 是在 macOS 上自动设置的,所以不需要这行。
【解决方案5】:
我创建了一个非常简单的配置,让我可以轻松地将Xdebug
与任何 PHP 版本一起使用*(v2:5.6-7.1,v3:7.2+)*。我只需要在三个地方配置 PhpStorm 和 docker-compose.yml
就可以调试了。
# 配置:
gander/dev (opens new window)@xdebug2.ini (opens new window):
zend_extension=xdebug.so
; https://2.xdebug.org/docs/all_settings
; ------------------------------------
; Enables Step Debugging
xdebug.remote_enable=1
; ------------------------------------
; Address where IDE listening for incoming debugging connections
xdebug.remote_host=host.docker.internal
; ------------------------------------
; Port where IDE listening for incoming debugging connections
xdebug.remote_port=9003
; ------------------------------------
; Color var_dumps when in CLI
xdebug.cli_color=1
; ------------------------------------
2
3
4
5
6
7
8
9
10
11
12
13
14
15
gander/dev (opens new window)@xdebug3.ini (opens new window):
zend_extension=xdebug.so
; https://xdebug.org/docs/all_settings
; ------------------------------------
; Enables Step Debugging
xdebug.mode=debug,develop
; ------------------------------------
; Address where IDE listening for incoming debugging connections
xdebug.client_host=host.docker.internal
; ------------------------------------
; Port where IDE listening for incoming debugging connections
xdebug.client_port=9003
; ------------------------------------
; Color var_dumps when in CLI
xdebug.cli_color=1
; ------------------------------------
2
3
4
5
6
7
8
9
10
11
12
13
14
15
gander/dev (opens new window)@docker-compose.yml
(opens new window):
version: '3.7'
services:
#...
dev74:
hostname: 'dev-74'
container_name: 'dev_74'
image: 'gander/dev:7.4'
volumes:
- './app/xdebug3:/www/localhost'
working_dir: '/www/localhost/public'
ports:
- '8074:80'
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
PHP_IDE_CONFIG: "serverName=dev.74"
#...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 通过 CLI 运行:
XDEBUG_SESSION=1 XDEBUG_CONFIG=1 php script.php
或:
docker-compose exec dev74 bash -c 'XDEBUG_SESSION=1 XDEBUG_CONFIG=1 php index.php'
# 截图:
【讨论】:
【解决方案6】:
我有同样的问题。从浏览器发出请求时我仍然有它,但是从命令行,就像你的情况一样,它现在可以工作了。我缺少的是带有路径映射的服务器配置。一旦设置好,连同你已有的设置,它就可以工作了。我在 Mac Os Big Sur 上,使用 PhpStorm 2020.3
【讨论】:
【解决方案7】:
我想指出ini文件中的以下配置选项,因为这对我来说解决了同样的问题。
xdebug.client_host=host.docker.internal
在 PhpStorm 的文档中 here (opens new window) 说 xdebug.remote_host=host.docker.internal
必须配置哪些接缝相似,但不是。
略有不同的是我使用的是 IntelliJ。
【讨论】:
remote_host
用于 Xdebug 2,client_host
用于 Xdebug 3。
【解决方案8】:
我遇到了同样的问题(ubuntu 20.4 - docker 20.10 - xdebug 3)
我的解决方案是:
xdebug.discover_client_host=1
- 禁用防火墙
sudo ufw disable
【讨论】:
- 完全禁用防火墙永远无法解决