Install Xdebug and configure it with webgrind

For EasyEngine Users…

You can use ee debug command which automates some part.

Installing Xdebug

1. Install Xdebug for php

sudo apt-get install php5-xdebug

2. Setup xdebug.ini for ubuntu

vim /etc/php5/fpm/conf.d/20-xdebug.ini

Add following lines:

xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=cachegrind.out.%p-%H-%R
xdebug.profiler_enable_trigger=1
xdebug.profiler_enable=0

3. Restart php5-fpm

sudo service php5-fpm restart

Install Webgrind

Under webroot for example.com, run following commands:

wget https://github.com/jokkedk/webgrind/archive/master.zip
unzip master.zip
mv webgrind-master webgrind

Install graphviz and Gprof2Dot

This is needed for graphical presentation of calls.

apt-get install python graphviz

Webgrind config

Open config.php under webgrind folder. You may need to specify location of dot binary.

static $dotExecutable = '/usr/bin/dot';

Using Webgrind

Goto http://example.com/webgrind in your browser and you will see webgrind UI there.

But wait, you may not see any output there as cachegrind files may not have created yet.

Trigger Profiling

You may not see anything as we have set xdebug to profile only on trigger. This is good for profiling sites used in production environment as well as on server with multiple sites using same PHP pool.

You can use this browser extension to trigger profiling:

Always Profile

Be warned, that this can eat up GB’s of space on a live server with decent traffic in an hour!

You can change a line like below and it will profile php code always.

xdebug.profiler_enable=1

Of course, you will need to reload PHP for change to take effect using service php5-fpm reload

Remote Profiling

We have covered remote debugging with Netbeans here: https://easyengine.io/tutorials/php/xdebug-netbeans/. Please note that changes to debug config in NetBeans article, if you decide to use it.

Xdebug site has more remote options.

For more details, check out the Xdebug Documentation.