Install XHProf for PHP5 on Centos, Ubuntu and Debian

php centos debian xhprof ubuntu

XHProf is a PHP profiling tool that was develop by and for Facebook. It's fast and very efficient. It has a small footprint and can be used in production environments.

I still use XDebug and Webgrind for profiling, but only when we're talking serious profiling. XHProf shows me information much quicker and should have a smaller entry level.

The installation have two steps:

The full documentation can be found here: XHProf homepage.

Install the PHP5 module

The graphviz library is used for displaying the execution path in a kind of flowchart between methods. It's easy to install and does not have a lot of dependencies on other libraries.

# for centos
sudo yum install graphviz
# for ubuntu and debian
sudo apt-get install graphviz

The module is should be easily installed via PECL, but I had problems installing via PECL as it supposed to be installed:

sudo pecl config-set preferred_state beta
sudo pecl install xhprof

For me, that caused an error that looked like this:

running: phpize
Cannot find config.m4.
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

ERROR: `phpize' failed

However a found a solution here at PECL bug tracker. In this case it just as easy to download the source, compile the module and install it yourself. It does require that you have the build tools though.

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension
phpize
./configure
make
sudo make install

Then we just enable it by

# for centos
sudo touch /etc/php.d/xhprof.ini
# for ubuntu and debian
sudo touch /etc/php5/conf.d/xhprof.ini

and enter this information in that config file:

[xhprof]
extension=xhprof.so
xhprof.output_dir="/tmp/xhprof"

and finally restart the webserver.

# for centos
sudo /etc/init.d/httpd restart
# for ubuntu and debian
sudo /etc/init.d/apache2 restart

The extension is now available for use, but it doesn't save or create any profiling data. For that we need to execute some profiling code before and after the PHP process.

Install the supporting PHP code

I'm using a slightly GUI enhanced version fork of the official FB one preinheimer @ github

Clone it somewhere where you can let apache reach it.

git clone git://github.com/preinheimer/xhprof.git
cd xhprof_lib/
cp config.sample.php config.php

Change the config.php to fit your setup.

Set up the database table

Check the utils/Db/database-of-choice.php for the schema definition in SQL format that will create the necessary table where profiling information will be saved.

Use vhost configuration to 'turn it on'

Add these configurations in the vhost of the site you want to profile:

 php_admin_value auto_prepend_file "/path/to/xhprof/external/header.php"

Next steps and full installation instructions

See the steps at preinheimer/xhprofi/Install