This is a straightforward guide on how to set up and use Xdebug 2.1.1 with webgrind 1.0 for PHP Performance Profiling on Mac OS X 10.6 Snow Leopard. Xdebug is a powerful debugging and profiling tool for PHP. While, webgrind is a web-based alternative to kCacheGrind, which allows you to see bottlenecks in your code.

Preparation
This tutorial assumes that you set up PHP using the directions in my article, How to set up Apache, PHP, & MySQL on Mac OS X 10.6 Snow Leopard. For Leopard instructions see: How to set up Xdebug with webgrind for PHP Performance Profiling on Mac OS X 10.5 (Leopard). Both these tutorials should still work for other setups if you correct the paths used in my examples below; if this is you make sure you know what you’re doing. And, as always make sure to backup any and all files I have you modify!

Open up the Terminal application in /Applications/Utilities

I will be using the GNU nano text editor in my examples since it already comes with the Mac, but if you’d like to use TextMate or BBEdit just replace nano with mate or bbedit respectively and remove any nano specific options (e.g. +n or -w).

Whenever I ask you to type something into Terminal you have to hit the return key for the command to go through. Alternatively, for you lazy ones out there, you can just copy and paste these commands.

Installing Xdebug
Go to the Terminal window and type:
sudo pecl channel-update pecl.php.net
You should receive the following message:
Update of Channel "pecl.php.net" succeeded

When you put sudo in front of a command you’ll need to enter your root/administrator password if prompted to do so.

Now type:
sudo pear channel-update pear.php.net
Check for this message:
Update of Channel "pear.php.net" succeeded

Time to install Xdebug:
sudo pecl install xdebug
Once this command finishes executing you should see something like the following:
Build process completed successfully
Installing '/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.1.1
configuration option "php_ini" is not set to php.ini location
You should add "extension=xdebug.so" to php.ini

It’s time to tell PHP to use the Xdebug extension. In Terminal type:
sudo nano -w /etc/php.ini

Hold control vuntil you reach the end of the php.ini file, and then copy and paste the text below:
[xdebug]
zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.remote_enable=1
xdebug.profiler_enable_trigger=1

Please note Xdebug has a lot of configuration options. The options above are meant to work with the Firefox Xdebug Helper add-on and webgrind, which we’re going to guide you through setting up next. For more on available config options and what they do check out Xdebug’s Documentation. The documentation is also good if you don’t know how to use Xdebug to debug PHP :P .

Something extra: If you have TextMate you’ll want to add this text to the end of the php.ini file:
xdebug.file_link_format = "txmt://open?url=file://%f&line=%l"

This makes error messages clickable and furthermore when you click on one of the links it will open up the problem file and go to the line number containing the error in TextMate.

When you’re finished making changes to the php.ini file hit control x to exit, then type y, and hit return to save your changes.

To complete the setup of Xdebug all you have to do is restart Apache. So, go to Terminal and type:
sudo apachectl restart

Finally, lets make sure Xdebug is installed. Create a file called info.php (or something similar) and put the following code in it:

<?php phpinfo(); ?>

Now visit the newly created PHP file in your web browser (it should be somewhere like http://localhost/info.php) and it should have a section that reads something like this:
This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.1.1, Copyright (c) 2002-2011, by Derick Rethans

Can’t find it? Here’s a screenshot:
phpinfo output if Xdebug is installed properly

If you really can’t find the text above do a search for “Xdebug” in your browser (normally you can do this by hitting command f.

Installing webgrind
The way we have Xdebug configured we’ll want to use webgrind in conjunction with a Firefox add-on called easy Xdebug, so go ahead and install/add it to Firefox. You’ll be using Firefox with webgrind so make sure you restart Firefox before proceeding.

Now, this next part is super easy. Go back to your Terminal window and type:
cd ~/Sites/
curl -O http://webgrind.googlecode.com/files/webgrind-release-1.0.zip
unzip webgrind-release-1.0.zip

Installed. I told you it was super easy.

Now, webgrind should be accessible at: http://localhost/webgrind/ or something like http://localhost/~ariadoss/webgrind/ depending on how Apache is setup on your Mac. In this particular example ariadoss would be your computer’s short name. So replace ariadoss with whatever yours happens to be, you can easily tell what it is from the Terminal cause it’s the name right before the dollar sign (e.g. Danilo-Stern-Sapads-MacBook-Pro:~ ariadoss$).

To start a new Xdebug session and the Xdebug profiler click on the green bug icon (Start xdebug session) and the green square icon (Start xdebug profiler!) on the bottom right of Firefox respectively. Once activated the bug will have a red circle over it and the square will become red. The square is to the right of the bug icon, its a bit hard to see so I included some before and after screenshots:

easy Xdebug Before - Firefox Add-on easy Xdebug After - Firefox Add-on

Once you’ve done this visit a site on your localhost and then refresh/update webgrind. Oh, and to learn more about the webgrind visit the official webgrind project page, though I’m sure you could have figured this one out on your own ;) .

Hope you found this tutorial helpful. If you have any comments or questions please post them below!

Last modified: February 19, 2013