Auto setting xdebug.remote_host ip address with vagrant/puppet

折月煮酒 提交于 2019-12-03 07:13:59

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.

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.

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