xDebug not working using docker, vscode and WSL 2 - php

I am not sure what the issue is, it just doesn't work.
The routing seems to work, I have a server name in my nginx conf file. e.g. test.com. that works.
My project is in the root of ubuntu and not in the mount folders.
I am not sure what else to try.
xdebug.ini
[XDebug]
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9002
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_host=soapboxtest.com
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log
launch.json
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9002,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www": "${workspaceRoot}"
},
"ignore": [
"**/vendor/**/*.php"
]
},
Request cookies
"XDEBUG_SESSION" => "VSCODE"
Dockerfile
FROM php:fpm-alpine3.11
...
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
...

This happened to me also and found that XDEBUG is not looking for the Docker Daemon Host. Got it fixed by adding:
"hostname": "0.0.0.0"
As part of the general options of the launch.json on VS Code.

After had a colleague helping me to configure Xdebug to listen out of WSL Remote environment from VSCode, I´ve looked up for an option that is opened when you click on the WSL Remote icon on the lower left side of the IDE:
Remote-WSL: Show Log
It opens a WSL terminal, which shows (between several infos) the IP Address that WSL is using. Adding this WSL IP to the xdebug.remote_host value, the only thing left to get Xdebug running correctly from your Docker container, inside your WSL2 environment (from WSL Remote VSCode extension), is adjusting your application path inside the launch.json file.
For that, clicking with the right-button mouse at the launch.json file tab, search and click on the "Copy path" option, and then paste it inside the pathMappings setting, like this sample (don´t forget to adjust the path removing the info relative to launch.json itself, it's for your application path!):
"pathMappings": {
"/application": "paste here the path you've copied"
}
Try it, for me worked like a charm. And if it work for you too, vote for my answer!

I have spent many days "ripping my hair".. I use docker.
add rule into firewall to allow traffic (taken/quoted from: https://github.com/microsoft/WSL/issues/4171#issuecomment-559961027
Go there for screenshots if this isn't clear enough)
In windows Start menu, type Firewall and choose Advanced settings, then Inbound Rules
select custom rule.
Click Next until you get to Protocol and Ports in the left menu
Select ICMPv4 as protocol type.
Select Next until you end up in the Name section, enter any name and click Finish.
If you now try pinging your host ip from wsl2, it should work as
expected.
For every other connection between your wsl2 and your host, you have
to allow the inbound rules for private and public networks or if they
don't exists manually create the rules for the corresponding UDP/TCP
port, but be aware that this might impact your security, if you use
your computer in public networks.
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9900,
"pathMappings": {
"/linux/path/in/docker":"${workspaceFolder}",
}
},
]
}
Dockerfile
RUN echo "zend_extension=/usr/lib/php/20190902/xdebug.so\n \
xdebug.remote_enable=on\n \
xdebug.remote_autostart=on \n \
xdebug.remote_connect_back=on\n \
# xdebug.remote_host=$HOST_IP\n \
# xdebug.remote_log_level=9\n \
# xdebug.remote_log=/var/www/mywebdir.com/xdebug.log \n \
xdebug.remote_port=9900" > /etc/php/7.4/mods-available/xdebug.ini;
EDIT:
After a while I started having problems again. Try these:
uncomment the logging lines above in dockerfile
try hard coding your windows ip address like 192.168.0.x
(uncomment xdebug.remote_host=... and comment out xdebug.remote_autostart=on)
My xdebug appeared to be connected but closed connection straight after because it had to be my windows ip. My understanding is that its the VSCode that responds to xdebug requests which runs in windows so the debugging packets need to get through out of WSL2.
try logging level 10. If you see that xdebug can't find a match for the breakpoints, check your webroot mapping in docker and pathMappings in vscode/launch.json
Check that you do have a breakpoint set (stupid but yeah)

Related

Xdebug shows method doesn't exist message

I am trying to implement Xdebug in my VSCode for WordPress project. I am using XAMPP and PHP 7.4.15. I have set up Xdebug on my computer (Windows 10).
launch.json
{
// 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",
"port": 9003
}
]
}
php.ini
[XDebug]
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_port=9003
xdebug.client_host = localhost
zend_extension="G:\xampp\php\ext\php_xdebug-3.0.3-7.4-vc15-x86_64.dll"
I have installed PHP Debug Extension by Felix Becker on my VSCode. Restarted my XAMPP and VSCode several times.
I have made several breakpoints on index.php file of my theme (Currently active).
I set the setting Listen for xDebug (wptest). But, When I run the debug, and refresh my index.php page, it shows me following error:
Any lead on this?
Xdebug is initiating a break-point here, likely because it was instructed to do so by VSCode. You can verify this by checking the breakpoint_set lines inside the Xdebug log file, which you can create by setting xdebug.log=/tmp/xdebug.log in php.ini.
On the bottom left of your last screenshot, you can see that there is a tick set for "Everything". For the VS Code plugin that means setting an Xdebug break-point on *, which includes every exception, including ones that are caught. If you un tick the box, Xdebug should no longer generate a break-point to interrupt your code here.

