Debugging PHP Sites with Netbeans and XDebug

By Paulus, 5 June, 2010

Prequesists and Assumptions

This HowTo/Tutorial assumes the following you have:

  • Basic Knowledge of Gentoo or Fedora (ie, installing and editing files)
  • Working local Apache Server with basic knowledge of said server
  • General knowledge of Netbeans
  • Netbeans installed

The following is requred:

  • Netbeans 6.8 with PHP
  • If using gentoo, have the proper USE flages set. the 'apache2' USE flag on dev-lang/php is required for Apache to parse php files.

Introduction

One of the things that I was never taught was how to debug an application or website. I will be the first to admit that for a long time I avoided using a debugger like the plague but as programs and websites get more and more complex, using a debugger definitely helps tracking down problems and seeing what's really going on.

Setting up XDebug

Getting XDebug installed is deathly simple now. Instead of having to manually download the debugger and place it in the extensions directory, modify the php.ini file, and pray PHP sees it all you have to do is run the following commands.

For Gentoo:

# emerge dev-php/xdebug-client dev-php5/xdebug 
# /etc/init.d/apache2 restart

One thing I found with Gentoo is that I had to make a minor change to the /etc/php/apache2-php5/ext/xdebug.ini file. I had to change:

xdebug.remote_enable="0"

to:

xdebug.remote_enable="1"

After doing this, you will be able to debug.

For Fedora:

# yum install php-pecl-xdebug
# service httpd restart 

That's it. To see that it worked properly run the following command:

$ php -m

You should see this:

[Zend Modules]
Xdebug

Configuring Net Beans

Typically the default options in Net Beans will get you on your way to debugging your PHP code. However, if you needed to change the port in which xdebug uses you will need to let Net Beans know it needs to use a different port. Go to Tools -> Options and click on the button that says PHP

  • Change the Debugger Port to the port you specified in the xdebug.ini file.
  • The Session ID is the value that the XDEBUG_SESSION_START variable needs to be set. If you want to debug http://localhost/test.php then append ?XDEBUG_SESSION_START=netbeans-xdebug
  • Checking where it says 'Stop at First Line' will stop at the first line of code that gets executed.

Click on Debug->Debug Project to start the debugging.