PhpStorm 和 Xdebug 配置

问题描述

我在使用 Xdebug 运行测试时遇到问题。 Xdebug 没有停止到断点,它说在我尝试 PHPStorm 后,在左下角“未建立连接无法使用 'Xdebug 2.8.1' 启动调试器会话

关于我的环境的一些信息:

  • PHP 7.4.20(我从自制软件安装了这个版本)
  • Xdebug 2.8.1

在我的 PHPinfo 中:

Runtime:       PHP 7.4.20 with Xdebug 2.8.1
Scan this dir for additional .ini files => /usr/local/etc/PHP/7.4/conf.d
Additional .ini files parsed => /usr/local/etc/PHP/7.4/conf.d/ext-opcache.ini,/usr/local/etc/PHP/7.4/conf.d/xdebug.ini

PHP API => 20190902
PHP Extension => 20190902
Zend Extension => 320190902
Zend Extension Build => API320190902,NTS
PHP Extension Build => API20190902,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => available,disabled

Registered PHP Streams => https,ftps,compress.zlib,compress.bzip2,PHP,file,glob,data,http,ftp,phar,zip
Registered Stream Socket Transports => tcp,udp,unix,udg,ssl,tls,tlsv1.0,tlsv1.1,tlsv1.2,tlsv1.3
Registered Stream Filters => zlib.*,bzip2.*,convert.iconv.*,string.rot13,string.toupper,string.tolower,string.strip_tags,convert.*,consumed,dechunk

This program makes use of the Zend Scripting Language Engine:
Zend Engine v3.4.0,copyright (c) Zend Technologies
    with Zend OPcache v7.4.20,copyright (c),by Zend Technologies
    with Xdebug v2.8.1,copyright (c) 2002-2019,by Derick Rethans

这是我在 PHPinfo 中找到的 xdebug 会话

xdebug

xdebug support => enabled
Version => 2.8.1
IDE Key => bpalumbo
Support Xdebug on Patreon,GitHub,or as a business: https://xdebug.org/support

Supported protocols
DBGp - Common DeBuGger Protocol

Directive => Local Value => Master Value
xdebug.auto_trace => Off => Off
xdebug.cli_color => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_includes => On => On
xdebug.collect_params => 0 => 0
xdebug.collect_return => Off => Off
xdebug.collect_vars => Off => Off
xdebug.coverage_enable => On => On
xdebug.default_enable => On => On
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.gc_stats_enable => Off => Off
xdebug.gc_stats_output_dir => /var/tmp/ => /var/tmp/
xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
xdebug.halt_level => 0 => 0
xdebug.idekey => no value => no value
xdebug.max_nesting_level => 256 => 256
xdebug.max_stack_frames => -1 => -1
xdebug.overload_var_dump => 2 => 2
xdebug.profiler_aggregate => Off => Off
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => Off => Off
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_enable_trigger_value => no value => no value
xdebug.profiler_output_dir => /var/tmp/ => /var/tmp/
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.remote_addr_header => no value => no value
xdebug.remote_autostart => Off => Off
xdebug.remote_connect_back => On => On
xdebug.remote_cookie_expire_time => 3600 => 3600
xdebug.remote_enable => On => On
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => /usr/local/Cellar/PHP@7.4/7.4.20/bin/xdebug.log => /usr/local/Cellar/PHP@7.4/7.4.20/bin/xdebug.log
xdebug.remote_log_level => 7 => 7
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.remote_timeout => 200 => 200
xdebug.scream => Off => Off
xdebug.show_error_trace => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_enable_trigger => Off => Off
xdebug.trace_enable_trigger_value => no value => no value
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => /var/tmp/ => /var/tmp/
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3

这里是 xdebug 日志

[5468] Log opened at 2021-06-11 14:26:14
[5468] I: Checking remote connect back address.
[5468] I: Checking header 'HTTP_X_FORWARDED_FOR'.
[5468] I: Checking header 'REMOTE_ADDR'.
[5468] W: Remote address not found,connecting to configured address/port: 127.0.0.1:9000. :-|
[5468] I: Connected to client. :-)
[5468] -> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" fileuri="file:///Users/bpalumbo/workspace/commercetools-PHP-sdk/vendor/PHPunit/PHPunit/PHPunit" language="PHP" xdebug:language_version="7.4.0" protocol_version="1.0" appid="5468" idekey="18793"><engine version="2.8.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[https://xdebug.org]]></url><copyright><![CDATA[copyright (c) 2002-2019 by Derick Rethans]]></copyright></init>

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

[5468] Log closed at 2021-06-11 14:26:15

如果我在这里运行 /usr/local/Cellar/PHP@7.4/7.4.20/bin PHPize 说(我不知道它是否已连接):

Cannot find config.m4.
Make sure that you run '/usr/local/opt/PHP@7.4/bin/PHPize' in the top level source directory of the module

在我的 PHP.ini 中,我没有添加任何关于 Xdebug 配置的内容,但是我可以找到 xdebug.ini

zend_extension=xdebug.so

我错过了什么?

解决方法

根据您的 Xdebug 日志,您需要将 Xdebug 端口从默认的 9000 更改为其他数字(例如 9003 - 在 Xdebug 3 中默认使用)。在 PHP(我猜是 xdebug.ini)和 PhpStorm 中执行此操作。

在您的计算机上,php-fpm 似乎已经在使用 TCP 9000 端口(它也默认使用该端口 + 在这种情况下您可以看到的典型响应)或其他一些服务知道 Xdebug 协议。

Homebrew 的意思是 Mac,对吧? PhpStorm 在 Mac 上检测已在使用的端口时存在问题(因此 IDE 不会发出警告:在 Windows 和 Linux 上,您会在这种情况下看到警告)。您可以使用 netstat 或类似程序(例如 Mac 上的 sudo lsof -nP -iTCP -sTCP:LISTEN)检查哪个进程使用该端口。