How to setup VSCode launch.json for Xdebug version 3.0?

I am building Laravel project now. Few days ago, I've reinstalled OS (Windows 10) and rebuild all development environment. Problem is updated version of Xdebug.
Debugging backend with version 2 of Xdebug worked well, but I have downloaded dll file which set with Xdebug v3 from xdebug.org/wizard, and according guide (something like I did before). But I cannot debug anymore.
Here is my launch.json file (old version)
{
// 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",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
And it seems there are some features updated from v2.
Something like above image.
I tried all cases I can but cannot found solution.. Anybody can help me? Any advice is appreciate. thanks.
IDE:
As the upgrade guide (https://xdebug.org/docs/upgrade_guide) shows, the first thing that has changed is the port number the IDE's should listen on for connections. This you have highlighted, but make sure you change it to 9003 in the json config file. This should be the only change you have to do in your IDE.
Next - Config of Xdebug:
There is this new setting in Xdebug: xdebug.mode (https://xdebug.org/docs/all_settings#mode). Its default value is develop and you have to change it (explicitly set it to debug) in order to put Xdebug in debug mode (you have to alter php.ini for this). Please see documentation for further details.
UPDATE:
As #LazyOne correctly suggested in the comments section, if you'd like to keep port 9000, you can skip modifying this in VS Code and instead set the port back to 9000 in the Xdebug settings: https://xdebug.org/docs/step_debug#client_port
This being said, the default port number was altered for a reason: https://bugs.xdebug.org/view.php?id=1848. You might want to adopt the new port number.
I solved the problem by deleting the cache using artisan command:
php artisan cache:clear
I don't know how this command solved the problem but I noticed that when I change the port to 9003 I still get error on port 9000 ! I think laravel has cached the port !

How to connect PhpStorm with Xdebug

I hope somebody here can help me with my issue:
I recently gotten into my first PHP project and need to set up debugging. I'm aware many people had the same problem before me but I struggle to find a solution.
I need to set up PhpStorm with Xdebug and with all settings made its still not working and when I start my debug session i get stuck at this with no further information:
This is my project setup:
Docker Webapp with Xdebug: 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp
PhpStorm PHP Debug Settings:
Xdebug settings from phpinfo():
Listening to debug connections in PhpStorm is on, starting a debug session create a session via GET: https://localhost/?XDEBUG_SESSION_START=16957 but all my breakpoints are ignored.
Trying to get more information I ran netstat:
Can anyone tell me what I am missing here?
Thank you very much in advance!
Don't use run/debug configurations for web debugging, it's counter-productive. You can initiate debugging connections from the browser directly using zero-configuration debugging.
Disable xdebug.remote_connect_back, it brings more harm than profit, especially with Docker.
xdebug.remote_host is not supposed to be localhost when you are using Docker, this way, the container is trying to send the debug data to itself instead of the host machine. It seems that you are using macOS and Docker for Mac, the correct hostname would be host.docker.internal in such a case.
If after you initiate a debugging session from the browser PhpStorm still cannot catch a connection, we'll need to take a look at the Xdebug log as #LazyOne suggested.
A blog post showing Docker in PhpStorm basics: https://blog.jetbrains.com/phpstorm/2018/08/quickstart-with-docker-in-phpstorm/
i used this setting and it worked :)
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.default_enable=1
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.profiler_enable=0
xdebug.profiler_output_dir="/var/www/html"
xdebug.var_display_max_depth=20
xdebug.remote_host=host.docker.internal
xdebug.remote_enable=1
xdebug.remote_connect_back=0
with launch.json in vscode
"name": "Listen 9000",
"type": "php",
"request": "launch",
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/html": "/Users/folder/project/src"
},
"port": 9000,
With docker-compose.yml:

Issues when Debugging PHP in VSCode using Docker and WSL2

I've been working with VSCode + Docker in Windows for some years now, and managed to have a fully working dev environment without any issues.
Recently i setup a new development environment with WSL2. Moved all my projects, libraries, CLIs, etc, into WSL, using Docker Windows with WSL2 containers and VSCode on Windows with remote connection to WSL. Everything is working very smoothly and i like the fact i can have everything separated.
But recently i came across an issue that i'm unable to solve, i lost the ability to debug PHP files.
I'm using VSCode Remote WSL extension to work on my projects inside WSL, but when i try to debug, nothing happens.
I have tree debugging settings in my VSCode for each dev environment that i use (Windows, MacOS and WSL). All work except for the WSL. When i try to debug with WSL, literally nothing happens, no output erros, no debug console information, nothing...
Here are my VSCode debug settings:
{
"version": "0.2.0",
"configurations": [{
"name": "Listen for XDebug Win10",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/project-a/api": "\\\\wsl$\\Ubuntu\\home\\ubuntu\\PROJECTS\\project-a\\api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
{
"name": "Listen for XDebug MacOS",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/project-a/api": "/Users/ricky/PROJECTS/project-a/api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
{
"name": "Listen for XDebug WSL",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/project-a/api": "/home/ubuntu/PROJECTS/project-a/api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
}
What am i doing wrong? Any ideas on how to solve this issue?
### UPDATE: I've changed the original right answer to a new one. Although #romain-prevost's solution worked, I think #dark's approach is wayyy much simpler :)
Forget about the other answers. They are working but too complicated in my opinion.
The problem is, that you can't connect to xdebug.
The solution is to tell xdebug to set remote_host to host.docker.internal. Everything from there is available to localhost. Now you only have to listen inside Visual Studio Code to localhost via hostname.
Et voilà.
Now you can debug things invoked by your browser, inside your phpunit tests or within your commandline scripts.
Complete Example
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"pathMappings": {
"/var/www/html/": "${workspaceRoot}"
},
"hostname": "localhost"
}
]
}
php.ini
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9000
Update for XDebug 3
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html/": "${workspaceRoot}"
},
"hostname": "localhost"
}
]
}
php.ini
[XDebug]
xdebug.mode = develop
xdebug.start_with_request = yes
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
I have been struggling with PHP xdebug in Docker using WSL2 as well. It all comes down to the remote host.
What is your xdebug config in php.ini? You should set xdebug.remote_host to your WSL2 local IP address (which you can get in a terminal using hostame -I).
I have tried several times to set the remote host IP address in my Docker—composing a file to pass to the container on startup—but it always fails. Thanks to another StackOverflow answer, however, I have a solution for that too:
In WSL2, set an environment variable for your local IP in your .bashrc file. I have set mine to
export IP=$(hostname -I)
In your Docker compose file for the PHP service, pass the IP address as a new host with the extra_hosts key. For compose v3.2 it is
extra_hosts:
- "docker.host:${IP}"
You can look here for other compose versions: https://docs.docker.com/compose/compose-file/
Finally, edit your php.ini file for xdebug to have this line:
xdebug.remote_host=docker.host
Your container will be able to reach the WSL2 distribution with your docker.host and connect to the port you set for xdebug.
I spent a lot of time figuring this out, mainly because WSL2 was released officially a few days ago and there are not many guides about it.
It was not so complicated in the end, but without the extra_hosts key I could not get the WSL2 IP address to work. It was in the container and in xdebug config, but I always got an error about the resource being unavailable, so don't forget it 😁
The answer by #Romain-prevost is definitely the easiest and best way. If for some reason you can't get that to work, there is another alternative.
host.docker.internal will likely be reachable from within your container, but is a 192.* address by default, and is your Windows-based Docker IP. So you would need to set up forwarding from your Windows host into your WSL2 instance. This is outlined in this script in this issue at Github/WSL. You would simply change the ports to 9000 in that script. You could also use WSL2-Host to have a named entry in your hosts file for that IP and modify the script further to both restrict calling/receiving IPs.

