php - Want Xdebug -- Using Win10 WSL Ubuntu 20, vscode and Laravel
Starting a new project in Laravel. Using the latest WSL2 under Win10 v2004 (Apr 2020) instead of Homestead. It's all working great. Now I am brave enuf to want PHP breakpoints with Xdebug. I have installed Xdebug in Ubuntu 20 in the WSL.
~/STGdev/lv $ php -v
PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Xdebug v3.0.0-dev, Copyright (c) 2002-2020, by Derick Rethans
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
I'm not able to get any breakpoints to work. The big question is this --- does the vs code Xdebug extension use the Ubuntu PHP or does it require that I install an XAMPP PHP on under Windows? The instructions imply that either can be used?
Answer
Solution:
You have two options to enable debugging:
- Use the VSCode Remote - WSL extension and let VSCode configure everything else for you
- Configure XDebug Remote Configuration in php.ini
My recommendation is using the WSL Extension method.
Keep in mind if you decided to use the Extension method, there will be a problem regarding Git not recognizing modified files correctly
Answer
Solution:
You have XDebug 3 installed. Version 3 of Xdebug changed the way you need to configure is's settings. The most important breaking change is that the listening port now is 9003 instead of 9000. This is the php.ini configuration in my localhost for Xdebug 3 (it should work anywhere):
[xdebug]
zend_extension="C:\xampp\php\ext\php_xdebug-3.0.4-7.4-vc15-x86_64.dll"
xdebug.mode=develop,debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=trigger
Change the path to your Xdebug extension and you should be able to debug your code now.
Answer
Solution:
You don't need to install XAMPP, all settings are managed by your WSL with installed PHP. I have a Laravel project with docker using WSL2 (Ubuntu 20) in Windows 10 21H2 and here is my settings to make xdebug work. I use Xdebug 2.9.8, there're some changes of text in xdebug.ini, but most of them are same.
PHP 7.1.33 (cli) (built: Nov 22 2019 18:34:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.9.8, Copyright (c) 2002-2020, by Derick Rethans
- If you use mount your project/files from Windows to your WSL2 environment
xdebug.ini
xdebug.remote_enable = On
xdebug.remote_autostart = On
xdebug.remote_connect_back = Off
xdebug.remote_host = "host.docker.internal"
xdebug.remote_port=9009
Dockerfile
FROM php:7.1-fpm
RUN pecl install xdebug-2.9.8 \
&& docker-php-ext-enable xdebug
COPY xdebug.ini /usr/local/etc/php/conf.d/
Docker-compose.yml
services:
webapp:
working_dir: /var/www/html
volumes:
- ./my-project-folder:/var/www/html
Move in to your project folder, type code .
to open projet with WSL and then create debug file (launch.json): (enable your PHP Xdebug extension inside WSL by click on extenstion and choose 'enable')
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9009,
"pathMappings": {
"/var/www/html": "${workspaceFolder}"
}
- If you use wsl file system (clone your project inside your wsl enviroment instead of mounting it from Windows) The setting is almost similar to (1) but you need a little additional setting:
Docker-compose.yml
extra_hosts:
- "host.docker.internal:host-gateway"
VS Code's launch.json
"hostname": "0.0.0.0",
That's all. 2nd method is much faster than 1st method in my experience.
Source