无法使PhpStorm连接到在WSL 2中运行的Xdebug

问题描述

我无法通过以下设置使PHPStorm 2020.2与xdebug一起使用: 运行Ubuntu 20的WSL2,安装了PHPPHP-xdebug。每当我在启用调试的情况下运行测试时,都会显示“未建立连接。”。

这是Xdebug日志显示内容

[5877] Log opened at 2020-08-14 17:20:45
[5877] I: Connecting to configured address/port: 172.18.48.1:9006.
[5877] E: Time-out connecting to client (Waited: 200 ms). :-(
[5877] Log closed at 2020-08-14 17:20:46

这仅适用于CLI ,我没有尝试通过PHP-fpm进行连接(因此没有Apache,Nginx等)。

到目前为止,我已经尝试过:

  • 将端口更改为9000以外的端口(端口扫描还显示它们均未使用)。

  • 按照https://github.com/microsoft/WSL/issues/4585#issuecomment-610061194中的说明添加防火墙规则以允许传入请求(以及在添加规则后重新启动几次)

    New-NetFirewallRule -displayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow
    
  • 向配置中添加诸如xdebug.remote_enable=1之类的选项,但这是由PHPStorm在运行远程PHP实例时进行的(remote_connect_back也没有做到):

    [wsl://UBUNTU2004]:PHP -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9006 -dxdebug.remote_host=172.18.48.1 /home/spaceemotion/code/xxxxxx/backend/vendor/PHPunit/PHPunit/PHPunit [rest ommitted]
    

任何帮助将不胜感激!

解决方法

按照:https://intellij-support.jetbrains.com/hc/en-us/community/posts/360000436444/comments/360000168070

在Windows“控制面板”>“ Windows防火墙”>“允许通过Windows防火墙的程序或功能”中,您可以看到允许通过专用和公用网络进行通信的软件列表。

确保实际上已允许phpstorm.exe(对我而言,未选中专用网络选项)。