Many developers are unaware of the great power that Xdebug tool adds to the process of debugging a php project. In this article I will explain how to install and set up Xdebug for Ubuntu (and perhaps other linux distributions) users. After following the instructions you will be able to debug your php project whether you run it on your local Apache web server or via the command line ("cli"). The IDE is assumed to be NetBeans.

Download & installation

Download & installation of Xdebug is done by the following command:

pecl install xdebug

Configuration

To "tell" your Apache web server about it, append to /etc/php5/apache2/php.ini file (this location may vary under other distributions) the following lines:

zend_extension="/etc/php5/xdebug/xdebug.so" ; make sure this path exists in your filesystem. If not, find xdebug.so file and put its path here
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.idekey="netbeans-xdebug"

Open Netbeans and navigate to Tools --> Options --> PHP --> Debugging. Make sure the "Debugger port" & "Session ID" configuration values match the values of "xdebug.remote_port" and "xdebug.idekey" from the Apache php.ini file, respectively. In our example we set them to "9000" & "netbeans-xdebug".

Also, in your project properties select "Run Configurations" And make sure your project url is set in the right field.

Set some breakpoint at an interest point in your code by clicking on the line number in NetBeans.

Restart the Apache web server:

 sudo service apache2 restart

If you wish to be able to debug your project via the command line, add to /etc/php5/cli/php.ini file the same lines you added to /etc/php5/apache2/php.ini, and run the following commands:

sh -c "echo 'export XDEBUG_CONFIG=\"idekey=netbeans-xdebug\"' >> /etc/profile"
source /etc/profile

All that's left on NetBeans is to navigate to Debug --> Debug Project. Now you can go to your project by surfing to it using the browser or by running the specific php file in the command line. Notice your breakpoint should then hit.

Credits

Article is based on the following pages:

http://xdebug.org/docs/install
wiki.netbeans.org/HowToConfigureXDebug#How_to_on_Linux
http://stackoverflow.com/questions/2944880/debugging-php-cli-scripts-with-xdebug-and-netbeans

SIGN UP FOR OUR NEWSLETTER

Start receiving monthly updates on new features and the latest industry trends.

Please enable the javascript to submit this form