Installing XDebug and configuring for PHP profiling purposes.

Xdebug and Php Profiling

Segment 1.

Installing and Configuring XDebug

 In this brief article I will instruct to you how to install and quickly configure Xdebug for PHP5. This article was built in relation to my Xdebug series of articles that helps you profile PHP quickly and with relative ease to find out why PHP is using excessive Memory, or just running very sluggishly.

Installation of XDebug Package

I have found that the easiest ways to install Xdebug are from the OS packages or the pecl manager.
 Official URL: http://www.xdebug.org/docs/install
Choose the appropriate install for you: 
Centos/Redhat: yum install xdebug
Ubuntu/Debian: apt-get xdebug
 Pecl: pecl install xdebug

Configuration:

After we installed the Xdebug package you need to locate xdebug.so library. I suggest using the locate command if you have it installed otherwise find works.
Locate Command: updatedb && locate xdebug.so
Find Command: find / -name ‘xdebug.so’ 2> /dev/null

Php ini Config XDebug.ini  file:
I have put together a pretty basic Xdebug config file, I like to keep it seperate from the primary php.ini file myself. 
Paste the entries below in a ini file /etc/php5/conf.d folder or whatever the Php folder is for Ubuntu. For example I use /etc/php5/conf.d/xdebug.ini.

zend_extension=”/location/of/your/library/xdebug.so” 
xdebug.auto_trace=Off
xdebug.profiler_enable=1
xdebug.profiler_enable_trigger=1
xdebug.trace_enable_trigger=1
xdebug.trace_output_dir=/var/log/php
xdebug.profiler_output_dir=/var/log/php
xdebug.trace_output_name= cachegrind.out.%s.%t.trace.%p
xdebug.profiler_output_name =cachegrind.out.%s.%t.profile.%p
xdebug.trace_format=1
xdebug.show_mem_delta=On
xdebug.var_display_max_data=128
xdebug.collect_params=4
xdebug.collect_return=On
xdebug.trace_options=1
;xdebug.idekey=
;xdebug.remote_host=127.0.0.1
;xdebug.remote_port=10000
;xdebug.remote_autostart=1

***Note: while Profile enabler are enabled, Xdebug will keep parsing all your php requests and your log folder will/can grow very large quickly. 

Setting the permissions and ownerships

I have manually create a log folder to keep track of the traces made by Xdebug. We then of course need to set the apache group ownership and writability, so that apache can create our traces. Finally restart Apache
mkdir /var/log/php
chown root:apache /var/log/php
chmod 775 /var/log/php
Centos/Redhat: service httpd restart
Ubuntu/Debian: service apache2 restart

Testing:

Lets test this out really quickly shall we? There is a script that comes installed with xdebug that will analyze your last running php process’s to see what was used. This little nugget is perfect to test with. 
 Here is a better understanding of what the tracefile-analyser.php is and does view: http://derickrethans.nl/xdebug-and-tracing-memory-usage.html .

First we find the file:

Locate Command: locate tracefile-analyser.php
Find Command: find / -name ‘tracefile-analyser.php‘ 2> /dev/null

I just copy this to the document root directory of where you will be testing the script from. 

Next we will generate a Trace log file by creating a quick php script test.php script. Use my short script below.  You just need to browse to the url,  which will create a new log file in /var/log/php.  Browse http://yourdomain.com/test.php?XDEBUG_TRACE=1
 
<?php
echo “Hello World”.”n”;
echo “<br>”;
echo “<br>”;
echo “<br>”;
echo date( ‘m/d/y h:ia’ );
echo “<br>”;
echo “<br>”;
$var = ‘122.34343The’;
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343
?>

Alright now goto /var/log/php and find your new log file that might end in .xt and use the command below:
php /path/to/your/tracefile-analyser.php cachegrind.out.xxxxx.xt memory-own 20

Results:
If you get something that looks like this below, your xdebug is working!
Showing the 20 most costly calls sorted by ‘memory-own’.

                Inclusive        Own
function#calls  time     memory  time     memory
————————————————
{main}       1  0.0026     1144  0.0001      784
date         1  0.0025      360  0.0025      360
floatval     1  0.0000        0  0.0000        0

jim has written 83 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>