Saturday, September 14, 2013

Setup for Debugging PHP code on Mountain Lion OS X with XAMPP and PhpStorm

I faced hard time configuring system to debug PHP code on localhost. Thought of archiving it before I forget! I started out with this very useful gist: https://gist.github.com/makangus/3816880 but there were few changes in the context of Mountain lion and XAMPP.

Xdebug is shipped along with Mountain Lion, so the new MacOSX users need not download it. We have to configure the system to use it.

1. Configuring the php.ini file to enable Xdebug: Usually we find the php.ini file in '/etc/php.ini'. But if you are running XAMPP to create a local webserver, make sure that you edit the php.ini file in the location: '/Applications/XAMPP/xamppfiles/etc/php.ini'. Add the following lines at the end of your php.ini file:

[Xdebug]
zend_extension=/Applications/XAMPP/xamppfiles/lib/php/php-5.3.1/extensions/no-debug-non-zts-20090626/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

Please note the location of xdebug.so file. The Mountain Lion is shipped with Xdebug and it's default location is "/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so" but I suppose when we install XAMPP, it moves a copy over for itself in the location "/Applications/XAMPP/xamppfiles/lib/php/php-5.3.1/extensions/no-debug-non-zts-20090626/xdebug.so". This could be confirmed by checking the phpinfo's extension folder location:


All set, now run this php script in your local webserver to confirm that Xdebug is setup:

<?php phpinfo(); ?>

Now to you should have a dedicated Xdebug section in the phpinfo:




 2. Now we need to setup the PhpStorm with the root folder of the PHP scripts which we want to debug. It's easy to create a project with all the files of your root folder containing the PHP code:



Next comes configuring the project for the PHP web application:

Go to Menu Run > Edit Configuration

Next, click the '+' button to add a new PHP web application:


Now configure the root folder for your PHP resources in this newly created Web Application:


Now setup the server for this project:


This is the setup for PHPStorm. Now, if you could open your index.php in the navigator and place a breakpoint you can confirm it. When you click on the 'debug' menu, a URL is triggered in default web browser something like:

http://localhost/your_web_folder/cms/?XDEBUG_SESSION_START=18601
PHPStorm supposedly uses this Xdebug session for debugging purpose. If you want to test and debug webservices, that is possible using the same debug session:

http://localhost/your_web_folder/cms/web_service_api_endpoint/node.xml?XDEBUG_SESSION_START=18601

All this done saying, now I have to start debugging and resolve my actual problem!

PS: I am a beginner and am eager to learn new things, there maybe some errors in this post and also I may have gone wrong in my understanding. I would welcome people correcting me and giving their views. If you have questions I will try my best to address them.