blog-banner

How to Make Apache Faster for Drupal

  • Apache
  • Drupal
  • Drupal Planet
  • HTAccess
  • PERFORMANCE

If you are reading this blog post you must be striving to improve the performance of your site. When we speak about performance two things should be taken into account essentially.

  1. Number of requests the site is able to serve (The more, The better)
  2. Response time per request (The least, The better)

I guess it is not much needed to stress how essential the response time plays in deciding the success of a website. Apache the prominent Web server plays a key role in this connection.
When configuring Apache for Drupal most of the tutorials recommend changing AllowOverride directive to All, including the official handbook page in drupal.org https://drupal.org/node/36628 , while the default is None.

This is needed to make .htaccess file in Drupal core to override the Apache config as needed. Especially to get a clean URL working. Besides it lets to set the page expiry time, and set necessary headers for css/js gzip compression if supported.

But this is a performance killer change!

Reason when AllowOverride is set to All. It prompts Apache to look for the existence of .htaccess file in recursive sub-folders as below.

/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess

and goes on...

Eventually, the number of I/O calls needed to serve the page request spikes up. Beware, I/O calls are expensive. And it goes further worst depending on the number of modules you have. Especially if you have hosted the site in VPS environments where disk operations are sub-optimal and can't be fully virtualized. The workaround for this problem is to tell Apache explicitly the location as to where to look for the overriding .htaccess file.

The following config helped us to fix the issue.

# Site example.com
<VirtualHost *:80>
  ServerAdmin user@example.com
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com
  <Directory /var/www/example.com>
    Options -Indexes -FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
    Include /var/www/example.com/.htaccess    
  </Directory>
  <Directory /var/www/example.com/sites/default/files>
    Include /var/www/example.com/sites/default/files/.htaccess
  </Directory>
</VirtualHost>
In favor of performance, AllowOverride directive has been changed to None. And with Include directive we have explicitly mentioned the paths as to where to look for .htacess files. This way, we could get the job done without compromising the performance & Drupal's features like clean url, expiry headers, etc. Some anonymous benchmark reports posted in groups.drupal.org says that Apache is certainly faster in serving dynamic pages over nginx when this specific directive is altered as mentioned above. But in Drupal besides the root folder, .htaccess can be found in the Temporary File system's files directory. Even some libraries like ckeditor and contrib modules shipped with their own .htaccess. This could happen when modules like Boost are installed. In the case of the Boost module, additional .htaccess files are created in the cache folder. To workaround this we could use the find command or similar, we will have to search for the existence of .htaccess manually and mention their path in the virtual host config.

Additionally, apache's module mod_expires and mod_headers could be installed as Drupal's core .htaccess tries to leverage it if they exist.

Get awesome tech content in your inbox