Post details: eAccelerator
03/01/06
eAccelerator
eAccelerator is a PHP cacher. It is the continuation of a really popular project known as Turck MMCache. Like its predecessor, eaccelerator is both free to the public and open source. Unlike its predecessor, it has support for PHP 5.x.
eAccelerator's main function is to cache PHP scripts in a precompiled state. Normally when you write PHP scripts for your website the source code is not compiled. The compilation is done on access. On every access. eAccelerator takes your source code and stores it compiled in Shared Memory as well as on your hard drive. This completely eliminates the need to compile every time on every access. Naturally, this is a very good idea for Server Administrators to apply to their PHP enabled servers.
Before we begin, we need to check a couple of prerequisites. First, you CAN NOT use a PHP cacher if you have PHP compiled as a CGI binary. Second, check to see if you have a Zend Loader installed. If you don't know what that is then don't worry because you probably don't have one. Last but not least, if you have ionCube installed then you will need to unload it from your php.ini file and use runtime-loading.
Installation
Chances are good that you'll be able to find a compiled binary of eAccelerator for your distribution; however, I always recommend compiling from source. But if you would prefer to use a compiled binary, then you can check this list to see if one already exists.
Assuming you're compiling from source the first thing you want to do is obtain the latest release. Log in to your server of choice and transform into root. My directory of choice for storing installs is /root/installs/ and this particular one I put in /root/installs/php/. Extract your source file and change to the appropriate directory.
baku# pwd
/root/installs/php
baku# ls
eaccelerator-0.9.5-beta1.tar
baku# tar xf eaccelerator-0.9.5-beta1.tar
baku# ls
eaccelerator-0.9.5-beta1 eaccelerator-0.9.5-beta1.tar
baku# cd eaccelerator-0.9.5-beta1
Next, we are going to set an environment variable to point to our PHP installation directory. This procedure is different for every shell. If the following doesn't work, please consult information for your shell type. We need to know the location of phpize. On my distribution the directory is /usr/local/bin/phpize. So the value I would need to set is /usr/local. This is where PHP is installed. Yours may very well be installed into just /usr or some other directory. The variable that we need to set this value to is $PHP_PREFIX. For you coders out there this means that: $PHP_PREFIX = /usr/local.
To do this in a C shell (csh or tcsh) you would type:
baku# setenv PHP_PREFIX /usr/local
baku# echo $PHP_PREFIX
/usr/local
baku#
Or to do this in a Bash shell you would type:
[root@baku ~]# export PHP_PREFIX="/usr/local"
[root@baku ~]# echo $PHP_PREFIX
/usr/local
[root@baku ~]#
To make a configure script and makefile with PHP you need phpize. Remember when we searched for it before? Now we are going to use it. Using $PHP_PREFIX/bin/phpize will run the command. Make sure you are still in your source directory with a quick pwd. If everything goes well you are ready to configure eAccelerator for your system. The configure line is pretty long so look for it in the code below. It starts with ./configure and is pretty easy to find. If you find no errors issue a make and make install.
baku# pwd
/root/installs/php/eaccelerator-0.9.5-beta1
baku# $PHP_PREFIX/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20050922
Zend Extension Api No: 220051025
baku# ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config
…
…
…
baku# make
…
…
Build complete.
baku# make install
Installing shared extensions: /usr/local/lib/php/20050922/
baku#
If you got this far, Congratulations. You have successfully installed eAccelerator; however, it is not configured or running. For that we need to edit some configuration files and restart our webserver.
Configuration
The first step to configuration is activating eAccelerator. This can be done by inserting the following line into either the extensions.ini file or the php.ini file. I personally use the extensions.ini file (found in /usr/local/etc/php/ on this particular server) for loading all shared modules. Using your favorite editor we need to add the line extension=eaccelerator.so to the bottom of your .ini file of choice like so:
baku# locate extensions.ini
/usr/local/etc/php/extensions.ini
baku# ee /usr/local/etc/php/extensions.ini
…
…
extension=xmlrpc.so
extension=pspell.so
extension=eaccelerator.so
…
"/usr/local/etc/php/extensions.ini" 39 lines, 777 characters
baku#
Next we will need to locate and edit our php.ini file to change the default eAccelerator settings. If you were using the extensions.ini file in the previous step and don't already have your php.ini file open, then you'll need to do that. Once it's open you'll need to scroll to the end of the file and add the following lines.
[eAccelerator]
eaccelerator.cache_dir = "/tmp/eaccelerator"
eaccelerator.check_mtime = "1"
eaccelerator.compress = "1"
eaccelerator.compress_level = "9"
eaccelerator.content = "shm_and_disk"
eaccelerator.debug = "0"
eaccelerator.enable = "1"
eaccelerator.filter = ""
eaccelerator.keys = "shm_and_disk"
eaccelerator.log_file = "/var/log/httpd/eaccelerator.log"
eaccelerator.name_space = ""
eaccelerator.optimizer = "1"
eaccelerator.sessions = "shm_and_disk"
eaccelerator.shm_max = "0"
eaccelerator.shm_only = "0"
eaccelerator.shm_prune_period = "0"
eaccelerator.shm_size = "0"
eaccelerator.shm_ttl = "0"
If you want to enable logging you need to change debug = "1" and the log_file variable to where your logs are stored. It would be a good idea to leave this on for a day or so just to make sure everything is running correctly. Another thing to note here is check_mtime = "1". This checks the last modification time of your PHP files. That way if the file was modified it wont serve up a stale file out of your cache. Lastly, check your cache_dir and make sure it exists. Set it to your global temp directory. Don't forget to chmod and chown everything correctly. Your temp directory needs to be chmod 777 and your log needs to be owned by your Web Server user.
baku# cd /var/log/httpd
baku# ls
access.log error.log php.log
baku# touch eaccelerator.log
baku# ls -la
total 3238
drwxr-xr-x 2 root wheel 512 Mar 1 16:08 .
drwxr-xr-x 5 root wheel 1536 Mar 1 05:00 ..
-rw-r--r-- 1 root wheel 2314171 Mar 1 11:30 access.log
-rw-r--r-- 1 root wheel 0 Mar 1 16:08 eaccelerator.log
-rw-r--r-- 1 root wheel 942678 Feb 28 15:33 error.log
-rw-r--r-- 1 root wheel 372 Dec 26 13:28 php.log
baku# mkdir /tmp/eaccelerator
baku# chmod 777 /tmp/eaccelerator
Congratulations, you have completely installed eAccelerator. Now all you need to do is restart your Web Server. If you want to verify that it is installed check your PHP info page or do it via CLI.
baku# /usr/local/etc/rc.d/apache2.sh restart
Performing sanity check on apache2 configuration:
Syntax OK
Stopping apache2.
Waiting for PIDS: 9257.
Starting apache2.
baku# php -i
…
…
eAccelerator
…
eAccelerator support => enabled
Version => 0.9.5-beta1
Caching Enabled => false
Optimizer Enabled => false
…
Directive => Local Value => Master Value
eaccelerator.cache_dir => /tmp/eaccelerator => /tmp/eaccelerator
eaccelerator.check_mtime => 1 => 1
eaccelerator.compress => 1 => 1
eaccelerator.compress_level => 9 => 9
eaccelerator.content => shm_and_disk => shm_and_disk
eaccelerator.debug => 0 => 0
eaccelerator.enable => 1 => 1
eaccelerator.filter => no value => no value
eaccelerator.keys => shm_and_disk => shm_and_disk
eaccelerator.log_file => /var/log/httpd/eaccelerator.log => /var/log/httpd/eaccelerator.log
eaccelerator.name_space => no value => no value
eaccelerator.optimizer => 1 => 1
eaccelerator.sessions => shm_and_disk => shm_and_disk
eaccelerator.shm_max => 0 => 0
eaccelerator.shm_only => 0 => 0
eaccelerator.shm_prune_period => 0 => 0
eaccelerator.shm_size => 0 => 0
eaccelerator.shm_ttl => 0 => 0
…
…
baku#
Don't worry though, if you were viewing your PHP information from CLI it may report that caching and optimization is disabled. Just check via a PHP information page if that worries you.
You should notice a drop in your loads very quickly on large servers. On smaller servers you may not see anything. Hopefully this guide will help you become an even better Server Admin. Or at least it will show you how to fake it.
This post is licensed under a
Creative Commons License.
Comments:
No Comments for this post yet...
This post has 2 feedbacks awaiting moderation...
Leave a comment: