Remote PHP debugging via virtual machine and Netbeans

I'm going to discuss a way to setup remote debugging via a virtual machine. You may need to do this if your host machine is windows and your dev environment requires tools available only to Linux or perhaps your lack admin rights to your Windows host machine and the only thing available to you is a VM, etc.

Anyway I won't go into details on how to setup the web server itself or the VM I'm going to focus primarily on how to funnel requests to your VM and then perform remote debugging through your VM from your Windows IDE (Netbeans in this case). I will also be using VirtualBox in this scenario as that's what I'm most familiar with and it's free.

Once your web server is setup the way you want you're going to need to setup port forwarding so that any requests to a certain port (80 for instance) is sent directly to your VM instead. To do that you're going to want to use NAT for your network interface in your VM settings.

NAT port forwarding

This is the network settings. Click the port forwarding button. Your Apache port forwarding settings in NAT will most likely look like this:

Apache port fowarding settings

You may also need to update your hosts file to reflect any host names you may have setup. You can find much more information about that here.

Ok, next you're going to want to configure your XDebug settings of your Guest VM to work with Netbeans. Via NAT it will most likely look something like this:

xdebug.show_local_vars=0
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=10.0.2.2
xdebug.remote_port=9000
xdebug.idekey="netbeans-xdebug"
xdebug.remote_autostart=1

These are basic settings but it's a start. You may need to add more, particularly to enable profiling in the future. Pay close attention to that xdebug.remote_host - why does it point to that IP? This is the default gateway for NAT. It is most likely that same IP but in case it's not you can verify by running a command like ip r the output will look like this:

Default gateway

The IP after "default via" is your default gateway. Set it to that. I didn't run this from my VM but you get the idea.

If you were to turn on your debugger via Netbeans and make a request you should notice that the IDE detected the connection but perhaps your breakpoint didn't work. Why? At this point your server is correctly making connections to your host IDE but the path of the files do NOT match. Your IDE won't know which files are being ran when. You're going to need to setup a server path.

Right click your project and go to Run configurations. Click Advanced.

Run configurations

Ignore project URL, put whatever is relevant to your project.Server paths

Then you should see this prompt:

Click the "..." in the Project path - and search for your project files. If your project is located at C:\Users\test\Documents\my_project and in your VM you have a shared folder located at /var/www/my_project then you will need to set this path in the Server path. Once that's done your IDE should now use breakpoints.

Hope this helps!