Installing xhprof for PHP function call debugging

cPanelTristan

Quality Assurance Analyst
Staff member
Oct 2, 2010
7,607
43
348
somewhere over the rainbow
cPanel Access Level
Root Administrator
For those interested in debugging a PHP script, you might consider using xhprof on your server. The following guide will cover installation of this component on a cPanel machine for a single account:

1. Pecl installation

Since xhprof is a beta module for PHP, the following command will not work:

# pecl install xhprof
Failed to download pecl/xhprof within preferred state "stable", latest release is version 0.9.2, stability "beta", use "channel://pecl.php.net/xhprof-0.9.2" to install
install failed
The following command should work:

Code:
pecl install channel://pecl.php.net/xhprof-0.9.2
You may see the following error on phpize for the pecl installation:

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

ERROR: `phpize' failed
If so, then do the following to manually configure xhprof on the machine:

Code:
cd /root/tmp/pear/cache/xhprof-0.9.2/extension
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
2. Adding xhprof to php.ini

Edit /usr/local/lib/php.ini to add the following at the bottom:

Code:
[xhprof]
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
3. Creating the temporary session directory

Code:
mkdir /tmp/xhprof
chmod 777 /tmp/xhprof
4. Checking xhprof shows up as a PHP module

Code:
php -m | grep -i xhprof
5. Install graphviz for graphical information

Code:
cd /
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.28.0.tar.gz
tar xzf graphviz*
cd graphviz*
./configure
make && make install
6. Copy all the xhprof directories to /usr/local/lib/php

Code:
cd /root/tmp/pear/cache
cp -r xhprof-0.9.2 /usr/local/lib/php/xhprof
7. Create a list.php file for the /tmp/xhprof sessions

Code:
touch /usr/local/lib/php/xhprof/xhprof_html/list.php
Place the following contents in that file:

Code:
<?php
$path = '/tmp/xhprof/';
foreach(glob($path."*") as $file) {
   $tmp = pathinfo($file);
   echo "<a href=\"index.php?run={$tmp['filename']}&source={$tmp['extension']}\">{$tmp['filename']}</a><br/>";
}
?>
8. Testing a php script

Create the following at /home/username/public_html/php.php location (where username is the cPanel account username):

Code:
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
<?php
// Start profiling
xhprof_enable();

// A basic PHP test
echo '<p>Hello World</p>';
echo date("M-d-Y");

// Stop profiling
$xhprof_data = xhprof_disable();

//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
include_once "/usr/local/lib/php/xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once "/usr/local/lib/php/xhprof/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new XHProfRuns_Default();

// Save the run under a namespace "my_xhprof_foo".
$run_id = $xhprof_runs->save_run($xhprof_data, "my_xhprof_foo");
?>
</body>
</html>
If you are using DSO, you can then symlink the global directories:

Code:
cd /home/username/public_html
mkdir xhprof && cd xhprof
ln -s /usr/local/lib/php/xhprof/xhprof_html .
ln -s /usr/local/lib/php/xhprof/xhprof_lib .
If you are using suPHP, you would need to copy the directories:

Code:
cd /home/username/public_html
mkdir xhprof && cd xhprof
cp -r /usr/local/lib/php/xhprof/xhprof_* .
You should then be able to load the http://domain.com/xhprof/xhprof_html/list.php page to see a listing of all /tmp/xhprof sessions. Click on the session to view the data. Please replace domain.com with the domain on the cPanel user's account where you created the code and linked / copied the files.

I've attached a screen print of an example of both http://domain.com/xhprof/xhprof_html/list.php and clicking on one of the sessions for the result data.

Screen shot 2012-01-15 at 12.34.47 PM.png

Screen shot 2012-01-15 at 12.34.33 PM.png

Due to the data being sent, you may well want to password protect the /home/username/public_html/xhprof directory using cPanel > Password Protect Directories area.
 

coolbloke1324

Registered
Feb 1, 2012
2
0
51
cPanel Access Level
Reseller Owner
After doing the pecl install my php was throwing errors in the apache error_log file about not finding the xhprof.so extension. I did a little digging and found that it wasn't symlinked to where PHP was looking for it.

I ran:

Code:
ln /usr/lib/php/extensions/no-debug-non-zts-20060613/xhprof.so /usr/local/lib/php/extensions/no-debug-non-zts-20060613/xhprof.so
This created the symlink and fixed my problem.:)
 

coolbloke1324

Registered
Feb 1, 2012
2
0
51
cPanel Access Level
Reseller Owner
On step 6, the copy failed because the folder didn't exist. I then ran:

Code:
tar xzf xhprof*
After which I rant the cp command again and it was ok.
 

syndicated

Member
Jun 1, 2010
14
0
51
I had the same issue as Cool - the files were not uncompressed, other then that no errors.
 
Last edited: