Configuring your dev environment to be able to use xdebug when you're working on Windows 10 (with WSL2) and Docker with VS Code can be (a bit) tricky.
This is a quick reminder of how I've done that.
-
Install and configure xdebug in Docker
# Install xdebug according to the Docker image you're using RUN pecl install xdebug COPY xdebug.ini $PHP_INI_DIR/conf.d/
(cf. the xdebug.ini file in this gist)
-
Setup VS Code
PHP Debug is the extension to debug PHP in VS Code.
Once installed and your project opened, create the configuration file.vscode/lauch.json
(cf. the lauch.json file in this gist)Two impostant things here :
- The port must match the one defined in xdebug.ini
pathMappings
must associate the path of your app on Docker and the path of your VSCode project
(do not use an absolute or relative path. You must use the variables VSCode provides)
-
Configure Windows Defender Firewall
Even after starting debugging, breakpoints might never be hitten. That's because of Windows Defender Firewall.
It treats WSL as a public network by default and blocks access. This can be fixed by adding a rule. Just run (with Powershell):New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow
More about this issue here
Now that your environment is set up, it's time to debug!
-
Run a debugging session
In the left side panel of VSCode, select the "Debug view" (or just press
Ctrl + Shift + D
), make sure that the chosen configuration is the one you added in the file lauch.json. Then click on the little green triangle titled "Start debugging". -
Add breakpoints wherever you want to debug your app
-
Refresh your browser!
Optional:
You can add Xdebug helper to debug in Chrome.
Yeah!
You're ready to create an awesone app!
Want to know more on debugging with xdebug?
Let's read the 'Step Debugging' doc
Hi,
thanks for the guide!
I am running Win11 and it should be the same, but its not working for me. I ran the powershell command, and I added a rule for ESET Internet Security, but it still wont hit the breakpoint.
Any suggestions?
Thanks in advance