Getting PHP XDebug, CodeIgniter and Netbeans to all work nicely together

Posted: August 26th, 2010     Tip   Web Development

After coding with CodeIgniter for the last 8 months, I’ve reached a point where a serious debugger is actually going to come in handy (it’s hard to believe I lasted that long using print_r and echo statements!). A real debugger gives you stack traces, function traces, parameter/variable display, break points, watch variables and all that goodness. The problem is that I’ve never been able to get a debugger actually operational and usable with my dev environment and CodeIgniter’s URL rewrites… until this morning.

My dev setup is like this: I use Netbeans IDE on my Windows Workstation. I always push and test on a cloud server at Rackspace Cloud running Ubuntu, so that my clients can see and test whatever I’m doing for them. I use CodeIgniter and PHP. I usually use Chrome as my browser. In this case, we’re going to install and run XDebug.

Getting and Installing XDebug

Thankfully, Ubuntu has an amazing utility called ‘apt-get’, which lets you simply type in ‘apt-get install php5-xdebug’ to install the xdebug package. If you’re using a different OS, you’ll need to figure out how to install XDebug. On Mac OS X, you can just use MAMP and uncomment the XDebug line in the php.ini file, and follow the instructions below to add remote debugging.

Back to my Ubuntu setup. The Ubuntu “apt-get” installer will put the xdebug config file in: /etc/php5/apache2/conf.d/xdebug.ini

If you’re compiling xdebug or using some other way of installing it, you just need to find out where the install is calling the xdebug PHP extension. It’s usually in php.ini.

Open the config file and add the following after this line:

zend_extension=/usr/lib/php5/20090626/xdebug.so    //This should already be in...
/** Add these lines **/
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host= {YOUR HOSTNAME OR IP ADDRESS ** SEE NOTE BELOW}
xdebug.remote_port=9000

If you are running your test server on your local workstation using MAMP/XAMPP or whatever, just put in ‘localhost’ into the remote_host. If you’re like me and running it on the internet on network, you need to get the IP address of your computer.

Test that it’s working: restart apache, make a script called phpinfo.php and add in:

<?php
phpinfo();
?>

Run that in your browser. You should see a whole section on XDebug, with the settings you included above, that verifies that XDebug is working.

Tricky/optional bit for people wanting to run this remotely…

If you’re running on a network with a router and subnet, you’ll need to find your public internet address and be able to forward packets on port 9000 to your workstation.

What I did, was logged into my Netgear router to get my public internet address. Because my workstation is on a subnet, you need to do port forwarding of packets coming in on port 9000.There should be a “Port Forwarding” section where you can add port 9000 and have all communication forwarded to an IP address (the address of your workstation).

Because I’m running a debugging session remotely, there is a likelihood that my ISP will give me a different IP address later. That simply means that I have to change the xdebug.ini (or php.ini) setting again to point to my new IP address.

Netbeans

In your project properties (right click on the project -> properties), click on Run Configuration, look for the “Advanced…” button and set “Do Not Open Web Browser“. Click OK.

Chrome or Firefox

You need the “Xdebug helper” extension/add-on. Install that from within your browser.

If you’re using the Xdebug helper in Chrome, you may need to go into its options and add the domain that you want to debug, so that it will show you the debugger and profiler button. See this.

Debugging

In Netbeans, open a file like a Codeigniter controller or something. Add a breakpoint somewhere interesting. Press CTRL+F5 to start debugging. Switch to Chrome, go to the URL of the controller that you’re debugging. It should automatically switch back to Netbeans with index.php open and the program paused at the first line of code to say “I’m ready to debug!” Click on Continue (or press F5) and it should bring you to your breakpoint, where you can observe all your variables, call stack, etc.

And developing in CodeIgniter suddenly became even more awesome….

Notes and Erratta

Expression Engine and XDebug don’t like each other! Turns out that there is a piece of code in EE that causes a segmentation fault. Apparently newer builds of XDebug have resolved this bug but on my latest apt-get of XDebug, I’m still having issues.

 
Leonard Teo
CEO, Ballistiq
Montreal, Canada