Quantcast
Viewing all articles
Browse latest Browse all 27

PHP Xdebug + Netbeans + Vagrant

Image may be NSFW.
Clik here to view.
Xdebug is a very nice extension for PHP. It makes step by step debugging possible in your IDE. For most people it’s easy to install Xdebug and start debugging their web application on localhost with some IDE, for example Netbeans. Because after installation of Xdebug, it just works.

But getting Xdebug remote to work if your website runs on a virtual machine / Vagrant box, it’s a bit trickier and requires a bit more configuration.

First let me explain how the Xdebug flow works. In my /etc/hosts file on my host machine I added the following line to point that example.local to the IP of my virtual machine / Vagrant box (The Vagrantfile contains config.vm.network :hostonly, “192.168.33.10″):

192.168.33.10 example.local

In my browser I open the following URL which will point to the virtual machine:

http://example.local?XDEBUG_SESSION_START=netbeans-xdebug

By opening the URL, the web server on the VM creates a connection with Xdebug to my IDE on my host machine. The Netbeans debugger listens on port 9000 by default, and Xdebug connects to that.

Configuration

Let’s make it work! Add the following settings to xdebug.ini:

xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=192.168.33.1
xdebug.remote_port=9000
xdebug.remote_autostart=0
xdebug.remote_log=/tmp/php5-xdebug.log

The Vagrant box runs on the IP 192.168.33.10. Vagrant gave my host machine the IP 192.168.33.1. So xdebug can reach my IDE on my host machine with the IP 192.168.33.1.

Restart your webserver or php5-fpm, check with phpinfo() if your configuration is really changed.

Give it a try

Click on the debug button in Netbeans or press Ctrl + F5 to start the Netbeans debugger and start listening on port 9000.

Open in your browser the URL:

http://example.local?XDEBUG_SESSION_START=netbeans-xdebug

Check on your virtual machine /tmp/php5-xdebug.log if the xdebug could connect the host machine on port 9000. It is possible that you get this error:

I: Connecting to configured address/port: 192.168.33.1:9000.
E: Could not connect to client. Image may be NSFW.
Clik here to view.
:-(

Sometimes if you enable/disable debugging in Netbeans a few times and you enable debugging again, Netbeans says it’s listening but it just isn’t. You can check with the following command on your host machine if something is listening on port 9000:

netstat -tln

Just close Netbeans and start it again and it should listen to port 9000 again when you click on the debug button.

Path mapping

At this moment almost everything works, but step by step debugging probably still fails. Click the right mouse button on your project in Netbeans and choose Properties -> Run Configuration -> Advanced.
Set Debug URL to “Do Not Open Web Browser”. And set the correct path mapping, I set the following:

Server Path          Project Path
------------------------------------------
/vagrant             /var/www

It also helps to take a look again at /tmp/php5-xdebug.log on your virtual machine to see on which path the PHP file is that Xdebug opens, caused by the call in your browser. Step by step debugging should be working when the correct path mapping is set.

This should be all! Developing virtually should be just as easy now as developing locally! Enjoy!


Viewing all articles
Browse latest Browse all 27

Trending Articles