64

I want a team using different computers to be able to debug PHP on a remote server, but I am having a hard time getting Xdebug to work in NetBeans 7.0.1. I’ve tried many online tips, but to no avail.

For the record, I have successfully installed Xdebug locally on a Windows 7 machine running WampServer. So I can debug PHP with breakpoints in NetBeans, provided I set the Project Properties->Run Configuration->Run As property to Local Web Site. However, as stated above my goal is to debug in NetBeans on a Remote Web Site.

My server is a Ubuntu 11.04 machine. I have used the output from http://www.xdebug.org/find-binary.php to put the proper binary on the machine. I have modified all php.ini files I could find (in both the php5/apache2 and php5/cli directories) to include these lines:

zend_extension="/usr/lib/php5/20090626+lfs/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000

If I check the phpinfo.php web page, it says:

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.2, Copyright (c) 2002-2011, by Derick Rethans

So Xdebug does seem to be installed properly. Still, when I try debugging in NetBeans, I get the endless status bar message Waiting For Connection (netbeans-xdebug). When I hit the stop button I get No connection from xdebug was detected within X seconds. The reason could be that xdebug is not installed or not properly configured.

Maybe I'm confusing local settings with server settings here? A post said xdebug.remote_host should be set to the IP of the machine running NetBeans, but I want a team to be able to debug using machines with different IP addresses. A problem could be port 9000, but I have checked that it is not blocked.

Any help that could clarify this would be appreciated!

Community
  • 1
  • 1
