Using Xdebug with NetBeans 7

There comes a time where it's unacceptable or inconvenient to stash var_dump() into your code. And then there's Xdebug. Xdebug makes it easy to debug your PHP application and with the intergration of NetBeans, it's even easier.


For the record, I'm using Debian 6.0.4 on my workstation and Debian Wheezy/Sid on my dev server.

First of all, lets install Xdebug on the server:

pecl install xdebug

Open up your xdebug.ini file, mine is located at /etc/php5/cgi/conf.d/xdebug.ini, and enable Xdebug. My xdebug.ini looks like this:
zend_extension=/usr/lib/php5/20100525+lfs/xdebug.so

xdebug.remote_enable=1

xdebug.remote_connect_back=1

xdebug.remote_handler=dbgp

xdebug.remote_mode=req

xdebug.remote_port=9000

xdebug.idekey=netbeans-xdebug

xdebug.profiler_enable=1

 The reason why xdebug.remote_connect_back is enabled is because it allows multiple developers to debug. Also, if it so happens that your IP changes on your workstation, you won't need to update the remote host value. 

 Run php -m and you should see xdebug at the bottom of the output. Great, this means that Xdebug is working on your development server. Now to configure Netbeans. 

 

Go to one of your projects and select Properties. From there, go to Run Configuration > Advanced. You can select "Do Not Open Web Browser" but you'll have to manually enter the URL to start the session. It's best to select "Default" and have Netbeans open your browser with the XDEBUG_SESSION_START variable, starting the session.

Then, change the debugger host to the IP of your server and the port to 9000. Now, you'll also have to add Path Mapping. In short, the path on your server has to match the path on your workstation. See the picture below for reference.

 

In the example, the current project is located at /home/easto/web/reddit-api-client while the files reside on the server at /data/reddit-api-client. 

Once that is completed, let's test to make sure everything works. Open up one of your projects and from the top menu, go to Debug > Debug Project (or file). If all goes well, you'll now be able to debug your project. Click on Window >Debugging > Variables to show the current watched variables. If you right click on any variable and select "New Watch...", you'll be able to view the variable's state. See below for an example.

 



No more messy echo or var_dump()! Debugging with Xdebug is much easier and cleaner. The great thing about this setup is that debugging is handled all remotely you won't have to run a web server and PHP locally.