How to optimize Drupal for performance
This article describes specific steps you can take to optimize a Drupal site.
Optimizing Drupal
Drupal includes several configuration options that you can use to help improve your site's speed and responsiveness. The procedures below show how to configure these options so they are optimized for high performance.
The settings described in this article are for Drupal 7. Earlier Drupal versions may not include some of these settings.
If you configure the settings as described below and your site's performance is still not at the level you want, there are additional options:
Caching
Drupal includes a built-in caching feature, but it is disabled by default. To enable caching for your site, follow these steps:
- Log in to Drupal as the administrator.
- On the top menu bar, click Configuration.
- Under DEVELOPMENT, click Performance.
- Under CACHING, select the Cache pages for anonymous users check box.
- Select the Cache blocks check box.
- In the Minimum cache lifetime list box, select the length of time that Drupal should store files in the cache until they are refreshed.
If your site content does not change frequently, you can set the cache lifetime to a longer interval. Similarly, if you update site content frequently, you should set a shorter cache lifetime interval.
- In the Expiration of cached pages list box, select the length of time external caches can use old versions of pages.
In this case, external caches refer to client applications, such as web browsers.
- Click Save configuration.
Drupal's caching features can cause the database to grow very large, which can affect server performance. For information about how to periodically clear Drupal's caches, please see
this article.
Cron job
Drupal includes a cron job routine, but it does not run by default. You should run the cron job, however, because it performs many routine maintenance tasks that help ensure better performance. For step-by-step instructions about how to configure Drupal's cron job for your site, please see this article.
Compression
You can add Apache mod_deflate directives to the .htaccess file in Drupal's root directory to compress output. Compression saves bandwidth and ensures faster page download times.
When caching is enabled, Drupal includes its own compression option in the administration interface. To access this option, on the top menu bar, click Configuration. Under DEVELOPMENT, click Performance. Under BANDWIDTH OPTIMIZATION, look at the Compress cached pages check box. If this option is enabled, do not follow the Apache compression configuration steps below (unless you clear the check box and disable the option). You should only use one of these compression methods at a time, not both simultaneously.
To enable output compression for Drupal, follow these steps:
- Using your preferred text editor, open the .htaccess file in the directory where you installed Drupal.
- Add the following lines to the .htaccess file:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript
</IfModule>
- Save your changes to the .htaccess file. The changes take effect immediately.
JavaScript and CSS files
You can help reduce your site's page load times by optimizing JavaScript and CSS files. This process includes:
- Combining JavaScript and CSS files. This reduces the number of HTTP requests necessary to load a page.
- Minifying (removing all unnecessary characters from) JavaScript and CSS files. This reduces the amount of bandwidth required to load a page.
To do this, follow these steps:
- Log in to Drupal as the administrator.
- On the top menu bar, click Configuration.
- Under BANDWIDTH OPTIMIZATION, select the Aggregate and compress CSS files check box.
- Select the Aggregate JavaScript files check box.
- Click Save configuration.
Database tables
You should periodically optimize the Drupal database to help ensure your site loads as quickly as possible. The DB Maintenance module enables you to quickly and easily optimize Drupal database tables at fixed intervals. You can configure the module to optimize all of Drupal's database tables, or just specific tables.
The DB Maintenance module supports both the MyISAM and InnoDB engines. For more information about MySQL database engines, please see
this article.
To install and configure the DB Maintenance module, follow these steps:
- Use your web browser to go to https://www.drupal.org/project/db_maintenance.
- Under Downloads, download the .gz file for your Drupal version to your computer.
- Log in to Drupal as the administrator.
- On the top menu bar, click Modules, and then click Install new module.
- Click Browse, select the .gz file on your local computer, and then click Install. Drupal installs the DB Maintenance module.
- Under Next steps, click Enable newly added modules. The Modules page appears.
- Scroll down to OTHER, and select the ENABLED check box next to the DB Maintenance module.
- Click Save configuration.
- On the top menu bar, click Configuration, and then under SYSTEM, click DB maintenance. The DB Maintenance module configuration page appears.
- In the Optimize tables list box, select how often you want the database tables optimized.
If you have a busy site, you may want to optimize database tables more frequently.
- Select the tables you want to optimize:
- To optimize all of the tables, select the Optimize all tables check box.
- To optimize specific tables, select the table or tables in the list box.
- Click Save configuration.
Database logging
Drupal includes a database logging feature to help debug and troubleshoot problems. Although helpful for diagnosing problems, it can also cause the database to grow very large. This consumes system resources and negatively affects server performance.
For information about how to optimize database logging, please see this article.