Three changes to the PHP, MySQL, and Drupal default configurations improves Drupal web site performance considerably: (1) install a PHP script cache, (2) turn on MySQL's query cache, and (3) enable Drupal's page cache.
Table of Contents
The Drupal content management system is a good way to set up a multi-featured web site quickly. To get Drupal running you'll need an Apache web server, the PHP "engine" for running PHP programs, and the MySQL database. These are all free and easily installed with a few mouse clicks. But once you've got them running and you load your first Drupal Web page, it's slooooow. This is easily fixed by making three key changes to your software configuration...
Step 1. Install a PHP script cache
PHP is a programming language for building web pages, and Drupal is written using it. To run a PHP program, the PHP engine converts the programmer's instructions into simpler instructions that the computer can understand. For a program the size of Drupal, this conversion process, or compilation, can take a few seconds. When the compilation is done, the engine runs the Drupal program to generate a web page, taking another few seconds. Depending upon the web site's complexity, the total time can be 7 or 8 seconds. That's way too long. Usability studies find that page load times need to be under one second for visitors to feel that a site is responsive, and under four seconds or visitors will leave.
By default, the PHP engine re-compiles Drupal every time you load a web page. To avoid this re-compilation time and reduce page load times, install a PHP "script cache." A script cache is a PHP engine extension that saves compiled programs into a reserved chunk of computer memory. On the first use of a Drupal web page, the engine compiles Drupal and saves it into the cache. From then on, every access to a Drupal page skips the compilation step and reuses the version saved in the cache. This dramatically improves Drupal's performance.
There are several PHP script caches available. Two of the most popular are free:
Both of these come with good installation instructions. For either one you'll drop an extension file into a special PHP directory and add a few lines to PHP's configuration file, "php.ini".
When you're done installing either script cache, restart your Apache web server. Load a Drupal web page, and it'll still be slow the first time. Do it again and it'll come back faster. From then on the PHP engine is using the cached version of Drupal and page loads go faster.
Both PHP script caches reduce HTML file load times by half.
Step 2. Turn on MySQL query caching
After installing a PHP script cache, HTML file load times dropped by half. But a complex web site can still take a long time to load the page. To improve performance further we need to reduce the amount of time Drupal spends preparing a web page.
Drupal uses a MySQL database to store a web site's content. When a site visitor asks for a web page, Drupal queries the database, get's the content, formats it, and sends it to the visitor's browser. For a typical web page, Drupal may do several dozen queries, and this takes time. By default, Drupal and MySQL perform these same queries every time a page is loaded. If the site's content in the database isn't changing, then the queries return the same results every time. To save time and avoid repeated queries, enable MySQL's "query cache."
The "query cache" is a built-in feature of MySQL that saves the results of common queries in a reserved chunk of memory. The first time a query is issued, MySQL collects the data and saves it to the cache. From then on, each time the same query is issued, MySQL skips re-collecting the data and returns the results saved in it's cache. This significantly improves MySQL and Drupal performance.
By default, MySQL's query cache is disabled. To turn it on, use the MySQL Administrator program available free with MySQL:
- Start MySQL Administrator and log in as the database administrator.
- Enable the query cache:
- Click the "Options" button at the top of the window.
- Select "Performance" from the pull-down menu to show the performance settings.
- Click on the pencil icon beside "Query cache size" and enter a value, such as 32 Mbytes.
- Click the "Save" button at the bottom of the window to save the settings.
- Restart the database:
- Click the "Service" button at the top of the window.
- Click "Stop server" to stop the server.
- Click "Start server" to restart it, now using the new settings.
Step 3. Turn on Drupal page caching
Drupal uses the results of MySQL queries to build a Web page to return to each site visitor. By default, Drupal builds a new page for every site visitor. If two visitors ask for the same page, Drupal builds the page twice. To avoid repeated page builds, enable Drupal's "page cache".
The "page cache" is a built-in feature of Drupal that saves frequently requested pages. The first time a page is requested by a visitor, Drupal issues MySQL queries, builds the page, and saves it to the cache. From then on, each time the same page is needed, Drupal skips the page building step and returns the page saved in the cache. This dramatically improves Drupal's performance.
Caveat: Drupal's page cache only works for anonymous visitors. The cache is disabled for visitors that log in. Once a visitor logs in, Drupal has to maintain unique information about them, such as their shopping cart's status for an e-commerce site, or the user's preferences for the site's language, theme, or reported time zone. Drupal uses this information to create custom web pages tailored to each user. Since no two logged-in visitors get the exact same page, the page cache can't be used.
By default, Drupal's page cache is disabled. To enable it:
- Log in to your site as the administrator.
- Click on the "Administer" menu choice.
- Select the "Performance" page under "Site configuration."
- Choose either "Normal" or "Aggressive" page caching.
- Save the configuration.
Normal and aggressive page caching both cache pages in the same way. Aggressive page caching also disables some optional features inside Drupal, giving a slight performance boost. Those features are only needed by a few modules. The performance settings page will tell you if you have any modules installed that will be a problem with aggressive page caching.
Keeping eAccelerator installed from step 1, and MySQL's query cache enabled from step 2, Drupal's page cache reduces file load times by another 90-95%. The simple and complex test sites now take about the same time to deliver a cached page, and both do so in under 1/10th of a second.
The combination of a PHP script cache, MySQL's query cache, and Drupal's page cache drastically reduces HTML file load times by 98-99% overall. For the simple test web site, load times dropped from 2 seconds to under 1/10th of a second. For the complex test site, load times dropped from nearly 8 seconds to under 1/10th of a second.
|Test||Simple site||Complex site|
|Original||10.473 sec||20.676 sec|
|Enable script cache||9.412 sec||18.060 sec|
|Enable query cache
||8.709 sec||14.748 sec|
|Enable page cache||6.725 sec||10.527 sec|
What to do next
There's more you can do: