On Windows 11, I use PhpStorm. The PHP script is executed through a Ubuntu instance via the WSL (Windows Subsystem for Linux) within PhpStorm.
I use php8.3 and xdebug-3. Xdebug seems loaded as:
var_dump(extension_loaded('xdebug'));
prints true
within my script.
Normal script execution works fine, but when I try to debug, I get the following error:
Xdebug: [Step Debug] Could not connect to debugging client. Tried: 127.0.0.1:9003 (through xdebug.client_host/xdebug.client_port).
I assume that the issue is connecting to the WSL via the IP address and that PhpStorm is unable to connect via the 127.0.0.1 IP, but that it rather must use the WSL IP (in my case 172.27.48.1:9003
). I don’t know how to change how the command is generated.
This command PhpStorm generates looks like this:
[\wsl$Ubuntu-22.04]:/usr/bin/php --distribution Ubuntu-22.04 --exec /bin/bash -l -c "export TERM=xterm && export XDEBUG_CONFIG=idekey=17964 && export IDE_PHPUNIT_CUSTOM_LOADER=/mnt/c/Users/Philipp/Documents/GitHub/my-project/vendor/autoload.php && cd /mnt/c/Users/Philipp/Documents/GitHub/my-project && /usr/bin/php -dxdebug.mode=debug -dxdebug.client_port=9003 -dxdebug.client_host=127.0.0.1 /mnt/c/Users/my-project/vendor/phpunit/phpunit/phpunit --no-configuration /mnt/c/Users/my-project/tests --teamcity --cache-result-file=/mnt/c/Users/my-project/.phpunit.result.cache"
In the /etc/php/8.3/cli/conf.d/20-xdebug.ini
to the to the proper WSL IP, yet, PhpStorm seems to ignore that value even though it says it reads the config from the WSL instance:
zend_extension=xdebug.so
xdebug.client_host=172.27.48.1
The generated command stays the same.
How do I connect to Xdebug on WSL via PhpStorm debug session?
Here’s my Windows 11 PC ipconfig:
ipconfig
Windows IP Configuration
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : fritz.box
IPv4 Address. . . . . . . . . . . : 192.168.178.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.178.1
Ethernet adapter vEthernet (Default Switch):
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::33b7:301b:55d3:45ff%32
IPv4 Address. . . . . . . . . . . : 172.23.240.1
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . :
Ethernet adapter vEthernet (WSL (Hyper-V firewall)):
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::a5b:7f00:1e2d:30d6%62
IPv4 Address. . . . . . . . . . . : 172.27.48.1
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . :
2
Answers
In PhpStorm, at
disable the flag:
Now, these values are read from the
php.ini
/20-xdebug.ini
within your WSL instance.Add this in your relevant
php.ini
or20-xdebug.ini
, e.g.:/etc/php/8.3/cli/conf.d/20-xdebug.ini
:The
xdebug://gateway
makes applying a dynamic and hence prone to change IP obsolete, it requires [email protected] though.As the console output reads, you’re connecting a firewall:
And there is a documentation for that:
Configure Hyper-V firewall with PowerShell.
To obtain the WSL GUID:
To list the rules:
Allowing an inbound xdebug connection on
:9003
might look about like this:Or to allow all inbound traffic:
Also see:
Get-NetFirewallHyperVVMCreator
New-NetFirewallHyperVRule
Set-NetFirewallHyperVVMSetting