PHP has become the de-facto standard for web applications over the past few years. Bigger and bigger applications are being developed for it every day, and with the advent of news-networking sites such as digg.com and slashdot.org, heavy traffic can happen with no notice whatsoever.

One of PHPs downfalls in this sort of situation is that it’s an interpreted language. This means that every time your webserver gets a request for a page written in PHP, it has to compile that individual page, before it can then process it and serve the results to the user. Most of the time, this happens in milliseconds, hardly noticeable at all. However under high load situations, several processes are spawned for every web request, memory is consumed, apache handlers are held open, etc. Things can get ugly pretty quickly.

Enter, XCache. XCache was developed by the lighttpd team, and is ported over to work with just about any webserver. What XCache does, and why it’s a beautiful thing, is that it stores a copy of the PHP page post-compilation in memory and then serves that to the next user that requests the same page. Not only do you gain the benefit of a ‘compile once, serve many times’ application, the page is also being pulled straight from memory. *WAY* faster than serving it up off of your disks.

In this post, I’m going to show you how to install XCache on CentOS and Apache with PHP 5. However this is easily ported to other Operating systems very quickly.

So let’s get started, shall we?

The current build of XCache is 1.2.2, and can be downloaded from http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz . So, SSH into your server and do the following

cd /usr/src; wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz

tar -xzvf xcache-1.2.2.tar.gz

This is going to unarchive the xcache installation in your /usr/src/ directory. Next we need to go in there and get it ready to build for our particular PHP installation.

cd xcache-1.2.2

It should be noted here, that you’ll need the PHP-Devel RPM installed.

To install it, just enter the next command.

yum -y php-devel

Now, let’s prep and build our xcache install.

phpize --clean && phpize; ./configure --enable-xcache

make && make install

There, Xcache is installed as a PHP extension now! Let’s go ahead and setup the configuration for it.

cat xcache.ini > /etc/php.d/xcache.ini

Now, open up your favorite editor of choice and let’s make a couple of changes here…

put a ‘;’ in front of all of the zend_extension lines that don’t already have one. In PHP, this comments out that line so it’s ignored.

add in

zend_extension = /usr/lib/php/modules/xcache.so

Now, change the xcache.admin.user line to read

xcache.admin.user = "admin"

xcache.admin.pass ="yourpassword"

The above password needs to be an MD5 Encrypted version of your password. You can generate an encrypted version of your password by doing the following from the commandline.

md5 -s "YOURPASSWORD"

It’ll look something like this…

Maynard:~ chrismm$ md5 -s "blah"

MD5 ("blah") = 6f1ed002ab5595859014ebf0951522d9

xcache.size = 16M

xcache.count = 2 ; (this is the number of real processors in your machine.)

Now save your document and type ‘php -v’, you should see xcache mentioned in the output.

[root@omega xcache-1.2.2]# php -v

PHP 5.2.9 (cli) (built: Mar 25 2009 13:18:19)

Copyright (c) 1997-2009 The PHP Group

Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

with the ionCube PHP Loader v3.1.34, Copyright (c) 2002-2009, by ionCube Ltd., and

with XCache v1.2.2, Copyright (c) 2005-2007, by mOo

Now, let’s go ahead and setup the web interface for Xcache. This isn’t necessary, but it’s a good tool for tuning your Xcache installation.

cp -pR ./admin/ /var/www/html/ ; (or wherever your website is)

This creates a /admin/ folder off of your site. Now you should be able to go there in a webbrowser, and enter the username/password

that you entered into the xcache.ini.

Voila! You’re now caching all of your PHP code. Expect a MUCH lower serverload, and much faster page serving. My next post on this blog is going to be about tuning your XChache instance. A base install doesn’t do you much good unless you know how to tweak it!