How to setup PHP server (nextcloud) or python server or node js on WSL2 inside Windows10 (Build 18980) with remote debugging on VS Code using Extensions.

For ex: PHP Debug 1.13.0 extension is the XDebug adapter inside VSCode

Intro

WSL2

Windows Subsystem for Linux is aimed to be a Linux (POSIX like runtime environment inside Windows10) In WSL1 it is a redirection of OS calls to underlying OS In WSL2 it is a Linux kernel , acting a VM inside hosting OS (W10) , localhost on Win10 redirects on the internal VNET address

Xdebug and VSCode

Please refer to this post on Nextcloud Debugging

Dynamic IP for WSL2 - Windows10 network bridge

https://docs.microsoft.com/en-us/windows/wsl/wsl2-ux-changes#accessing-windows-applications-from-linux

to connect to the Windows hosted (inside VS Code) XDebug server , PHP server (php-fpm10) must contact the hosting windows. The IP is the nameserver inside /etc/resolv.conf

gui@SAGIS-09:~$ cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.20.0.1

By default with a untouched php.ini setup , the log contains:

[11583] Log closed at 2019-09-17 14:54:46
[11583] E: Could not connect to client. :-(
[11583] W: Creating socket for '0.0.0.0:9009', poll success, but error: Operation now in progress (29).
[11583] I: Connecting to configured address/port: 0.0.0.0:9009.
[11583] Log opened at 2019-09-17 14:54:46

The solution is to configure php-fpm to get xdebug listening on bridge IP / windows side instead on localhost

gui@SAGIS-09:/$ sudo vi /etc/php/7.3/fpm/php.ini
...
[xdebug]
zend_extension="/usr/lib/php/20180731/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9009
xdebug.remote_host=172.20.0.1
xdebug.remote_log = /var/log/xdebug.log

Windows Defender and XDebug sockets

It could be mandatory to open firewall on windows 10

Xdebug could log this:

[11682] Log closed at 2019-09-17 14:57:03
[11682] E: Time-out connecting to client (Waited: 200 ms). :-(
[11678] I: Connecting to configured address/port: 172.20.0.1:9009.
[11678] Log opened at 2019-09-17 14:57:01

It is possible due to Firewall blocking VS Code , Xdebug server. So authorize program: code.exe to listen into Private

For other IDE , it could be time-saving to know what is the program:

Powershell command gives the Program listening on port 9009

Get-Process -Id (Get-NetTCPConnection -LocalPort 9009).OwningProcess

Dev workstation side (VS Code on windows10)

Because WSL2 is boxed, XDebug inside VS Code must listen on 0.0.0.0 instead of default localhost. https://docs.microsoft.com/en-us/windows/wsl/wsl2-ux-changes#other-networking-considerations Open the configurations on DEBUG / PHP (the launch.json file)

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "hostname": "0.0.0.0", 
            "port": 9009,
            // server -> local
            "pathMappings": 
            {
                "/var/www/html": "${workspaceRoot}/www",
//                "/app": "${workspaceRoot}/app",
                "/home/gui/nc": "${workspaceRoot}",
                "/apps": "${workspaceRoot}/apps",
                "/custom_apps": "${workspaceRoot}/custom_apps"                
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "hostname": "0.0.0.0", 
            "port": 9009
        }
    ]
}

Listening on the external public IP

PORT RELAY / PROXY of Windows 10 on WSL2 (internal VNET)

A server listening inside WLS2 could be accessed by localhost on Win10. https://localhost would bridge to the dynamic VNET IP address inside WSL2.

For being able to redirect external IP to this internal dynamic VNET IP address, Win10 provides Port Proxy :

Powershell:

netsh interface portproxy add v4tov4 listenaddress=<ip or dns> listenport=80 connectaddress=localhost connectport=80
netsh interface portproxy show all

Écouter sur ipv4 :             Connecter à ipv4 :

Adresse         Port        Adresse         Port
--------------- ----------  --------------- ----------
<ip or dns>             80          localhost       80

Remove the port proxy

netsh interface portproxy delete v4tov4 listenaddress=<ip> listenport=9000

more info: Docs from Microsoft