使用Xdebug进行远程调试会导致PHP脚本执行停止

问题描述

我正在尝试使用xdebug调试远程PHP脚本。为网站启用xdebug时,如果xdebug能够建立tcp连接,则PHP脚本将冻结,就像在等待执行断点一样。网页的JavaScript部分显示正确,只是等待PHP代码返回完成页面所需的数据。

这在过去一直有效。我无法一生找出原因。

如果xdebug无法与远程主机建立TCP会话,则一切正常。

我正在使用Angular 10作为我的开发环境,并使用PHPStorm作为我的IDE。

环境:

Debian bullseye (testing)
Apache2.4
PHP 7.4.11
mpm_prefork
xdebug.so 2.9.8

我也尝试过xdebug.so v2.8.x and v3.0.0 (Beta)无效。

当前的/etc/PHP/7.4/mods-available/xdebug.ini

#set xdebug flags/values
zend_extension=xdebug.so    

xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.remote_connect_back=0
xdebug.remote_host=MyClientIP    
xdebug.remote_port=9099
xdebug.idekey=PHPSTORM
    
#xdebug logger
xdebug.remote_log = /var/log/xdebug/xdebug.log

虽然我使用PHPStorm开发应用程序,但此问题与我的IDE无关。为了说明这一点,我从xdebug.org下载了dbgbClient.exe

使用脚本时,将在客户端和服务器之间创建会话。

客户端输出

dbgpClient.exe  -p 9099 -f
Xdebug Simple DBGp client (0.4.1)
copyright 2019-2020 by Derick Rethans

Waiting for debug server to connect on port 9099.
Connect from 10.130.189.1:39144
DBGp/1.0: Xdebug 2.9.8 — For PHP 7.4.11
Debugging file:///home/init/DEV/MT4/PHP-services/ClassAPI.PHP (ID: 70831/PHPSTORM)
1 | feature_get
1 | supports_async: supported > 0

xdebug.log来自服务器:

[70831] Log opened at 2020-10-14 20:59:24
[70831] I: Connecting to configured address/port: myClientIP:9099.
[70830] Log opened at 2020-10-14 20:59:24
[70830] I: Connecting to configured address/port: MyClientIP:9099.
[70831] I: Connected to client. :-)
[70831] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///home/init/DEV/MT4/PHP-services/ClassAPI.PHP" language="PHP" xdebug:language_version="7.4.11" protocol_version="1.0" appid="70831" idekey="PHPSTORM"><engine version="2.9.8"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>

[70830] I: Connected to client. :-)
[70830] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///home/init/DEV/MT4/PHP-services/ClassAPI.PHP" language="PHP" xdebug:language_version="7.4.11" protocol_version="1.0" appid="70830" idekey="PHPSTORM"><engine version="2.9.8"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[copyright (c) 2002-2020 by Derick Rethans]]></copyright></init>

此时,将创建网页的开头。

如果我发出一个run命令和一个或两个额外的回车符,则调试会话将继续并且屏幕结束。

客户示例:

(cmd) run
2 | run > stopping/ok

(cmd)
Error while handling connection: Error reading length: EOF
disconnect
Connect from 10.130.189.1:39146
DBGp/1.0: Xdebug 2.9.8 — For PHP 7.4.11
Debugging file:///home/init/DEV/MT4/PHP-services/ClassAPI.PHP (ID: 70830/PHPSTORM)
1 | feature_get
1 | supports_async: supported > 0

(cmd) run
2 | run > stopping/ok

(cmd)
Error while handling connection: Error reading length: EOF
disconnect

上述的xdebug日志:

[70831] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_get" transaction_id="1" feature_name="supports_async" supported="1"><![CDATA[0]]></response>

[70831] <- run -i 2
[70831] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="2" status="stopping" reason="ok"></response>

[70831] <- run -i 3
[70831] Log closed at 2020-10-14 21:00:32

[70830] <- feature_get -i 1 -n supports_async
[70830] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_get" transaction_id="1" feature_name="supports_async" supported="1"><![CDATA[0]]></response>

[70830] <- run -i 2
[70830] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="run" transaction_id="2" status="stopping" reason="ok"></response>

[70830] <- run -i 3
[70830] Log closed at 2020-10-14 21:00:41

[70829] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="4" status="stopping" reason="ok"></response>

[70829] Log closed at 2020-10-14 21:13:17

[70832] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="4" status="stopping" reason="ok"></response>

[70832] Log closed at 2020-10-14 21:13:38

感谢您阅读本文。我确定那里有人可以解决

解决方法

这是相关部分:

处理连接时出错:读取长度错误: EOF 断开连接

首先,

xdebug并不意味着要连接到远程(活动)站点。

即使stunnel工作正常,由于某些原因,它仍然可能无法连接回去...例如

xdebug.remote_host=EXTERNAL_ROUTER_IP
xdebug.remote_connect_back=1

然后将端口:9099转发到路由器中的内部MyClientIP

,

好吧,事实证明,我的开发PC之间通过VPN连接到公司网络之间存在某些问题。我一次都工作。感谢IT部门拧紧安全螺钉(firewall rules)。

尽管我的PhpStorm会看到PHPSTORM cookie并在正确的端口上建立TCP会话,但是该协议不会继续完成。我猜想网络上是否存在智能防火墙。

秘密是像这样:在我的PC和linux服务器之间建立ssh隧道:

ssh -R 9000:localhost:9000 username_goes_here@hostname_goes_here

我也将/etc/php/7.4/mods-available/xdebug.ini修改为:(魔术酱是xdebug.remote_host=127.0.0.1

zend_extension=xdebug.so    
xdebug.remote_log=/var/log/xdebug/xdebug.log    
xdebug.remote_host=127.0.0.1
xdebug.remote_enable=1
xdebug.idekey=PHPSTORM

因此ssh隧道将连接到服务器上的端口9000,而PC上的本地端口9000是隧道的另一端。

那么所有要做的就是设置PhpStorm。为此(以及这些说明的起源),请参见here

仅需注意-我仅通过网络浏览器测试了php脚本访问。我尚未测试过基于CLI的PHP脚本,但我猜答案大致相同。

如果您需要有关ssh端口转发(隧道)的更多信息,请参见here