Debugging Drupal Remotely with NetBeans and LAMP

Prerequisites: PHP, NetBeans, Linux Administration

We at Tarakan Design came to the web development world after spending many years in traditional software development. One of the tools we missed was a readily available interactive debugger for our PHP scripts, particularly for remote servers. Sure, there were PHP debuggers out there, but setup was marvelously complicated and results were, well, let's just say that our mileage did vary. Fortunately, setup is now simple if you use NetBeans and a LAMP-based development server.

For these instructions, I used NetBeans v6.8 and Ubuntu Server v9.10.

Configuring the Server

The first thing you have to do is install the Xdebug extension for PHP. The good news is that installation these days is a breeze. Just open an SSH session and install it with the aptitude package manager:

$sudo apt-get install php5-xdebug

Update: you may get an error (e.g., on 10.04 LTS) saying something about php5-xdebug not being available. In that case, edit the file /etc/apt/preferences.d/lucid and change the php5-xdebug line from "karmic-updates" to just plain "karmic"

This should create the file /etc/php5/cli/conf.d/xdebug.ini. Let's have a look inside. It should already have the Zend extension defined:

zend_extension=/some/path/to/xdebug.so

We just need to add a few lines to enable it to work with NetBeans remotely:

;Enable remote debugging
xdebug.remote_enable=1
;Select the debugger protocol (php3 or dbgp)
xdebug.remote_handler=dbgp
;Select when a debug connection is initiated.
; req tells Xdebug to try to connect to the client as soon as the script starts.
; jit tells Xdebug to only try to connect on errors.
xdebug.remote_mode=req
;Set the IP address of your local machine, not the server
xdebug.remote_host=192.168.1.9
;Set the port.
xdebug.remote_port=9000

Save the file and restart Apache

$sudo /etc/init.d/apache2 restart

That's it for server configuration. Now on to the client...

Configuring the Client

All you really have to do is make sure the client and server files are in sync and tell NetBeans where they are. An easy way to do this is to start with the site files on the server and create a new NetBeans project by importing them:

  1. Start NetBeans and create a new PHP project (File->New Project). Select PHP Application from Remote Server and click Next.
  2. Give the project a name and local location and click Next.
  3. Setup the Remote configuration as appropriate for your server and click Next.
  4. NetBeans will construct a list of files it will download. Click Finish to download the files locally.

When it's done downloading, NetBeans will bring up index.php from the Drupal root. Right-click anywhere in the file and select Debug File. The debugger will automatically stop on the first line. From here, you can set any breakpoints you need by opening the source file, right-clicking on the desired line and selecting Toggle Line Breakpoint (there are a few other ways of setting breakpoints as well). Via toolbar, hotkeys or the Debug menu, you also have familiar interactive debugger functions like run, step over, step into, step out, and run to cursor. At the bottom, you have panes for watches, variables and the call stack.

And with that, we've reclaimed interactive debugging for our Drupal development toolkit. The gods of productivity will be pleased.

Our Philosophy

 

  • Customer centered, Agile development
  • Clear and open communication
  • Solid software engineering principles

 

Drupal

Drupal

 

We use the Drupal Content Management Framework for secure and reliable websites.
Learn More