Install xdebug and enable profiling
Installing xDebug and enabling profiling is a two step process which is outlined below.
First of all you’ll need to install the xDebug library
PHP5.x
Tested on Ubuntu 14.04
sudo apt-get install php5-xdebug
PHP7
Tested on Ubuntu 16.04.3
sudo apt-get install php-xdebug
If the package was installed successfully, you should see ‘xdebug’ listed when you run:
Then you’ll need to configure xDebug to enable profiling
Profiling isn’t enabled by default, so the next step is to enable it via the config file. You can find the file at:
Apache
If you’re using Apache, you’ll find the config file at:
/etc/php/YOUR_VERSION/apache2/conf.d/20-xdebug.ini
Nginx
If you’re using Nginx (and PHP fpm), you’ll find the config file at:
/etc/php/YOUR_VERSION/fpm/conf.d/20-xdebug.ini
Once you’ve found the file, append the following lines:
xdebug.profiler_append = 1
xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 1
Restart the server to enable the changes
To enable the config changes, you’ll need to restart your server
Apache
First check that the new config isn’t going to break anything:
sudo apache2ctl configtest
And then, if all is well, restart the server:
sudo service apache2 restart
Nginx
First check that the new config isn’t going to break anything:
sudo nginx -t
And then, if all is well, restart the server:
sudo service nginx restart
Ready to profile
You’re now ready to profile your PHP applications. It’s important to note with ‘profileenabletrigger’ enabled, you need to explicitly request profiling. You can do so by appending the following variable to your query-string:
?XDEBUG_PROFILE=1
Upon doing so, profiling data will be dumped to the /tmp folder on your server. You can then use KCacheGrind to parse and visualize the dump.