Speed up a Drupal web site by enabling PHP accelerator script caching

Technologies: Drupal 5+, PHP 4+, APC or eAccelerator

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.

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:

  1. Download the PHP script cache extension. If necessary, compile it for your operating system and your version of PHP.
  2. Install the cache extension into your PHP engine’s folder.
  3. Edit your PHP engine’s “php.ini” configuration file to enable the cache.
  4. 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?

I benchmarked the effect of PHP script caching on two representative test sites: a simple Drupal site with just the basic modules, and a complex site with lots of modules (see my Specifications for Drupal web site testing). Both sites have no other performance improvements (e.g., no file compression, no MySQL query cache, and no Drupal page cache or CSS file aggregation). Load times are for each site’s home page, including HTML, CSS, JavaScript, and images. Tests simulated a web page uploaded over a 64Kbps cable modem, such as that used to serve small web sites from a home or small business.

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).

Total page load time

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:

Wikipedia maintains a good list of PHP accelerators.

Conclusions

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.

Further reading

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Nadeau software consulting
Nadeau software consulting