7

I'm in the process of setting up a Vagrant environment using puppet for provisioning.

I'm stuck with one issue, I would like xdebug to 'just work' when running vagrant up however I need to specify the host machines ip address in the php.ini file xdebug.remote_host, obviously this is going to be different on each machine the config is used so I would like a way to automatically update that value when issuing vagrant up.

VagrantFile:

config.vm.network :forwarded_port, guest: 9000, host: 9000

.ini settings:

'xdebug.default_enable=1',
'xdebug.remote_enable=1',
'xdebug.remote_handler=dbgp',
'xdebug.remote_host=localhost:9000',
'xdebug.remote_port=9000',
'xdebug.remote_autostart=0',
'xdebug.max_nesting_level=250'

I have also tried it with xdebug.remote_host=localhost

ifconfig results from the vagrant machine:

vagrant@precise64 ~ : ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:cf:f9:89
      inet addr:192.168.61.142  Bcast:192.168.61.255  Mask:255.255.255.0

phpinfo()

REMOTE_ADDR 192.168.61.2

REMOTE_PORT 51886

Just to confirm, if I give remote_host my actual ip address I have on my osx host machine, it works correctly.

tobias47n9e
  • 2,233
  • 3
  • 28
  • 54
greg
  • 6,853
  • 15
  • 58
  • 71
  • See http://stackoverflow.com/questions/19917148/tell-vagrant-the-ip-of-the-host-machine – Tgr Jan 30 '15 at 23:24

2 Answers2

11

As mentioned in http://www.xdebug.org/docs/all_settings, you can set option

xdebug.remote_connect_back = 1

So, xdebug will connect back to the host, which requested for web-page, and will ignore option "remote_host".

This solution has one problem: If you enable xdebug for any request, and user, opening web-page doesn't have running xdebug client (waiting for connection from server), and has non-closed 9000 port, the server would wait for a long time (trying to connect to client's xdebug session), before it can finally load page. I had this problem with windows 7 machines, because it's firewall doesn't actually closes port, and connecting software can't understand, that nobody is listening port.

If this doesn't work:

I had the same situation, then I was need for VirtualBox VM with configuration, that should work on any machine with any IP. So, I made it this way:

  1. I've created virtual network interface in VirtualBox (I don't know, are there any options for this in vagrant, but it should be), and set it local address to 192.168.100.1, so, my REAL machine have two addresses: eth0:192.168.1.2, and vboxnet0:192.168.100.1.
  2. I've configured virtual machine with following values: IP=192.168.100.100, Default gateway = 192.168.100.1
  3. Configure my XDebug to remote_ip=192.168.100.1

And now, I have 3 copies of this machine (my copy, and 2 copies used by my co-workers), and it works fine!

So, solution is to set your IP address to some "constant one", just virtually.

MihanEntalpo
  • 1,952
  • 2
  • 14
  • 31
  • I did try that but it didn't work. I took a look at the remote_host super global and the i.p was of the vagrant guest machine that PHP was running on, not my host machine. – greg Aug 22 '13 at 23:27
  • Ok, then you should try this: 1) Configure your xdebug to use localhost:9000, so it will connect to a virtual machine' port, and 2) then forward that port to your real machine – MihanEntalpo Aug 23 '13 at 05:42
  • Still no luck. Updated the question with my config. – greg Aug 23 '13 at 18:40
  • I noticed that the REMOTE_ADDR on from phpinfo is `192.168.61.2` yet in OSX (host machine) the vmnet ip's from ifconfig are `192.168.213.1, 192.168.56.1, 192.168.61.2` – greg Aug 23 '13 at 19:20
  • Updated answer with new info – MihanEntalpo Aug 24 '13 at 19:25
0

In case of a custom vagrant machine running GNU/Linux or using a docker image you can find the ip via the following command:

netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10

To get the ip, tun this command after you run first:

vagrant ssh #login into the machine

BONUS INFO in case you are or wanna run docker inside vagrant

Also an intriguing case is the one that you run php inside a docker container that is spawned inside a vagrant GNU/Linux image. In that case, do not attempt to shawn a shell of a running php image and the run the command above, find the ip that the VM connects back into the host, via running the command:

netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10

In the running VM's shell and NOT on a running docker container (either using docker exec or docker-compose exec). In case of an entrypoint script set an enviromental variable for the XDEBUG ip and autoset the value in case that this one is empty then autoset the ip.

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164