Auto setting xdebug.remote_host ip address with vagrant/puppet

此生再无相见时 提交于 2019-12-04 11:41:20

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18241065/auto-setting-xdebug-remote-host-ip-address-with-vagrant-puppet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!