Speed up page load times by saving compiled Drupal PHP scripts into a script cache using a PHP accelerator. Cached scripts don’t have to be compiled again, saving time and enabling Drupal to build pages faster for site visitors.
Table of Contents
This article is part of the Essential steps to speed up a Drupal web site article series. Other articles in the series discuss configuration changes for Drupal, MySQL, and Apache that can improve web site performance.
How to enable PHP accelerator script caching
The standard engine that runs PHP programs, like Drupal, has no script cache. Installing and enabling a script cache speeds up page load times by 10%.
The two most popular (and free) PHP script caches are:
Both are good and both come with their own installation instructions. Because these instructions change from release to release, it isn’t practical to repeat them here. Select either cache, download it, and follow its own instructions.
So that you know what to expect, both script caches will require that you do these steps:
- Download the PHP script cache extension. If necessary, compile it for your operating system and your version of PHP.
- Install the cache extension into your PHP engine’s folder.
- Edit your PHP engine’s “php.ini” configuration file to enable the cache.
- Restart the Apache web server.
For Windows, pre-compiled binaries are available. APC’s Windows binaries are available from the PHP downloads page as part of the PECL extensions for PHP. eAccelerator’s windows binaries are available from SiteBuddy. Be sure to download the version appropriate for your installed version of the PHP engine.
What does it do?
Drupal is written using the PHP programming language. When a site visitor asks the Apache web server for a web page, Apache starts a PHP engine to run Drupal. The engine converts Drupal’s programmer instructions into simpler instructions the computer can understand. This conversion process, or compilation, takes time. When the compilation is done, the engine runs the Drupal program, which builds a web page and returns it to the site visitor.
By default, the PHP engine re-compiles Drupal every time a visitor loads a web page for the site. Because the Drupal program changes rarely (only when you install a new version), re-compiling it over and over again is redundant. Instead, when a PHP accelerator script cache is installed, the PHP engine compiles Drupal just once and saves the results into the script cache. The next time the program is needed, the engine re-uses the compiled script in the cache. This saves time, speeding up the web site.
Both script caches compile Drupal on the first site visit. If Drupal or any of its modules change, both caches detect the change and automatically recompile Drupal on the next site visit.
How well does it work?
With PHP script caching, total page load times speed up by 10%. APC and eAccelerator produced virtually identical results (eAccelerator’s were used for this plot).
When doesn’t it work?
Both script caches work well and are unlikely to need your attention once installed. If you upgrade to a new version of PHP, you will need to re-compile and re-install the script cache.
APC stores compiled scripts in memory. If you restart Apache, the cache is cleared and page load times will be slow until all of Drupal is compiled again and saved into the cache.
eAccelerator stores compiled scripts in memory too. Optionally, it also saves a copy to a hard disk folder. If you restart Apache, eAccelerator restores its cache from the hard disk. This avoids recompiling Drupal after a restart and keeps page load times fast.
What else could be used?
While APC and eAccelerator are the most common PHP script caches in use, there are others:
- XCache (free)
- Zend Platform (commercial)
- ionCube PHP Accelerator (free, discontinued)
- Turck MMCache (free, discontinued)
Wikipedia maintains a good list of PHP accelerators.
APC and eAccelerator work equally well to cache compiled PHP scripts. Either one is a useful way to speed up a PHP-based web site.