Install and Configure XHProf (Ubuntu) Problem

I'm trying to use XHProf but I'm getting horrible php error every time it runs.

Warning: Invalid argument supplied for foreach() in xhprof_compute_inclusive_times() (line 1670 of /var/www/drupal/sites/all/modules/XHProf/xhprof.inc).
Warning: Invalid argument supplied for foreach() in xhprof_compute_flat_info() (line 1644 of /var/www/drupal/sites/all/modules/XHProf/xhprof.inc).
Warning: Invalid argument supplied for foreach() in theme_xhprof_overall_summary() (line 367 of /var/www/drupal/sites/all/modules/XHProf/xhprof.module).


The problem is solved here because the temporary folder doesn't exist, however mine does.

Set-Up Server Side

I'm running a fresh Ubuntu Server 13.04 with Drupal 7.

Install xhprof using pear following the instructions here.
I also added a temporary directory to php.ini, which I created manually.

extension=xhprof.so
xhprof.output_dir="/var/tmp/xhprof"


Drupal Set-Up

I have both the devel installed and I also installed the XHProf module. 

In admin/config/development/xhprof, I enabled profiling of all page views and drush requests.

On following an xhprof link I get the error above.
Good god Batman. That was incredibly frustrating. Here's a guide to getting xhprof working. (Note: I am very new to Linux. I'm not sure if the permissions I use are safe, or whether there is a better way of doing it. I'd only do this on a test environment.) 

IMPORTANT: It won't tell you anything about how to interpret it. I have no idea how to, I'm just happy it works.

Getting XHProf Working

1. What operating system are you running?

Mac or Linux?

It's possible.

Windows?

Probably not. To run xhprof on windows you have to have a compiled dll. There's a link to one in this thread, but I always had compatibility issues with it when I tried to use it on XAMPP. 

I think it was compiled with a different version of Apache.

2. Getting it working on Ubuntu

It's possible on Mac. Here are two useful links:

http://www.lullabot.com/blog/article/installing-xhprof-mamp-and-php-52-mac-os-106-snow-leopard
http://www.midwesternmac.com/blogs/jeff-geerling/getting-xhprof-working-well

But I haven't done it. I managed to get it working on Ubuntu Server 13.04, although I think any recent version of Ubuntu Server would work.

I set-up my server on VirtualBox. VirtualBox is free but it might take a bit to learn how to set it up.

3.Installing XHProf

I'm going to assume we have a fully functioning Ubuntu Server.

First we need to install pear. We'll use this to install XHProf.

sudo apt-get install pear


We follow this up with phpdev which is also needed.

sudo apt-get install php5-dev


If these commands don't work and I've got them wrong, just go straight ahead with:

sudo pecl install -f xhprof


Ubuntu will tell you what you're missing and give you the prompt to install it. Once you've got all the dependencies install xhprof.

You might get an error on installing xhprof, it'll try to download the beta and fail. In that case try:

sudo pecl install -f xhprof-beta


Hopefully xhprof is now installed.

Now we need to configure it. Open your php.ini which is at /etc/php5/apache2/php.ini
and add the following two lines under the extensions section:

extension=xhprof.so
xhprof.output_dir="/var/tmp/xhprof"


The second line sets the output directory. If this folder doesn't exist you'll have to create it.When you've done that reboot apache using

sudo service apache2 restart


4. Checking if XHProf is running

Now we need to check if that worked. 

Test 1:

Enter php -m

If you see xhprof in the list you're good to go. If not, don't worry, try this instead to check if it's working:

Go to your webroot /var/www/ and create a php file with only:

phpinfo();


Save it as phpinfo.php. Open this in your browser and you should see PHP information. Search for xhprof and if you see this then it's running.



5. Getting it working with Drupal

I couldn't get it working with the Drupal module. So instead I'm going to tell you how to get it working with the devel module.

First some config that I borrowed from here. I didn't really follow this part, but it works so gift horse and all that.

Create a new config file with the following command:

sudo nano /etc/apache2/conf.d/xhprof.conf


and insert the following code. 

alias /xhprof_html "/usr/share/php/xhprof_html/"


Then we possibly need to create a symlink to our xhprof_html folder. So just in case set this:

ln -s /usr/share/php/xhprof_html /var/www/xhprof_html

This will create a folder in your webroot that points to the existing xhprof file. Then in your browser you can open http://mywebsitehere.com/xhprof to see all the runs you've made.

Then restart apache. Now it's time for a little devel config. Go to the devel settings, enable xhprof and set:

xhprof directory = /usr/share/php

XHProf URL = /xhprof_html


Now at the bottom of each page you should see a little link to xhprof.



And clicking it should take you to a page that looks like this. You're done. Congratulations! 



If that didn't work and you're getting run errors it's probably because Drupal doesn't have permission to write to the tmp folder that we set earlier so enter:

sudo chown -R www-data /var/tmp/xhprof


And hopefully that works. We're done. Hoorah!

Unless you want graphing functions. Thankfully that's as easy as:

sudo apt-get install graphviz
sudo service apache2 restart


And now you should have graphing functions. Hopefully that helps!