Friday, May 14, 2010

Debugging PHP with Eclipse on Ubuntu

I have followed the instructions at the site below but there are many differences I found when I was setting up PHP debugging using XDebug with Eclipse on Ubuntu 10.04. So I wanted to take you through the steps that worked for me.

Older/incomplete instructions are found here:
http://www.phpeclipse.com/wiki/Howto/XDebugAndPHPEclipse


Program installation
First you will have to install the following (we use postgres and smarty so that may not be required for you):

eclipse, php5, php5-pgsql, apache2, smarty, zendframework, php5-xdebug

you can run this:
sudo apt-get install eclipse php5 php5-pgsql apache2 smarty zendframework php5-xdebug

In eclipse add a new update site in Help->Install New Software
Click the Add button and enter this site url:
http://update.phpeclipse.net/update/stable/1.2.x

Select the root check box to install all components of the PHPEclipse plugin

XDebug set up

Edit xdebug.ini:
sudo nano /etc/php5/apache2/conf.d/xdebug.ini

find a line that looks like this (or you may need to add one):
zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so

Add the following lines for a local debug session:
xdebug.remote_enable=On
xdebug.remote_autostart=On
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_mode=req


Apache Configuration

Edit apache2.conf settings:
sudo nano /etc/apache2/apache2.conf

Add an alias and directory for your project (this is only for testing):
Alias /project_name "/home/your user/workspace/project directory"
ServerName test.net
< Virtualhost *:80>
DocumentRoot /home/your user/workspace
ErrorLog /error.log
CustomLog /access.log common
</ Virtualhost>

If you do not have ErrorLog in here, the error long is located:
/var/log/apache2/error.log
This is defined in another section in the apache2.conf file as well.

Restart Apache:
sudo /etc/init.d/apache2 restart


Set up Eclipse remote debug session:

Click on this link and follow the instructions in section XDebug Debug Profile

Set a break point by opening the file with the Eclipse php editor and right clicking in the left margin next to line you want to break on. Then select "Toggle XDebug Breakpoint"

To start the debug session go to:
http://localhost/your_prj_sub_directory/?XDEBUG_SESSION_START=ide_id_string

Navigate to the place in your site that would execute the code with the breakpoint and start debugging.

The local variables can take some time to show up after you go to that tab. And if you run into trouble (null pointer exception) with the custom expressions (i.e. watch expressions), then remove them. The PHPEclipse plugin does not seem to be very robust in the way it handles any exceptions your php expression may throw while you are stepping through.

Some times Eclipse (or the PHP plugin) gets a little flaky.
When this happens try:
  1. refresh the page

  2. "hard refresh" (ctr-F5)

  3. restarting the PHP XDebug Remote Script

  4. all else fails you will have to restart eclipse

For more configuration options here are some important file locations for Ubuntu 10.04:
php.ini: /etc/php5/apache2/php.ini
httpd.conf: /etc/apache2/httpd.conf

If there is anything that I missed please let me know and I will update these instructions.

Hope this help.