Gruber
  • 4,478
  • 6
  • 47
  • 74
  • 1
    Using ``xdebug.ide_key`` and setting it to some complex value should serve as a password, iirc. – Gelmir Feb 18 '14 at 13:35
  • 1
    Running into this problem again years later, the problem was caused by a firewall setting that prevented the server running Xdebug from contacting my development computer (see @Linus Kleen's answer). So check your firewall. – Gruber Aug 15 '14 at 09:30
  • 1
    Yea, some kind of NA(P)T eventually is required to forward incoming external connection from development server to your computer in local area network. – Gelmir Aug 15 '14 at 15:44

12 Answers12

85

The server running PHP (and XDebug) needs to be able to connect to your workstation/desktop.

Diagram

So you'll need the server set up accordingly by either telling it to connect to a specific IP-address (xdebug.remote_host) or to automatically "connect back" (xdebug.remote_connect_back). The latter has some security implications, though. These are outlined in the manual.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • 1
    Many thanks! It now works! I simply added the configuration lines `xdebug.remote_connect_back=1` to the php.ini files. Also thanks for noting the security implications. – Gruber Nov 08 '11 at 12:27
  • 2
    Checking the documentation, the `xdebug.remote_host` setting seems to accept only one address, and with many team members with different IP addresses it seems to me `xdebug.remote_connect_back=1` is the only setting that works. – Gruber Nov 08 '11 at 12:37
  • 5
    You could set up multiple virtual hosts that have a dedicated `php.ini` to circumvent the connect-back issue. Our development department is set up like that. – Linus Kleen Nov 08 '11 at 14:31
  • Thanks, that sounds like a solution (albeit a quite cumbersome one). I hope Xdebug will be updated in the future to accept multiple hosts in the `xdebug.remote_port` setting. – Gruber Nov 09 '11 at 09:44
  • Using ``xdebug.ide_key`` and setting it to some complex value should serve as a password, iirc. – Gelmir Feb 18 '14 at 13:34
  • @LinusKleen I have setup the remote_connect_back but Nginx start showing 504 gateway timeout error...please note I have tried increase timeout in nginx config too...any suggestion for me? – user269867 May 19 '14 at 05:50
  • 2
    @user269867 Does [this question](http://serverfault.com/questions/178671/nginx-php-fpm-504-gateway-time-out-error-with-almost-zero-load-on-a-test-se) help? – Linus Kleen May 19 '14 at 06:01
  • Wouldn't you also need to either forward port 9000 from your router to your local development machine, or else use SSH tunneling to connect to the remote debug session? – Buttle Butkus Oct 27 '15 at 21:16
  • @ButtleButkus You'd need to if the debugged machine and the PC doing the debugging are on separate networks; not so, if they're not. – Linus Kleen Oct 28 '15 at 06:37
  • @LinusKleen could you give any hint about SSH tunneling? I think I understand the basic concept, that the two machines can communicate as if they are on port 9000 but actually they are communicating through port 22. Yes? I followed this jetbrains tutorial (https://confluence.jetbrains.com/display/PhpStorm/Remote+debugging+in+PhpStorm+via+SSH+tunnel) but Xdebug on the remote server still can't connect to my port 9000 (timeout). I can't tell if the tunnel is actually "working" - how would the remote server's Xdebug output know to go through the tunnel instead of directly to my blocked port 9000? – Buttle Butkus Oct 28 '15 at 18:00
  • 2
    @ButtleButkus In short, if machine **A** needs to connect to **B** via port 9000, but that port is blocked, it establishes a common SSH session and instructs the SSH client (on machine **A**) to set up a port 9000 *locally*. Connections to this local port are then *tunneled* via SSH to target machine **B**. Not much room left here in the comment field; ask a question about this if you need more details. – Linus Kleen Oct 29 '15 at 06:46
  • @LinusKleen I hope this question is not too long and/or general. Let me know. Thank you. http://stackoverflow.com/questions/33424393/explain-ssh-tunneling-process-and-limitations-for-a-remote-xdebug-session – Buttle Butkus Oct 29 '15 at 20:54
  • I am using PHP5.6 and XDebug2.5.5 How is the configuration for my case. Can you please look into https://stackoverflow.com/questions/70092201/netbeans-xdebug-remote-host-waiting-for-connection-for-infinite-time – Gireesh Doddipalli Nov 24 '21 at 08:06
25

Xdebug 3 upgrade guide for remote debugging

Summary of: https://xdebug.org/docs/upgrade_guide

Required PHP configuration changes/additions:

  • remove xdebug.(remote_enable|default_enable|profiler_enable|auto_trace|coverage_enable)
  • add xdebug.mode=debug OR use develop|coverage|gcstats|profile
  • add xdebug.start_with_request=yes
  • xdebug.remote_autostart is replaced by xdebug.mode=debug with xdebug.start_with_request=yes
  • xdebug.remote_host is replaced by xdebug.client_host
  • xdebug.remote_port is replaced by xdebug.client_port OR use new default in IDE setting (more below)

Example new configuration

xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.idekey=myKey
xdebug.client_host=x.y.z.a
xdebug.remote_handler=dbgp

Where

  • x.y.z.a = your IDE host
  • myKey = your configured key in the IDE

Required IDE configuration changes/additions:

  • set remote_port to 9003 (xdebug's new default port) OR set xdebug.client_port to 9000 in the configuration above to keep the old default
Brad G
  • 2,528
  • 1
  • 22
  • 23
  • Very correct answer; Thanks... In my case I had Win 10 with Php7.3.13, Netbeans 8.2 and IIS 10 (browsers are Chrome with netbeans extension & FF)... Then I installed xdebug(using wizard) and config as below: – Saurabh Jan 07 '21 at 18:49
  • Just want to add that with Xdebug 3 - we should use `xdebug.discover_client_host=1` instead of `xdebug.remote_connect_back` – Sannek8552 Mar 12 '21 at 14:17
  • @Brad, I am using PHP5.6 and XDebug2.5.5 How is the configuration for my case. Can you please look into https://stackoverflow.com/questions/70092201/netbeans-xdebug-remote-host-waiting-for-connection-for-infinite-time – Gireesh Doddipalli Nov 24 '21 at 07:40
  • Thank you, I had a mix of old and new ways from wamp initial setup before upgrading and using your config above minus the idkey, worked perfectly! – Liam Wheldon May 13 '22 at 14:06
24

For me, xdebug.remote_connect_back = On does not work. What I did was to set ssh port forwarding on my client machine.

xdebug config on the remote machine:

xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_handler = dbgp
xdebug.remote_mode = req

forward ports on the client machine:

ssh -g -N -lusername -R9000:127.0.0.1:9000 [remote.host.ip]

The shell access on the remote machine must be allowed.

David Refoua
  • 3,476
  • 3
  • 31
  • 55
medium8
  • 241
  • 2
  • 3
  • 2
    I believe this is called SSH tunneling, not port forwarding, although it is kind of similar. I think "port forwarding" generally refers to a public-facing router configured to send traffic to certain machines inside its private network depending on what port the traffic is for. I used that for setting up a web server on my home machine. Traffic for port 80 (http) was automatically sent to that machine (instead of, say, my room mate's). – Buttle Butkus Oct 29 '15 at 00:19
  • 1
    Excellent, this works, can you explain what is going on, because I had used before a similar ssh command to make a proxy to a server, but that solution didnt work. – santiago arizti Sep 15 '17 at 23:15
20

The key directive is this:

xdebug.remote_connect_back = On

This allows the web server to connect to whatever computer is asking for a debugging session. This way you don't have to hard-code an IP address and are able to share Xdebug. This directive was not present in earlier versions and is often omitted from tutorials and documentation.

You also need to verify that every client computer accepts incoming connections to port 9000 (xdebug.remote_port). This includes configuring the firewall and making sure the debugger client is up and running

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thanks. I have read much Xdebug documentation but somehow overlooked the `xdebug.remote_connect_back` setting. Since NetBeans itself produces a help message with proposed php.ini settings, I assumed it was a matter of finding the right values for these settings rather than adding another new settings line. – Gruber Nov 08 '11 at 12:48
  • 1
    I had to turn off `xdebug.remote_connect_back`. With an SSH tunnel, the remote machine is apparently supposed to connect to its own port 9000 (hence `xdebug.remote_port=9000` and `xdebug.remote_host=localhost`). Therefore, you don't want it to try to connect to the remote ip e.g. 173.12.34.25:9000, but localhost:9000. Also, the remote_host and connect_back options don't make sense to use together. – Buttle Butkus Oct 29 '15 at 00:15
8

In my case, those commands helped me:

xdebug.remote_enable = On
xdebug.remote_autostart=1

Notice: the debugger will work even if GET/POST/COOKIE variable is not present because of 'xdebug.remote_autostart=1'

Asaf Hananel
  • 7,092
  • 4
  • 24
  • 24
7

I had the same issue a couple of times while trying to configure docker and after scratching my head multiple times I realized this was the way to fix it. So I decided to put this here as an answer for my future self.

Most of the time the Dockerfile was adding this this statement to php.ini:

xdebug.remote_connect_back     = on

This would cause everything to seem okay but somehow no debug connections were actually caught by PHP storm. Replacing the line above with the following instantly fixes stuff for me.

xdebug.remote_connect_back     = 0
xdebug.remote_host             = host.docker.internal

Of course, after that you still need to run: $ docker-compose down $ docker-compose build and $ docker-compose up -d

Note: on Linux host.docker.internal will not work. You can use 172.17.0.1 instead. (Note it is not 127.0.0.1, I always thought it was because I'm a bit dyslectic)

Jules Colle
  • 11,227
  • 8
  • 60
  • 67
  • 2
    that `host.docker.internal` bit...I can kiss you right now. – Chris Anderson Apr 19 '19 at 22:12
  • 1
    That only works on Mac or Windows, but not Linux where it is mostly used... I can't believe this kind of things... this make me angry... – matiaslauriti Feb 23 '20 at 00:17
  • I tried what I thought was the ip for host.docker.internal (172.17.0.1) and it didn't work; the host name sure does though! thanks! – Charlie Jul 27 '20 at 21:07
  • @matiaslauriti on linux this works for me: `xdebug.remote_host=172.17.0.1` – Jules Colle Jul 29 '20 at 14:02
  • @JulesColle still a no, that is your assigned ip for that docker... If I have to share that with a team, they will have to input their on IP... that is a no-no... – matiaslauriti Aug 20 '20 at 03:55
  • Sure, just saying it's working for me. I develop on 2 separate PCs, both running on Ubuntu. So as long as your team is working with linux or WSL, I guess this approach should work. – Jules Colle Aug 20 '20 at 09:40
  • I am not using any Docker or PHPStorm. Can you please help me here https://stackoverflow.com/questions/70092201/netbeans-xdebug-remote-host-waiting-for-connection-for-infinite-time – Gireesh Doddipalli Nov 24 '21 at 07:41
3

You will need set:

xdebug.remote_host=192.168.1.104

192.168.1.104 is the client's ip, where you working with the IDE

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • Thanks. However, I don't think this approach works when you have several IPs you want to be able to connect to the Xdebug remote server. See the comment above by Linus Kleen. – Gruber Aug 13 '12 at 09:12
3

Thanks for xdebug.remote_connect_back = On on server side php.ini
Also I had to use this plugin for Chrome to be able to start debugging session in PhpStorm

Dmitry Davydov
  • 987
  • 13
  • 21
3

If you are using xdebug 3, you need to know that some variables are renamed. Read this: https://xdebug.org/docs/upgrade_guide

php.ini configuration wich worked for me:

[xdebug]
xdebug.mode=debug
zend_extension="C:/wamp64/bin/php/php7.4.26/ext/php_xdebug.dll"
xdebug.idekey=PHPSTORM
xdebug.client_host= "localhost"
xdebug.client_port=9003

PHPstorm settings

1

Very correct answer by Brad for xdebug 3; Thanks... In my case I had Win 10 with Php7.3.13, Netbeans 8.2 and IIS 10(eindia.com is one among many websites ) (browsers are Chrome with netbeans extension & FF)... and no Xampp, Wampp, Lampp... Then I installed xdebug(using wizard) and config as below:

[xDebug]
zend_extension = "C:\Program Files\PHP\v7.3\ext\php_xdebug-3.0.2-7.3-vc15-nts-x86_64.dll"
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.remote_autostart=on
xdebug.remote_enable=on
xdebug.remote_handler="dbgp"
xdebug.client_host="eindia.com"
;xdebug.remote_host=192.168.1.5
;xdebug.remote_connect_back=1
xdebug.client_port=9003
;xdebug.remote_mode=req
xdebug.idekey="netbeans-xdebug"  

and its working like charm.

Saurabh
  • 1,545
  • 1
  • 9
  • 9
1

What page extension are you firing up to start debugging? I remember I went nuts and spent sleepless nights where all settings with XDebug are going great. The problem was I was not starting up with .PHP rather starting up with .HTML.

If you are then try starting up your debugging with .PHP file.

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
  • 1
    I have a .PHTML file as startup file. It is recognized by NetBeans as a PHP file, at least judging by its icon. I tried changing to .PHP, but it didn't work. Thanks for the tip though! – Gruber Nov 08 '11 at 12:18
0

When using Laravel Sail

If you're using Laravel Sail, you need to add the line below to your .env file.

# Enable XDEBUG (PHP Debugger)
SAIL_XDEBUG_MODE=debug

After adding this, restart sail using vendor/bin/sail restart.

Fore more information see the Laravel Sail docs.

Kerwin Sneijders
  • 750
  • 13
  • 33