Configure Xdebug + PHPstorm on wsl2

Posted date: Jun 24, 2022

phpstorm wsl2 xdebug

First of all, we need the wsl2 IP address. To find out what’s the IP open Windows PowerShell and run ipconfig
Under the Ethernet adapter, vEthernet (WSL) look for the IPv4 Address

Go to wsl2 and open your php.ini
e.g sudo nano /etc/php/7.x/apache2/php.ini
And paste these configurations

zend_extension=”xdebug.so”
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.client_host=paste-wsl-ip-address
xdebug.client_port=9001
xdebug.mode=debug,develop,profile
xdebug.cli_color=1
xdebug.start_with_request=trigger
xdebug.log_level=3
xdebug.idekey=PHPSTORM
xdebug.discover_client_host=false
xdebug.max_nesting_level=256
xdebug.log = /var/www/html/xdebug.log
xdebug.profiler_enable=On

Note: These settings work for xdebug 3. If you are on xdebug 2 then use xdebug.remote_host and xdebug.remote_port instead.

Restart your webserver.

PHPstorm settings

Press ctrl+alt+s to open the settings tab look for PHP menu item on the left and set the
CLI Interpreter
Click in the three dots and add a new interpreter from WSL

Expand PHP menu item open Debug and set Debug port to 9001
Click also Break at first line in PHP scripts

Close the settings tab and click to start listening for connections

Open your browser and load your local site
Now your PhpStorm should listen for external connections and you are ready to debug.