Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save erickpatrick/84da06a32b37d65f4f2a3adb27f391c2 to your computer and use it in GitHub Desktop.
Save erickpatrick/84da06a32b37d65f4f2a3adb27f391c2 to your computer and use it in GitHub Desktop.
Making xdebug work with Vagrant, PHPStorm and Laravel Homestead on Windows

If you, like me, use a development machine provided by the company It department, it will probably be Windows based due security policies enforcement or something on these lines. That's ok, many of the new Windows based machines are really good and sturdy, and now, with the Windows Subsystem for Linux it can even work better for development that is not related to C#, .Net or related technologies.

The environment

However, when working with PHP and Laravel, we already have a nice way to run our code, inside the Vagrant VM from Laravel itself, Homestead. It comes with a bunch of software preinstalled that a Laravel application (and developer) may need at anytime during its/her life and, for the most part, it works out of the box.

The problem

When I say for the most part, I mean the main subject of this post: debugging. For some strange reason, when running Homestead under Windows, it simply does not work out of the box. It's not a matter of turning Xdebug on (the default debbuging extension bundled, which comes disabled), restart the server (which the shell aliases/functions already present on the VM do for us) and we are ready to debug the app. Unfortunatelly, it's a little bit more complicated.

The solution

Luck you, I walked this lonely road (if you got the reference, high five for you) brought all the steps necessary to make it work like a charm.

So, just to be 100% on the safe side, I'll assume you already have a Homestead installation working on your machine and you need/want to make debuggin work. If not, head to the Laravel Homestead Documentation and come back when you're finished.

Without further ado, here is the guide.

Port forwarding for a site (or a new one)

When adding a new site to your Laravel Homestead installation, don't forget to add the following port forwarding to your Homestead.yaml:

ports:
    - send: 9000
      to: 9001

send: 9000 is the port from your Host Operating System (be it Windows, Mac or Linux machine) and to: 9001 is the port from your guest Operating System (the virtual machine you want to connect to). We set the guest Xdebug port to 9001 due to PHP running on Laravel Homestead in the port 9000 which would cause a conflict and not allow Xdebug to proper receive and/or return data of the debugging session.

Proceed with the start up of the VM. Once logged in (perhaps, after homestead ssh if you have followed the tips/tricks from Laravel Homestead Documentation), run route -nee. You will see an output much like:

Xdebug connection configuration

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface    MSS   Window irtt
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 eth0     0     0      0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0     0     0      0
10.0.2.2        0.0.0.0         255.255.255.255 UH    100    0        0 eth0     0     0      0
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1     0     0      0

The first line, with the destination 0.0.0.0 is the host system. We need to use the if gateway value as the remote_host configuration of Xdebug. This way, if you edit the file /etc/php/VERSION/fpm/conf.d/20-xdebug.ini, switching VERSION by the PHP version you're using in your applications, you should have something like this:

zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_host = 10.0.2.2
xdebug.remote_port = 9000
xdebug.max_nesting_level = 1000
xdebug.remote_log = /home/vagrant/var/log/xdebug.log

Pay attention that we dont have xdebug.remote_connect_back turned on as this will transform our host gateway address into something like 192.168.10.1 and listen the port 9000. As Xdebug receives data on port 9001 it can safely send data to the port 9000 that will, in turn, forward data to the port 9000 of the host operating system that will have PHPStorm listening to be able to pause on our breaks on the code.

PHPStorm configuration

Now, the PHPStorm configuration. Go to File > Settings > Languages & Frameworks > PHP > Debug and ensure that Xdebug > Debug port is set to 9000 as well.

PHPStorm breakpoints

After all this done, we can start debbuging our application by setting breaking points through IDE, by clicking by the side of the line numbers of the code. When a breakpoint is set, a red circle will appear. And after setting the breakpoints we can press the button Start Listening for PHP Debug Connections which will cause PHPStorm to listen to connections from Xdebug from Laravel Homestead.

@mkambeck
Copy link

After hours and hours of researching this was the solution to my problem. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment