vscode调试php(解决vscode远程调试无效的问题)

1,246 阅读3分钟

vscode调试单个文件正常,就是无法远程调试(比如通过浏览器运行时调试),折腾了一整天,最后发现是php.ini 中少了两项配置所导致:

xdebug.remote_enable=1

这个开关控制Xdebug是否应该尝试联系一个正在监听主机和端口的调试客户端,这些主机和端口是用xdebug.remote_host和xdebug.remote_port设置的。如果不能建立连接,脚本将继续进行,就像这个设置为0一样。

xdebug.remote_autostart=1

通常,您需要使用特定的 HTTP GET/POST 变量来启动远程调试,当此设置设置为 1 时,Xdebug 将始终尝试启动远程调试会话并尝试连接到客户端,即使 GET/POST/COOKIE 变量不存在。

vscode配置xdebug步骤

下载xdebug

1. 查看PHP版本 

image.png

2. 查找对应的xdebug 

image.png

3. 放到php更目录下的ext目录下 

  image.png

4. 或者通过phpstudy配置xdebug 

image.png

image.png

注意:phpstudy配置完成后查看php.ini,查找xdebug.remote_autostart =1,如果没有,就在xdebug模块追加,修改xdebug.remote_enable 等于 1 或者 On,修改debug.trace_output_dir和 xdebug.profiler_output_dir的路径

5. 配置php.ini****

1. 配置php.ini文件

1. [XDebug]

2.  zend_extension=E:/phpstudy_pro/Extensions/php/php7.4.3nts/ext/php_xdebug.dll    ;xdebug的路径

3.  xdebug.collect_params=1

4.  xdebug.collect_return=1 ;此设置默认为 0,控制 Xdebug 是否应将函数调用的返回值写入跟踪文件。

5.  xdebug.auto_trace=On ;当这个设置为开时,函数调用的追踪将在脚本运行之前被启用

6.  xdebug.trace_output_dir=E:/phpstudy_pro/Extensions/php/log/php/trace

7.  xdebug.profiler_enable=On

8.  xdebug.profiler_output_dir=E:/phpstudy_pro/Extensions/php/log/profiler

9.  ;远程调试配置

10.  xdebug.remote_enable=1  ;开启远程调试功能

11.  xdebug.remote_host=localhost

12.  xdebug.remote_port=9001  ;默认端口9000  和 php冲突

13.  xdebug.remote_handler=dbgp

14.  xdebug.remote_autostart =1

2. 配置vscode

3. vscode 下载PHP Debug,修改配置信息

1. {

2.   *// 使用 IntelliSense 了解相关属性。*

3.   *// 悬停以查看现有属性的描述。*

4.   *// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387*

5.   *// 修改端口号和php.ini里一直*

6.  "version": "0.2.0",

7.  "configurations": [

8.      {

9.          "name": "Listen for Xdebug",

10.          "type": "php",

11.          "request": "launch",

12.          "port": 9001      

13.      },

14.      {

15.          "name": "Launch currently open script",

16.          "type": "php",

17.          "request": "launch",

18.          "program": "${file}",

19.          "cwd": "${fileDirname}",

20.          "port": 0,

21.          "runtimeArgs": [

22.              "-dxdebug.start_with_request=yes"

23.          ],

24.          "env": {

25.              "XDEBUG_MODE": "debug,develop",

26.              "XDEBUG_CONFIG": "client_port=${port}"

27.          }

28.      },

29.      {

30.          "name": "Launch Built-in web server",

31.          "type": "php",

32.          "request": "launch",

33.          "runtimeArgs": [

34.              "-dxdebug.mode=debug",

35.              "-dxdebug.start_with_request=yes",

36.              "-S",

37.              "localhost:0"

38.          ],

39.          "program": "",

40.          "cwd": "${workspaceRoot}",

41.          "port": 9001,

42.          "serverReadyAction": {

43.              "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",

44.              "uriFormat": "http://localhost:%s",

45.              "action": "openExternally"

46.          }

47.      }

48.  ]

49. }

4. 测试vscode调试(直接上图)

1. 选择调试模式为Listen for Xdebug,打好断点 

image.png

2. 打开浏览器,进入编写好的程序 

image.png

3. 打开调试,然后刷新页面,就可以进入断点 

image.png

问题一:

倒腾了一整天,nginx使用xdebug可以连接,就是不能进入断点调试,下面是xdebug返回的日志信息。

1. [11592] Log opened at 2021-08-14 09:59:12

2. [11592] I: Connecting to configured address/port: 127.0.0.1:9001.

3. [11592] I: Connected to client. :-)

4. [11592] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///E:/phpstudy_pro/WWW/classOverNow/public/index.php" language="PHP" xdebug:language_version="7.4.0" protocol_version="1.0" appid="11592" idekey="Administrator"><engine version="2.9.4"><![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>

5. 

6. [11592] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

7. 

8. [11592] Log closed at 2021-08-14 09:59:16

解决办法:

无意间发现phpstudy的fastcgi的默认端口号为9001,和我设置的xdebug的端口号重复了,反复了好几遍,apache能进去,nginx进不去。到最后发现是是个乌龙,所以再次建议,这样的端口号尽可能的整大一点。

问题二:(日志报以下错误)

Time-out connecting to client (Waited: 200 ms)

解决办法:

连接超时: 先去看下端口号,vscode每个项目下面都有个.vscode文件夹,launch.json这个文件就是PHP Debug的配置文件,也就是每个项目可以单独配置PHP Debug

以上就是CRMEB介绍的关于vscode调试php(解决vscode远程调试无效的问题)的所有内容,有不懂的地方可以在下方留言一起交流学习!