Sunday, February 21, 2010

Debugging PHP with NetBeans on Mac and Linux

As you probably know, I recently started working with Drupal, with lots of help from the Pro Drupal book. In the book (page 524), the author states:

Real-time debugging is a feature of PHP and not Drupal, but it's worth covering, since you can easily be identified as a Drupal ninja if a real-time debugger is running on your laptop.

In my relentless quest for Drupal ninjahood, I naturally wanted to get debugging working with NetBeans, my PHP editor of choice. Just kidding ... the need to get debugging working was driven by the relatively opaque nature of Drupal - the only two debugging features I am aware of are the watchdog logging feature, which I have used, and the Drupal Devel module, which I have not (yet). Also, like most Java programmers, I am probably more dependent on a good IDE than most PHP programmers.

In any case, this post talks about what needs to be done to get PHP debugging working on NetBeans on Linux (CentOS) and Mac (Snow Leopard). The information was gleaned from multiple posts, some of which provided inaccurate or incomplete information, so there was some amount of trial and error involved.

I will assume that you have NetBeans installed and you occasionally use it. I use NetBeans only for scripting, and so far, the lack of a debugger (or my lack of knowledge on how to run it) has not affected me. With Drupal, however, there is no real way of knowing what module(s) are getting called in a request, save from writing watchdog calls in your code, so having a debugger to step through the code can be quite helpful.

This part probably doesn't matter unless you are also doing Drupal development, but there appears to be a Goldilocks syndrome thing going between Drupal 6.15 and PHP 5.2. My CentOS 5.3 has PHP 5.1.6 in the default yum repository. Apparently that doesn't quite cut it with Drupal, so I initially enabled Remi's repository based on information in Binit Bhatia's post - but that now gives me PHP 5.3 (not surprising, since Binit's post is almost a year old now), which has even bigger problems with some of the modules. Ultimately, I ended up getting the 5.2 from the CentOS testing repository following guidelines in Irakli Nadareishvili's post.

On the Mac, I am using MAMP, which has PHP 5.2 installed as a component within MAMP (ie, under /Applications/MAMP/bin/php5/bin), even though at the OS level it has PHP 5.3 installed (ie, under /usr/bin).

So anyway, what you need to do is to install and configure XDebug to work with PHP and NetBeans. On CentOS, just download the source and build it as explained in the XDebug install page.

sujit@lysdexic:xdebug$ phpize
sujit@lysdexic:xdebug$ ./configure --enable-xdebug
sujit@lysdexic:xdebug$ make
sujit@lysdexic:xdebug$ sudo cp modules/ /usr/lib64/httpd/modules/

On the Mac, compiling from source did not work, neither against the PHP 5.3 in /usr/bin nor the PHP 5.2 packaged with MAMP. It fails on make, and it looks like a bad #ifdef in the code somewhere. However, I was able to get it by downloading the PHP Remote Debugging package from the ActiveState Komodo Site and copying the file under the 5.2 directory to /Applications/MAMP/bin/php5/lib/extensions/no-debug-non-zts-20060613/ - most of this information came from Felix Geisendörfer's post.

The next step is to hook XDebug with PHP and NetBeans. Thankfully, this is (almost) identical on both Mac and CentOS. Essentially, the following lines need to be added to the php.ini file (/etc/php.ini on CentOS and /Applications/MAMP/conf/php5/php.ini on Mac). The information below mostly comes from the Debugging PHP Source Code in the NetBeans IDE NetBeans article, although some of it has been changed using information from other posts.


In addition, remove/comment any zend_* properties from the php.ini file. Apparently XDebug and Zend don't go well together.

Restart your webserver and bring up a page with phpinfo() and verify that XDebug is available, or use php -m to list the modules as specified in the XDebug install page. If XDebug seems to be installed okay, the next step is to check if NetBeans can see XDebug.

To verify this, start NetBeans, open an existing PHP project (such as a Drupal application), then click the debug button. You should see a "Connecting to XDebug" message on the status bar which should change to "netbeans-xdebug" within a few seconds. At this point, you can debug a request by clicking a URL on your browser and stepping through the code, setting breakpoints, inspecting variables, etc on NetBeans.

6 comments (moderated to prevent spam):

jagdeep said...

Hi Sujit,
I am regular reader of your blog.
Thanks for sharing your knowledge!
Keep it up....

Sujit Pal said...

Thanks, Jagdeep.

Hemant Patel said...

Hi Sujit,

Glad to know you are slowly moving to Netbeans :-). To be honest, I have never missed any of your blog, share with my friends even with proud, Thanks for knowledge sharing.

Sujit Pal said...

Hi Hemant, hope you are doing well. Actually, I have kind of standardized on Netbeans for scripting stuff, because most of the time all I need in there is a good code editor, and Netbeans has good support for new languages via its Schliemann framework, and most of the time Netbeans ends up supporting new languages sooner than Eclipse. However, I still use Eclipse (with MyEclipse) for Java/JSP development. This is actually the first time I had to do something more with Netbeans than code editing (small scripts are easy to test by running them, but not so with Drupal) - my take is that the debugging setup with XDebug/php/netbeans is usable, but only barely.

Anonymous said...
here you may download a VM all pre-configured.

Sujit Pal said...

Thanks Anonymous, your work should be very useful for someone needing this functionality.