Most examples on the web for getting PHP debugging working with VSCode and WSL use Xdebug 2.x php.ini settings. Those no longer work with version 3.0. See the Xdebug Upgrade for details on changes.
The following worked for me on a PHP project with a single file just to test debugging. Using Ubuntu 20.04, WSL2, Xdebug 3.02 with the VSCode extensions Remote WSL and PHP Debug by Felix Becker.
I had to modify both /etc/php/7.3/apache2/php.ini
and /etc/php/7.3/cli/php.ini
on my system. Hope this works for you folks.
php.ini
[xdebug]
zend_extension = ./lib/php/20180731/xdebug.so
xdebug.start_with_request = trigger
xdebug.mode = debug
xdebug.discover_client_host = 1
xdebug.log = /tmp/xdebug_remote.log
xdebug.client_port = 9003
launch.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"stopOnEntry": true,
"log": true,
"pathMappings":
{
"/var/www/html/test": "${workspaceRoot}"
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9003
}
]
2
Answers
Update for PHP 8.1. Needed to change the full path to the xdebug.so file. zend_extension = ... Your path may be different,depending on your system. It is the "20210902" that is different.
To supplement what @Jim D has documented:
I had originally opened my VSCode WSL2 workspace using its Windows network share path
\wsl$Ubuntu-18.04srvwwwmyphpwebsite
. Xdebug did not work with this configuration—presumably because port 9003 was not being forwarded from Windows to WSL2.I had to reopen the workspace/folder using the Remote – WSL extension. To do this, you can click the Remote-WSL control on the VSCode status bar (bottom left corner) and choose "Reopen Folder in WSL". It took about 10 minutes to "install" Remote-WSL into WSL2.
Thereafter, I had to "install" (or enable) the PHP Debug extension specifically in the WSL:Ubuntu WSL2 environment, even though it was already installed in VSCode.
My
launch.json
configuration is:My PHP configuration (for PHP 7.3) is as follows: In the
/etc/php/7.3/apache2/conf.d
folder, the20-xdebug.ini
file is a symbolic link to/etc/php/7.3/mods-available/xdebug.ini
. This was configured automatically viasudo apt install php7.3-xdebug
. It contains just:Then I added a supplemental
99-xdebug.ini
file to the/etc/php/7.3/apache2/conf.d
folder (to ensure it loads after the20-xdebug.ini
file, and to keep its configuration separate from the distro). That file contains:Don’t forget to restart Apache after making
.ini
changes.With the
xdebug.start_with_request=trigger
setting, after launching the debugger in VSCode, I use the Chrome Xdebug helper extension to trigger an XDebug session. (If the.ini
settingxdebug.start_with_request=yes
is used instead, then the Xdebug helper extension is not needed, since Xdebug will try to connect with a debugger on port 9003 with every web request to PHP.)Lastly, I commented out the
xdebug.log=/tmp/xdebug/xdebug.log
setting, since this produces very large verbose logs. (But it was useful in initially diagnosing what was happening.)UPDATE
To use
xdebug.start_with_request=trigger
andxdebug.trigger_value=VSCODE
with VSCode’s "Launch currently open script" debug configuration, I had to set the correct environment variables in an"env"
section inlaunch.json
:Good luck with Xdebugging!