To install Xdebug, it’s easy if you’ve installed PHP extensions before, follows the basic 4 step procedure. Also we’ll need to download it, I’ve been using the latest 2.0 beta with no problems, you can visit the Xdebug site to get more information and download it.
First we have to run phpize, this builds the configure script and other make/configure related files tailored to your install of PHP, you’ll need your PHP install’s bin folder in path, typically this is for system installed PHP or custom installs in /usr/local. If you installed PHP via something like apt in debian or ubuntu, you’ll need a particular package, this should be ‘php5-dev’ which includes phpize and necessary files to build PHP extensions. Then we can run configure, make, then make install to install it. The basic installation commands:
$ cd xdebug-2.0.0beta6
$ phpize
$ ./configure
$ make
$ sudo make install
Now that we installed it, Xdebug requires some minimal php.ini setup, particularly a special way of loading the extension unlike normal ones. I basically want the debugger available when I need it, and for Xdebug to be using it’s stack traces for any notices/errors, and profiling to be only on when I want it. Below is what I have, for debugging support (which requires something like Komodo or Eclipse with Xdebug support) you’ll need to enter the address of the machine that will have a debugger, along with the port, and the username you’re on on that machine. So this setup, my laptop will do any debugging, and I’m always ‘jerome’ on any machine, which is what the debugger clients usually use to identify the ide/client. For profiling you’ll need to have a directory writable by the web server where the cachegrind formatted profiler dumps go which is the last option I have.
zend_extension= /usr/lib/php5/20051025/xdebug.so ;full path to xdebug extension, installed in your extensions folder
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=192.168.1.40
xdebug.remote_port=9001
xdebug.idekey=jerome
xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 1
xdebug.profiler_output_dir = /storage/sites/profiler_files
For other options, you can consult www.xdebug.org for documentation on xdebug and it’s configuration, available functions, and more.
Now to use profiling, all you have to do is restart web server if you haven’t already after saving php.ini changes, and then any script you want profiled, just include XDEBUG_PROFILE=1 in GET or POST and this will cause profiling to be run and a cachegrind.out.##### type file to be generated in the path specified in php.ini. It’s best to rename the file to something usable after the script is done so you know what it is, and if you do the same script, it might try to write to that same file. These files are then best viewed in a program that knows cachegrind format, like KCacheGrind as seen in my previous post.
Technorati Tags: howto, php, programming, tutorial, xdebug