Filed under Linux

Profiling PHP Code with XDebug and CacheGrind

In my posting about installing a PHP development environment using Ubuntu 10.04 I mentioned the possibility to debug PHP code using XDebug and CacheGrind. Here is a little tutorial on howto use profiling with some free open source tools.

Installation / Configuration

XDebug is a debugging component which can be added as a module to PHP using PECL. To install XDebug (if you havent’t used my tutorial yet) simply use the following code:

1
pecl install xdebug

Now you need to configure XDebug. As a default configuration you should put the following into /etc/php5/conf.d/xdebug.ini:

1
2
3
xdebug.profiler_enable_trigger = 1
xdebug.profiler_enable=0
xdebug.profiler_output_dir = /tmp

The first line enables the profiler by using a trigger. The trigger is a GET/POST parameter called XDEBUG_PROFILE=1 you can use whenever you start a local page via your webbrowser.

The second line enables the profiler by default and would create a profiler output file without a trigger parameter. We don’t want to do that all the time, so the value here is 0. If you want to enable your profiler the whole time, just put a 1 in there.

The third parameter is the path of the directory you want to write your profiler output files to. I’ve inserted /tmp as a default here, but you can change it to any other directory your webserver has write access to.

So an example URL to access a local webpage and create a profile output would be something like this:
http://localhost/index.php?XDEBUG_PROFILE=1

There should be a cachegrind file in your output dir, e.g. cachegrind.out.12345

The number at the end of the filename is the apache pid. The name of the cachegrind file can be configured using another parameter, xdebug.profiler_output_name. A complete documentation of all possibilities can be found here.

Displaying the profile data

There are several tools to analyze the data created by the XDebug profiler. One of these tools is KCachegrind. KCachegrind visualizes traces generated by profiling, including a tree map and a call graph visualization of the calls happening.

As an example: you can analyze how often certain heavy-load methods are called within your application; that way you can find ways to minimize those calls, reduce them to a pure minimum and increase the performance of the application or website.

Alternative visualization tools for different platforms and operating systems are also available, e.g. MacCallGrind für MacOS. A web based solution would be Webgrind, hosted on Google Code.

Tagged , , , , ,

Installing Ubuntu 10.04 Lucid Lynx for PHP development

Lucid-LynxLast year I have written a small how-to for installing an Ubuntu 9.04 work environment as virtualbox environment (though it also works for native systems). Recently I have updated this guide to 10.04 as kind of check list to get a working environment to develop PHP/MySQL applications.

Continue reading

Tagged , , , ,

Ubuntu 10.04 LTS (Lucid Lynx)

On April 29th the new Release of Ubuntu, named Lucid Lynx, will be available to download from the official Ubuntu site. Currently the release candidate is already available, so if you want to try out, you can get it now and upgrade to the full version later.

One of the biggest changes is the new default style in Gnome, which puts the menu buttons to the upper left part of the window instead of the upper right. This is a little bit confusing, but you can change back to a more traditional layout.

Only problem so far was installing apc but I guess I will solve that one soon :)

Tagged ,

Will Google Chrome OS support other browsers than Chrome?

It’s almost a week old news that Google plans to release an own operating system based on the Chrome browser in the next year. After thinking about it for a while I came to the point were I have to ask the following question:

Will Google Chrome OS support other browsers than Chrome?

Basically a lot of people complained about Microsoft that they are bundling their own browser Internet Explorer with their operating system. But what about an operating system which basically IS a browser? Is it different, or will it be possible e.g. to install a Firefox to Chrome OS?

I dont’t know the answer yet, but I’m very curious if Google will become a second Microsoft in terms of OS.

Tagged , ,

How to install an Ubuntu based Web Server

I recently upgraded my old VPS, which was running with Ubuntu 6.06, to a fresh new Hardy Heron version. That gave me the opportunity to make a clean installation and configuration and then transfer my websites to the new server.

Hint: I did all the install and update stuff as root user. If you have another user with sudo rights, then apply a sudo before most of the commands here.

Updating the Apt

First step I did was updating the packages.

apt-get update
apt-get upgrade

LAMP

After that i installed the primary components, like Apache, PHP5 and MySQL:

First the webserver itself:

apt-get install apache2

I installed additional Apache modules. An easy way to do that is to use a3enmod:

a2enmod ssl
a2enmod rewrite
a2enmod suexec
a2enmod include

Just don’t forget to reload your apache afterwards:

/etc/init.d/apache2 force-reload

Afterwards install PHP5:

apt-get install php5
apt-get install php5-cli
apt-get install php5-dev
apt-get install php-pear

Some more PHP5 modules are following later.

PEAR is a package repository which enables you to install additional PHP package libraries.

To make sure that my link to PEAR is up to date, I use the following command:

pear channel-update pear.php.net

Installing MySQL is rather simple:

apt-get install mysql-client mysql-server libmysqlclient15-dev

Afterwards you get asked to enter a MySQL root password. Make sure to keep that one safe!

I say I need some more PHP5 modules, so here they come:

apt-get install php5-gd
apt-get install php5-mcrypt
apt-get install php5-imagick
apt-get install php5-curl
apt-get install php5-xmlrpc php5-xsl
apt-get install php5-mhash

Additional tools

To create statistics based on my webserver’s log files, I use awstats:

apt-get install awstats

Just make sure that the folder /usr/lib/cgi-bin is password protected (or move the file awstats.pl to a more secure location.

Configuration of awstats is another topic, I don’t want to talk too much about right now. There is an example file in the folder /etc/awstats just digg through it :)

To keep the server time up to date you can install ntp:

apt-get install ntp ntpdate

Now there is always the correct time on your server.

Finally

I did some more stuff but those are the basics to install on a fresh web server. Another hint: make sure your server is able to send mails to the outside world (maybe install another MTA, but make sure only the server itself can relay mails, unless you want to use the server as SMTP server as well).

If you have anything to add to this howto, I am happy for every comment on it.

Tagged , , , , , ,