This article explains how to boost the performance of a PHP site by installing a PHP accelerator software.
Why is PHP slow
PHP is an interpreted language that requires to parse the PHP files for each request received by the server. With a compiled language such as Java or Ada, this long and error prone process is done beforehand. Even if the PHP interpretor is optimized, this parsing step can be long. The situation is worse when you use a framework (Symfony, CakePHP,...) that requires many PHP files to be scanned.
eAccelerator to the rescue
eAccelerator is a module that reduces this performance issue by introducing a shared cache for the PHP pre-compiled files. The module somehow compiles the PHP files in some internal compiled state and makes this available to the apache2 processes through a shared memory segment.
First get eAccelerator sources at http://eaccelerator.net/
Then extract the tar.bz2 file on your server:
$ tar xvjf eaccelerator-0.9.6.1.tar.bz2 eaccelerator-0.9.6.1/ eaccelerator-0.9.6.1/COPYING ...
Build eAccelerator module
Before building the module you must first run the phpize command to prepare the module before compilation:
$ cd eaccelerator-0.9.6.1/ $ phpize
Then, launch the configure script:
$ ./configure --enable-eaccelerator=shared \ --with-php-config=/usr/bin/php-config
Finally build the module:
Installation is done by the next steps:
$ sudo make install
Don't forget to copy the configuration file (have a look at its content but in most cases it works as is):
$ sudo cp eaccelerator.ini /etc/php5/conf.d/
Restart Apache server
To make the module available, you have to restart the Apache server:
$ sudo /etc/init.d/apache2 restart
What performance gain can you expect... That will depend on the PHP software and the page. It's easy to have an idea.
To measure the performance improvement, you can use the Apache benchmarking tool. Do a performance measurement on the web site before the installation and another one after. Be sure to benchmark the same page.
The following command will benchmark the http://mysite.mydomain.com/index.php page 100 times with only one connection.
$ ab -n 100 http://mysite.mydomain.com/index.php
Below is an extract of the percentage of the requests served within a certain time (ms) for one of my web page served by Dotclear:
Without with eAccelerator eAccelerator 50% 383 236 66% 384 237 75% 387 238 80% 388 239 90% 393 258 95% 425 265 98% 536 295 99% 796 307 100% 796 307 (longest request)
The gain varies from 38% to 60% so it is quite interesting. The other benefit is that the variance is also smaller meaning that requests are served globally in the same time.