Netbeans + xdebug with CLI PHP - php

I was hoping someone could help with this:
I want to use xdebug to debug CLI PHP on a remote server. I'm not interested in debugging over the web.
I've set xdebug on the remote server, and port 9000 is open on my local machine. I know it connects since when I start debugging I can see it on the remote server's xdebug.log file:
I: Connecting to configured address/port: 192.168.123.195:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1"xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///path/scriptname.php" language="PHP" protocol_version="1.0" appid="15612" idekey="netbeans-xdebug"><engine version="2.3.1"><![CDATA[Xdebug]]></engine>
<author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2015 by Derick Rethans]]></copyright></init>
I've two problems:
1) the scriptname.php file above isn't what I'm trying to debug. It's just one of many PHP scripts my remote server's cron runs periodically. I just want to debug what I'm working on at the time.
2) I can't actually debug the file which I do run. When I hit php myfile.php from the command line nothing happens, but on Netbeans I can't actually step into functions, see variable values or anything like that.
Am I missing something here? My php.ini values are as follows:
xdebug.remote_handler=dbgp
xdebug.remote_host=192.168.123.195
xdebug.remote_port=9000
xdebug.remote_mode=req
;xdebug.idekey="netbeans-xdebug"
(when the last line is commented out I do export XDEBUG_CONFIG="idekey=netbeans-xdebug" instead).
I don't even mind moving to a different IDE (I'm running Linux) as long as it works. But it has to be CLI execution on a remote server.
Any ideas? Thanks in advance.

Related

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.

How to debug a remote PHP script that is called by a third server

I need to debug a PHP script on a remote server that I can not call from my web browser. Instead it is called by a third party server as a web hook.
Preferably I would like to make the webhook start a remote Xdebug session and step through the code in the NetBeans IDE that I am using.
I am using an SSH tunnel to connect the remote debugger to my development machine.
I can not use the GET/POST/COOKIE variable with the webhook so I have set xdebug.remote_autostart=1 to make the debug session start nontheless.
I have added a couple of break points to my page and have it opened in the IDE Editor while I fire up the webhook call from the third server.
Still a debug session is not started. I know that the page is executed because I have added some logging commands to it.
Is it at all possible to do what I want? Thanks for any hints and comments!
When answering please note: I will not accept any answer that recommends logging to a file because that is what I am already doing.
Thanks!
Here is the Xdebug section of my php.ini (and yes it is the correct php.ini file instance):
xdebug.idekey="netbeans-xdebug"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_autostart=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
Here is the Xdebug log of the session.
[21318] Log opened at 2019-04-01 19:50:38
[21318] I: Connecting to configured address/port: 127.0.0.1:9000.
[21318] I: Connected to client. :-)
[21318] -> <init xmlns="urn:debugger_protocol_v1"
xmlns:xdebug="https://xdebug.org/dbgp/xdebug"
fileuri="file:///var/www/couchback/update.php"
language="PHP"
xdebug:language_version="7.0.33-0ubuntu0.16.04.2"
protocol_version="1.0"
appid="21318"
idekey="netbeans-xdebug">
<engine version="2.7.0"><![CDATA[Xdebug]]></engine>
<author><![CDATA[Derick Rethans]]></author>
<url><![CDATA[https://xdebug.org]]></url>
<copyright><![CDATA[Copyright (c) 2002-2019 by Derick Rethans]]></copyright>
</init>
[21318]
[21318] -> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="https://xdebug.org/dbgp/xdebug" status="stopping" re
ason="ok"></response>
[21318]
[21318] Log closed at 2019-04-01 19:50:40
The debug session is started but for some reason the IDE does not seem to take care. Probably that is because of the missing idekey in the uri? I have no means to add the idekey since the webhook originates from a third party. It might be an option to send the webhook to a proxy and forward the call with the idekey added to it. I will be happy to hear any other suggestions that are not as complicated.

Xdebug with SSH tunnel on Docker for Mac

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.

PHPStorm not reacting to break points

I have recently installed PHPStorm and Xdebug in order to debug PHP scripts running on a local server. I am running Mac OS 10.8.5 and MAMP. I have been using Safari 6.2.4 as my principal browser, but the issue occurs also with Initially, I was able to get PHPStorm to stop at checkpoints I have placed, but in one script, it refused to enter a foreach() {} statement. I therefore created a small test script which contained only this statement, and tested it on its own. First PHPStorm was happy to step through each line, and then it refused to stop at any breakpoints at all, either in my small test script or in any other script.
I have spent several hours checking and rechecking the various official guides, and testing all the techniques suggested. At times, my test page will start to load, and then the progress bar in the browser will stop. I am guessing that PHPStorm is, in fact, reacting to the break point, but failing to show the debugger controls.
Here is a screenshot of this situation. Should there be another panel in the PHPStorm window, to allow me to step through the script? If so, how can I get it to appear? If not, is there any way of debugging the PHPStorm debugger?
Here are the xdebug settings in php.ini:
[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_host="localhost"
xdebug.remote_port="9000"
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/Applications/MAMP/tmp"
xdebug.remote_log="/Applications/MAMP/logs/xdebug.log"
I have a Run/Debug configuration set up in PHPStorm as:
Host: web Port: 9000
Document Root: /Volumes/Sites/web
Here's the output of the xdebug.log:
Log opened at 2015-05-20 12:06:48
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:///Volumes/Sites/web/test.php" language="PHP" protocol_version="1.0" appid="3174" idekey="1"><engine version="2.2.3"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2013 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 2015-05-20 12:06:48
In the Run panel, I see:
/Applications/MAMP/bin/php/php5.5.3/bin/php -S jce:9000 -t /Volumes/Sites/web
[Wed May 20 08:06:48 2015] 127.0.0.1:51388 Invalid request (Malformed HTTP request)
The port number after 127.0.0.1: changes each time I refresh the browser.
PHPStorm also fails to react to breakpoints set in JavaScript files running in the browser.

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!

Categories