Xdebug with SSH tunnel on Docker for Mac - php

I was reading a lot of posts from the Docker community recently on how to debug PHP application in the PHPStorm with the Docker for Mac. All of them contains pieces of useful information, but haven’t seen working solution in one place.

Here is what did work for me.
Inside Docker Container
Edit xdebug configuration
# automatically start debugger on every request
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_port=9000
# send all debug requests to 127.0.0.1, remote_connect_back should be turned off
xdebug.remote_connect_back = 0
xdebug.remote_host=127.0.0.1
#log all xdebug requests to see is it working correctly
xdebug.remote_log=/var/log/remote.log
Verify that xdebug works
At this point try to run PHP application. Log should contain such entries for every request:
I: Connecting to configured address/port: 127.0.0.1:9000
I: Connected to client. :-)
If you see something like this in the log, remote_host or remote_connect_back are configured incorrectly.
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.18.0.1:9000.
W: Creating socket for '172.18.0.1:9000', poll: Operation now in progress.
E: Could not connect to client. :-(
I've seen situations when Xdebug worked in CLI but not from the browser, when this issue appeared in the log, remote_connect_back=0 fixed it.
sshd configuration
In order to allow ssh tunnelling to the container: edit /etc/ssh/sshd_conf and add:
GatewayPorts yes
Restart sshd if needed (ideally this should be part of the Dockerfile).
On Host Machine
Start reverse SSH tunnel
Run this command and keep it in separate Terminal tab open:
ssh -p {container_22_port} -R 9000:localhost:1111 root#127.0.0.1
where {container_22_port} is the port on host machine mapped to the exdposed 22 port on docker container. 9000 is the port used by Xdebug inside container, 1111 port that will be used by host machine to listen to Xdebug connections.
Test with netcat
At this point you can verify that Xdebug actually passes information from inside docker container to the host machine. Start netcat to see what is sent to the 1111 port and run php application:
nc -l 1111
You should see something like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/magento2/index.php" language="PHP" xdebug:language_version="7.0.12" protocol_version="1.0" appid="1006" idekey="XDEBUG_ECLIPSE"><engine version="2.5.0rc1"><![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>
Configure PhpStorm
Opne File->DefaultSettings, and there in Languages&Frameworks->PHP->Debug change Xdebug->Debug port to 1111 (the one we used to open ssh tunnel).
PhpStorm should start accepting connections from xdebug at this point.
Are there any concerns with this approach?

I found out that you don't need a SSH tunnel to get it to work.
Xdebug need only to connect out to the running IDE debugger but there might be some default ports already used like the one for FPM (9000).
Inside the container
Set the xdebug options as this:
xdebug.remote_enable = 1
xdebug.remote_host = localhost
xdebug.remote_port = 10000
xdebug.remote_connect_back = 1
Note: if using the nginx-proxy container as reverse proxy set your remote_host as the one you defined in the VIRTUAL_HOST
Using PhpStorm
File -> Settings
Languages & Frameworks
PHP
Servers: add one and set the host as localhost: 80 with Xdebug with Path mapping selected ... map the folder INSIDE the Docker (/var/www/) to the one
Debug: set the Xdebug port to 10000 with the Can accept external connections checked

use xdebug.remote_host=host.docker.internal works everywhere.

Related

Use Xdebug with dockerized PHP project

Recently I've started to work on a new project that is dockerized and has Xdebug module. Also I've checked in container itself and 9000 port is listening and has Xdebug module version 2.9.8
I don't have any PHP or anything installed on my Ubuntu (host system).
In my PhpStorm I set my PHP interpreter manually:
Also set DBGp proxy like
Also created a server:
This is my etc/hosts:
and I call API in Postman with the following address: api.vendet.local/api/v1/test
UPDATED path mapping:
Still not break in any point. What is the problem and how can I do that?
I've checked in container itself and 9000 port is listening
It is IDE that should be listening on port 9000, not the container.
#1: you don't need to use DBGp proxy.
#2: at PHP | Servers you have to enable "Use path mappings".
If I were you, I would delete all the configuration you made in PhpStorm (#1 & #2) and would start from the scratch.
First: make sure to configure XDebug properly in a container, see Configure Xdebug running in a Docker container. Pay attention to the value of xdebug.client_host described in the article.
Then: just run a simple Zero-configuration debugging
Be aware that as long as you are not using network_mode: "host" for the php container localhost / 127.0.0.1 for Xdebug running within the container is not the same as localhost on your Ubuntu host!
To be able to connect to Xdebug from PHPStorm you need to ensure the following:
the debugger is listening on an IP reachable from the host: listening on 127.0.0.1 it would only be accessible from within the container itself. Connecting to 127.0.0.1:9000 on the host will be forwarded to the container, but to the virtual network interface of the container and not to the loopback device. To simply listen on all devices within the container specify 0.0.0.0 as address
path mappings are enabled - since (most probably) the paths of the PHP files within the container are not identical with the paths to the same files on the host.

NetBeans Xdebug Remote Host waiting for connection for infinite time

I am trying to configure Xdebug to my NetBeans IDE which is showing as "Waiting for Connection". I have already spent two days but I didn't find a clue on it.
I am connecting to the code which is in the remote server from my NetBeans IDE using SFTP Connection which is successful.
My Remote Server is a VM with the Cent OS 7.
My Application is configured with nginx, PHP-FPM, PHP 5.6.
I have installed xdebug-2.5.5 in the server and added the following in the php.ini:
zend_extension=/apps/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.idekey="netbeans-xdebug"
;xdebug.remote_connect_back=1
xdebug.profiler_enable_trigger=0
xdebug.remote_log="/mypath/xdebug.log"
I have cross checked that xdebug.so is located in the correct path which I mentioned above.
In my NetBeans, Tools >> Options >> PHP >> Debugging:
Debugging port: 9000
Session ID: netbeans-xdebug
When I start the Debugging in NetBeans IDE, I am seeing the below logs:
Log opened at 2021-11-24 07:11: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:///myserver/location/path/index.php" language="PHP" xdebug:language_version="5.6.14" protocol_version="1.0" appid="4643" idekey="netbeans-xdebug"><engine version="2.5.5"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2017 by Derick Rethans]]></copyright></init>
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="break" reason="ok"><xdebug:message filename="file:///myserver/location/path/index.php" lineno="13"></xdebug:message></response>
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>
Log closed at 2021-11-24 07:11:45
and in NetBeans IDE, "Waiting for Connection" is keep on loading for infinite time.
I am looking for some help to understand what is the problem on this. I am not using any Docker and PhpStorm. Looking forward for some help.
You say "My Remote Server is a VM with the Cent OS 7" but then you have xdebug.remote_host=localhost
Xdebug is the one that connects to the IDE and NOT other way around. If your code is run on another server (another physical machine or inside the VM or some container) then xdebug.remote_host should point to the machine where your IDE is running (an IP address or domain name that can be resolved to that IP; as seen from that server). Unless, of course, you are using reverse SSH tunnel for Xdebug connections.
Right now you are connecting to some service on your server on TCP 9000 port. Extremely likely that it will be php-fpm (that also uses that port: that's one of the reasons why Xdebug v3 have changed the default port to be 9003 BTW). Your Xdebug log is a very short one: a typical response of a service that knows how Xdebug works that just closes the session. php-fpm can do just that.
So a few suggestions:
To ensure that only Xdebug is using this port: change Xdebug port to another number (e.g. 9001 or maybe better 9003 so it will be compatible with Xdebug v3) in both php.ini as well as NetBeans. Do not forget to restart your web server/php-fpm after that (check live settings using phpinfo();).
If no reverse SSH tunnel is used then set xdebug.remote_host to have an IP of the machine with your NetBeans. Quite often that would be the same IP as seen in $_SERVER['REMOTE_HOST'].
NOTE: Since this will be an incoming connection the firewall on your local OS, on your server and your router (if it's outside of your LAN) can prevent such connection. So you would need to ensure that you can receive incoming connections on Xdebug port on your local machine.

Debug remote server [PHP app] on my local machine [PhpStorm+Xdebug]

I want to debug remote server (AWS EC2 Ubuntu), where there is a PHP application. I use PhpStorm locally on my Windows machine. I can not debug the app on the server.
I have Googled a lot and found a lot of explanations, but stuck. I have installed Xdebug on the server and locally. I have setup the SFTP connection in PhpStorm, so it has the Server and can synchronize. The PhpStorm->Settings->Languages & Frameworks->PHP->Debug - [Validate] says everything is OK, when I choose Remote Web Server
When I click Start listening for PHP Debug connections in the PhpStorm IDE, I can debug the PHP code with breakpoints. The files are exactly the same as on my server. But nothing happens right now.
First and foremost; xdebug should be able to call back home; ie the local computer running phpStorm.
Here is a quick checklist
can the remote connect to local directly? Can the remote host connect to local hosts xdebug port (generally 9000)?
IF yes;
is xdebug configuration correct? Here is an example for a direct connection between remote and local:
xdebug.remote_enable = 1
xdebug.remote_port = 900
xdebug.remote_connect_back = On
IF No;
there should be a way for remote to connect back to local. One option would be using an ssh tunnel. For that; xdebug configuration on remote should be amended accordingly; like:
xdebug.remote_host=127.0.0.1
xdebug.remote_port = 9001
xdebug now tries to connect its own port; 9001 which is actually a tunnel to your localhost; port 9000
you can create a tunnel with ssh on nix; Putty on win
Also, xdebug log files are indispensible whide debugging problems; be sure that xdebug IS creating logs; check them; example:
xdebug.remote_log = /var/tmp/xdebug.log
After all those are set; activate PHPStorm's listening mode and start debugging.
Also; may be good to remember the documentation

Waiting For Connection (netbeans-xdebug) MAMP OS X

Preamble
After many hours, I have been unable to get NetBeans to connect to xdebug. Some months ago, after upgrading from an old version of MAMP to MAMP PRO, debugging worked flawlessly. A week ago it started getting flakey. It would appear to connect but would not stop at the breakpoints. Restarting NetBeans (v7.0.1) and apache sometimes got it working for a short time.
I really needed it fixed so I installed the latest version of MAMP PRO (2.1.2). Now I get the Waiting For Connection message forever.
Testing I have done
While the Waiting For Connection message is there with the moving bar, I look to see if it’s listening. It is...
# lsof -i -n -P |grep 9001
java 6496 tim 230u IPv6 0xffffff80239d8190 0t0 TCP *:9001 (LISTEN)
In NetBeans php config I have the interpreter set to:
/Applications/MAMP/bin/php/php5.4.10/bin/php
Executing the following:
# /Applications/MAMP/bin/php/php5.4.10/bin/php -i | grep xdebug
tells me that xdebug is running as does phpinfo()
I have (many times) confirmed that I have the port number the same everywhere. I have tried port 9000 and 9001.
Doing a tail on xdebug.log then initiating a session from the browser without starting a debug session in NetBeans produces:
I: Connecting to configured address/port: localhost:9001.
E: Could not connect to client. :-(
With the waiting for connection message and initiating a session from the browser, I get this in the log:
: Connecting to configured address/port: localhost:9001.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///Users/tim/MAMPSites/facts.tvd.us/htdocs/sendfile/tim.php" language="PHP" protocol_version="1.0" appid="7279" idekey="netbeans-xdebug"><engine version="2.2.1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2012 by Derick Rethans]]></copyright></init>
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>
My php.ini file has the following:
[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.3.20/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.remote_enable=on
xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9001
xdebug.idekey="netbeans-xdebug"
Update
I just noticed that the lsof command above shows NetBeans listening on ipV6. Forcing java (NetBeans) to use ipV4 does not help.
launchctl setenv JAVA_TOOL_OPTIONS -Djava.net.preferIPv4Stack=true
I found a post that suggested a test to confirm xdebug is working correctly. Create a php file:
<?php
$address = '127.0.0.1';
$port = 9000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Unable to bind');
socket_listen($sock);
$client = socket_accept($sock);
echo "connection established: $client";
socket_close($client);
socket_close($sock);
?>
Run it from the command line and load any page in your browser with the following at the end of the url:
?XDEBUG_SESSION_START=nb
If it outputs something like "connection established: Resource id #5", xdebug is working correctly. With that, I reinstalled Java and NetBeans. I told NetBeans NOT to import my existing preferences... Still no connection.
Update2
I installed the phpStorm IDE for Mac. I learned enough about it to get the debugger running with my existing MAMP and xdebug setup. I think this confirms the problem is with NetBeans.
At this point, getting this working seems impossible. :(
I asked the same thing and got this quite good answer: How to track execution time of each line / block of lines / methods in PHP?, beside that i have this answer also on a currently running other question with the same content.
Some additional notes on that (stuff i've collected in other SO posts in my own research with this problem):
The port might be also 9001 (seems to work for some people while 9000 don't).
Switching to PHPStorm IDE is a real alternative that solves this problem (as PHPStorm has included perfectly running xdebug already).
Download a new version of xdebug via the wizard (http://xdebug.org/wizard.php) and if you follow the instructions maybe you will be lucky.
Switching off the firewall might help.
Add to php.ini: xdebug.idekey=netbeans-xdebug.
Find out if you have a xdebug.ini file and add the xdebug related php.ini lines to that file.
you have to un-comment the zend_extension line (i.e. remove the ; at its begninning), so Xdebug is actually loaded.
make sure Xdebug is loaded, calling phpinfo() from a PHP file (just to be sure).
Xdebug will only connect if there is an index.php file in your project folder, so check to make sure you have one.
it works for me now. I have LAMP installed. I modified according to all answers from above, started apache2 and now it flies... i am happy... for some time i thought to switch to phpstorm, but I reconsidered... tweak a little more... et voila. it's working.
Here what I have in php.ini
zend_extension = /usr/lib/php5/20121212/xdebug.so
xdebug.max_nesting_level = 250
xdebug.auto_trace=On
xdebug.remote_enable=On
xdebug.remote_port=9001
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_log="/var/log/xdebug.log"
xdebug.trace_output_dir=/var/www/AMRO
xdebug.idekey="netbeans-xdebug"
On clean download and install of netbeans working on xampp, xdebug worked except would not stop at breakpoints. Changed php.ini to: xdebug.remote_enable = 1 (from 0) and xdebug worked as expected!

Xdebug for remote server not connecting - netbeans

I'm trying to use XDebug in the following scenario
my computer Windows 7, Netbeans 6.9
Physical Host at computer on the same network (Windows 7) with virtual Box
Virtual CentOS 6.2, with Apache server 2.2.15 and PHP 5.3.3
the PHP code of my website is on a shared folder on CentOS, in /var/www/html/mysite and have separate and can access it by server ip 192.168.1.240
Edited C:\Windows\System32\drivers\etc\hosts 192.168.1.240 mysite
the PHP code is accessible from my Windows host, on \\HostIP\html\mysite, with R/W permissions
I created a Netbeans project on my computer, pointing to \\HostIP\html\mysite. In the project Run configuration, I have the following:
Run as: Remote Web Site
Project URL: http://mysite/
Index file: index.php (does exist in the project)
In the Advanced Run Configuration:
I checked "Default"
I haven't touched the proxy settings
I have the following in the php.ini on my CentOS VM
;extension=xdebug.so
zend_extension="/usr/lib64/php/modules/xdebug.so"
xdebug.remote_enable=on
xdebug.remote_log="/var/log/xdebug.log"
;xdebug.remote_host=192.168.1.31
xdebug.remote_connect_back=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
note: I tried the configurations with extension=xdebug.so and tried commenting xdebug.remote_connect_back=1 with uncomment xdebug.remote_host=192.168.1.31 which is my computer ip address.
so basically i have all the configurations like this image
but still not working! after run the debugger will open this url
http://mysite/index.php?XDEBUG_SESSION_START=netbeans-xdebug
and nothing will happened just netbeans Listing waiting for Xdebug connection
When i want to debug on a remote host i normally have to forward my local 9000 port to the remote server's local 9000 via a ssh tunnel:
ssh -R 9000:localhost:9000 username#remote.example.com
Use bitvise ssh client or putty on windows to get the same effect.
Also in the project settings -> run configuration -> Advanced button
make sure to specify the remote and local full paths the the project files so the debugger can attach (don't worry about the port in this screen leave as standard).
I know this is an old post, but it seemed like the one I ran across the most tonight.
The NAT Network setup is the way to get the least amount of conflict. In the Windows 7 Firewall, setup a custom Inbound Rule to allow inbound traffic to port 9000 for the entire /24 network that the VM is on (192.168.202/24).
My working XDebug setup in the php.ini, using 2.2.7 with Netbeans 8.0.2:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.idekey="netbeans-xdebug"
xdebug.show_local_vars=0
;xdebug.extended_info=1
xdebug.output_buffering=off
xdebug.remote_log=/var/log/xdebug.log
Lastly, I want to point out that Netbeans listens for the remote address setup in the remote file config. Doing a quick netstat -an showed that Netbeans was listening on port 9000, with a CLOSE_WAIT status on an old address from my initial bridged network setup. Even after changing the Project URL, and the Remote Connection IP address, and closing several xdebug connections, this never changed. Only after closing Netbeans, does the stale connection drop. Once Netbeans is started again, the new remote server URL is used for the remote xdebug connection.
Mention of Netbeans holding a stale state was never discussed, and the ability to stop the xdebug connection from the IDE gives a false sense of resetting the socket.
If it still doesn't work, use the provided debugclient on the VM, visit the project page with "?XDEBUG_SESSION_START=netbeans-xdebug" appended to the index.php URL, and watch for the xdebug info to come in. At least then you know it works locally.
Try setting up any breakpoint to your executed script. Debug session may not show up while ide does not respect "break at first line" flag.
Also check if xdebug.idekey property in php.ini equals to ideKey in Netbeans' settings.

Categories