Installing eAccelerator on mod_php

PHP is an interpreted language. That unfortunately means that every time a client requests a PHP page from your site, the PHP interpreter has to re-read the script and compile it on the fly before it is run.

All this costs valuable processing time and if you're under heavy load or working in an environment where you don't have an awful lot of CPU grunt available to you, it can be beneficial to use a PHP caching system to shave some time off the pageload.

eAccelerator is one such tool - it compiles PHP scripts the first time they are run, but caches the results in memory (and sometimes on disk too). This means that subsequent loads of that page can be sped up by executing the cached, pre-compiled script. In essence, you cut out the middle man.

eAccelerator isn't too difficult to install, and here I'll show you the process for installing eAccelerator from source where PHP is set up as an Apache module (i.e. mod_php). This tutorial does assume some knowledge using the command line and you need to be proficient in editing php.ini settings (and know how to use phpinfo to get certain bits of information about your PHP install). With that in mind, let's get started!

First things first, like any source-based install we need dependencies. For installing eAccelerator, we'll need the PHP development packages but that's about it. If your PHP is source based, these should be in place. If you're using PHP from packages provided by your distributions, you'll need the PHP development package. For me, that's php5-dev on Ubuntu.

$ sudo apt-get install php5-dev

Now, let's acquire the source code for eAccelerator. The latest version at the time of writing is 0.9.52, so let's download and extract that now.

$ wget -c http://bart.eaccelerator.net/source/0.9.5.2/eaccelerator-0.9.5.2.tar.bz2
$ tar xjvf eaccelerator-0.9.5.2.tar.bz2

If you're a whiz with compiling software from scratch, you're probably already itching to do a ./configure. Wait.

Because we're compiling a PHP extension, we first need to prepare the ground by running the phpize command. It's as simple as:

$ phpize

If for some reason the command can't be found, double-check you've got your PHP development libraries installed. After that, it's the same old ./configure, make and make install (with root) dance. For me:

$ ./configure
$ make
$ sudo make install

You should now have a built PHP module. The remaining two steps are to copy this in place and edit your php.ini file to enable it and to set your preferences.

On my Ubuntu system with mod_php from the repositories, the extension_dir where I need to copy the module is /usr/lib/php5/20060613+lfs. You can determine yours with the standard phpinfo method (and looking for extension_dir).

$ sudo cp modules/eaccelerator.so /usr/lib/php5/20060613+lfs

We also need to create the cache directory that eAccelerator will use for PHP scripts that are compiled to disk. This directory needs to be set so that the user that runs PHP can write to it. In Ubuntu's case, the relevant user is www-data, but on other systems (for example Red Hat-based systems) it may be apache or something else.

$ sudo mkdir /var/cache/eaccelerator
$ sudo chown www-data /var/cache/eaccelerator
$ sudo chmod 700 /var/cache/eaccelerator

Now it's time to add the eAccelerator module into your PHP installation by editing php.ini. Open up said file in your favourite text editor (its location will differ depending on your exact setup, but you can use your old friend phpinfo to find out where it is).

In the relevant section, paste this:

extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/var/cache/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

Finally, you'll need to restart Apache so that it is using the new PHP with eAccelerator. You can test that it is working by heading back to phpinfo, and checking for something that looks a little bit like this:

eAccelerator

If you see that, the install was successful and your PHP scripts are now being cached!

A quick word of warning before we part ways - every time there is a PHP upgrade, you will need to recompile eAccelerator to create a new module and then copy that one over the old one (or you might get errors with PHP refusing to start up properly).

And that should be it - next time you get hit hard, your server might be able to cope a little bit better (or at very least, PHP compiling scripts won't be the bottleneck anymore).

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Installing eAccelerator on mod_php

Home » Articles » Installing eAccelerator on mod_php