Visual Studio Code won't stop at breakpoint in PHP Xdebug

I have move from my Thinkpad laptop to HP Elitebook. I must set my development environment in my new laptop. After setup my Visual Studio Code and XAMPP in Windows 10 Pro 64 bit edition, I can't debug my PHP code.
I have properly installed Xdebug extension for Visual Studio Code, and make sure the php.ini correct. This is my setting for php.ini (I use portable XAMPP which is placed on E: drive):
zend_extension = "E:\xampp\php\ext\php_xdebug-2.4.1-5.4-vc9.dll"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_log="E:\xampp\apache\logs\xdebug.log"
And my xdebug.log show information like this :
Log opened at 2018-10-27 05:56:45
I: Connecting to configured address/port: localhost:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///E:/xampp/htdocs/absen/index.php" language="PHP" xdebug:language_version="5.4.3-dev" protocol_version="1.0" appid="7112" idekey="william"><engine version="2.4.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2016 by Derick Rethans]]></copyright></init>
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>
Log closed at 2018-10-27 05:56:45
But my Visual Studio Code won't stop at breakpoint.
I have reinstall my Visual Studio Code, XAMPP, and using another Xdebug version but it still doesn't work.
I have follow these links:
visual studio code PHP debugging not working
xdebug won't stop at breakpoint
xdebug breakpoint fail
Xdebug successfully connects to DBGP clients, but won't stop at breakpoints
None of these solutions work.
Finally i know the source of problem. The HP Elitebook have preinstalled program named HP Velocity, which used port 9000.
The issue is port 9000 from my host is bind to port 9000 of HP Velocity software. When HP Velocity service connect to the localhost xdebug server, logs say that connection is ok, but it is connected to HP Velocity.
I changed xdebug port to 9090 in php.ini and changed in launch.json on visual studio code. After that, my visual studio code work as usual and can stop at breakpoint.
Hope this help others :)
This is what fixed the same issue i was having,worth the shot if previous answers didn't help
php.ini
[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug-3.0.2-8.0-vs16-x86_64.dll"
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_port = 9000
xdebug.client_host = "127.0.0.1"
xdebug.log = "C:\xampp\tmp\xdebug\xdebug.log"
xdebug.idekey = VSCODE
launch.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"type": "php",
"request": "launch",
"name": "Run using local PHP Interpreter",
"program": "${file}",
"runtimeExecutable": "C:\\xampp\\php\\php.exe"
}
]
In my case, the pathMappings was incorrect, and did not change the port
{
...
"configurations": [
{
...
"port": 9000,
"pathMappings": {
"/var/www/yourproject": "${workspaceFolder}/",
}
}
]
}
My Breakpoints is actually working if I check the "Everything" breakpoint
(It captured a suppressed exception, but VS code alerted that the file was no found)
In my case, I was trying to debug a php script on my local machine, so I had to realize that I had to debug the php script using the port which the Web Server is using (8080 in my case) rather than using the port set on the VS Code configuration (I use 9090).
For anyone who is stuck with this error, this is my configuration:
OS: Windows 10 64 bits
PHP 5.6 (32 bits TS)
Xdebug 2.5.5 (32 bits)
Apache 2.4 (32 bits)
Xdebug Helper installed on Google Chrome
1) httpd.conf
2) php.ini (XDebug section)
3) launch.json (VS Code configuration)
The reason might caused by the port of php xdebug.It works for me fine when I did this:
1.use php --ini to show php config file and change xdebug port to 9900:
xdebug.remote_port=9900
then restart your php
2.open vscode and change the port to the same number in launch.json:
"port": 9900
3.enjoy your debug
I am a novice programmer so this will probably sound dumb, but might be helpful to someone new to PHP and web servers like me.
I had the same issue with Xdebug skipping a break point. For me it turned out to be that I had a break point on an "echo" statement. Apparently that is not a valid line for using a break point. It started working once I moved the break point to a variable declaration.
I had same issue but after change port to 8000 in ubuntu it works for me.
Please note if you are using Xdebug version 3.xx you need to do the following:
Stop Apache server, then in php.ini add at the end of the file:
[XDebug]
xdebug.mode = debug
zend_extension="C:\xampp\php\ext\php_xdebug.dll"
After that, start Apache server agian.
Note that adding xdebug.start_with_request = yes will tell xdebug to initialise communication with an IDE for every request even though you might not debugging, which gives a warning in your IDE console state:
[Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
It's better to use a chrome extension to trigger the activation of xdebug using a cookie in your browser. See https://xdebug.org/docs/step_debug#browser-extensions for more information.
In vscode go to Run in your top menu, then Add configuration, this will open launch.json, add under configurations:
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
},
Please note that port 9003 is different than your app port. I'm running my laravel app using: php artisan serve with port of 8000 and in my launch xdebug is 9003.
Re-run your debug in vscode and should be working.
I wish you the best in your bug-fighting war!
I saw the same issue, running ./vendor/bin/phpunit -c <config file> was causing all of my breakpoints to be disabled. I was only able to debug if I checked "Everything" and sorted through all the junk.
I updated the pathMappings in my launch.json file, and added a new launch configuration with a different pathMappings to work locally
{
"name": "PHP Unit Test XDebug",
"type": "php",
"request": "launch",
"port": 9001,
"log": true,
"externalConsole": false,
"pathMappings": {
"${workspaceFolder}": "${workspaceFolder}"
},
"ignore": []
},
I found this solution from this SO post, and this one here Debugging PHPUnit tests in VS Code?
Turns out the shell environment variable XDEBUG_MODE overrides the xdebug.mode setting in your php.ini, and I was setting the XDEBUG_MODE variable in my .zshrc file (likely a configuration from a previous project that I had long forgotten about).
In short, if xdebug isn't working and you've seemed to follow all of the instructions about setting up your IDE and php.ini file correctly, make sure you aren't inadvertently setting XDEBUG_MODE somewhere else.

Categories