Software and Other Mysteries

On code and productivity with a dash of unicorn dust.

Xdebug in Netbeans on a MAMP Setup

I have been doing a lot of PHP development for my bachelor thesis over the last few months. We are working in a team of six and it is not always trivial to keep track of all the changes to the code, and debugging code you are not all that familiar with is a pain in the ass. I am coding in Netbeans 7.0 beta 2 with MAMP Pro 1.9 taking care of the server. To resolve my debugging issue I decided it was time I got Xdebug into the mix. It should also be noted that I’m using PHP 5.3, but I believe all of this should work just the same for 5.2 if you just replace the version number wherever it appears.

MAMP actually comes bundled with Xdebug, though it is not enabled by default. The first thing I did was to upgrade the version of Xdebug to 2.1.0. I don’t think this is required, but it sure didn’t hurt. Just download the PHP Remote Debugging Client from the the boys at ActiveState and replace the xdebug.so in your /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626 folder (the date on the end might be different, but just go with what you find!) with the one unzipped from the downloaded file.

The next step is to enable Xdebug which we do in the php.ini file. Open up MAMP, select File -> Edit Template -> ‘PHP 5.3.2 php.ini’ from the menu and modify the end of the file so that it looks (somewhat) like the one below. This is just an example however, and these INI-settings are basically a mash-up of all the tips I found while googling the matter. Especially note that the xdebug.default_enable, _ xdebug.remote_autostart and xdebug.idekey_ settings are not mentioned in the NetBeans Wiki’s guide, so it might be that they are not required. Also be really sure the Zend Optimizer line is commented out, since it apparently interferes with Xdebug.

1
2
3
4
5
6
7
8
9
10
11
12
13
[xdebug]
xdebug.default_enable=1
zend_extension="/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.idekey="netbeans-xdebug"

[Zend]
;MAMP_zend_optimizer_MAMP

After this step I wasted a lot of time on trying to find out why NetBeans couldn’t connect to Xdebug. Finally I rebooted my computer and everything was suddenly working. My best guess is that this was due to a conflict on port 9000 (possibly with Skype). Feel free to try it out with only restarting the MAMP server but remember to do a full reboot if things still aren’t working.

By now you should be up and running. NetBeans debugging functionality is pretty straightforward, however you might want to check out the official documentation to get started. Hope this saves someone else a few hours, and feel free to share your tips in the comments!

Comments