How To Improve Performance With Boost Module (Drupal 7 Tutorial) (Article + Video)

Submitted by TheMain on Wed, 09/16/2015 - 16:40

Boost module improves loading times by serving static pages for anonymous visitors.

Install Boost module

Download the module from https://www.drupal.org/project/boost, put it in the sites/all/modules folder and enable the module. Or use drush:

drush -y en boost

Enable Clean URLSs and disable Core Cache

Make sure Clean URLs are enabled in admin/config/search/clean-urls

Disable Core cache in admin/config/development/performance

Edit .htaccess file

Go to admin/config/system/boost/htaccess/generator to generate the code for .htaccess file.

Copy the code that looks something like this:

  ### BOOST START ###

  # Allow for alt paths to be set via htaccess rules; allows for cached variants (future mobile support)
  RewriteRule .* - [E=boostpath:normal]

  # Caching for anonymous users
  # Skip boost IF not get request OR uri has wrong dir OR cookie is set OR request came from this server OR https request
  RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [OR]
  RewriteCond %{REQUEST_URI} (^/tut/d7/1509161747t/(admin|cache|misc|modules|sites|system|openid|themes|node/add|comment/reply))|(/(edit|user|user/(login|password|register))$) [OR]
  RewriteCond %{HTTPS} on [OR]
  RewriteCond %{HTTP_COOKIE} DRUPAL_UID [OR]
  RewriteCond %{ENV:REDIRECT_STATUS} 200
  RewriteRule .* - [S=3]

  # GZIP
  RewriteCond %{HTTP:Accept-encoding} !gzip
  RewriteRule .* - [S=1]
  RewriteCond %{DOCUMENT_ROOT}/tut/d7/1509161747t/cache/%{ENV:boostpath}/%{HTTP_HOST}%{REQUEST_URI}_%{QUERY_STRING}\.html\.gz -s
  RewriteRule .* cache/%{ENV:boostpath}/%{HTTP_HOST}%{REQUEST_URI}_%{QUERY_STRING}\.html\.gz [L,T=text/html,E=no-gzip:1]

  # NORMAL
  RewriteCond %{DOCUMENT_ROOT}/tut/d7/1509161747t/cache/%{ENV:boostpath}/%{HTTP_HOST}%{REQUEST_URI}_%{QUERY_STRING}\.html -s
  RewriteRule .* cache/%{ENV:boostpath}/%{HTTP_HOST}%{REQUEST_URI}_%{QUERY_STRING}\.html [L,T=text/html]

  ### BOOST END ###

And paste it under the # RewriteBase / line in .htaccess file.

Set cache folder permissions

Go to admin/reports/status to see if you have any errors like this:

The default cache folder is created in the root of your site. If it is not created there, create it and give the web server permissions to write in it. For my setup the cache folder was created but I have to set the owner to www-data by running this command in my site root (in ubuntu):

sudo chown -R www-data:development cache

If you don't have development group in your system, then you could use www-data:www-data.

If the owner (www-data) has not permissions to write in this folder, then run this command:

sudo chmod -R 755 cache

The second error we fixed by disabling the Core Cache.

Refresh admin/reports/status page to see if the errors are gone.

Test if it works

Visit the frontpage with another browser (or logout) and you should see the static pages generated in the cache folder. For me the frontpage is generated in cache/normal/drupal.dev1/tut/d7/1509161747t/_.html and you can open that file with a browser to see the site frontpage served as html file.

Also you can see the cache comment in the source code of the page:

Page cached by Boost @ 2015-09-16 22:01:29, expires @ 2015-09-17 22:01:29, lifetime 1 day

Ignore cache and .htaccess for git

If you are using git, then you should add the cache folder to the .gitignore file:

# Ignore configuration files that may contain sensitive information.
sites/*/
settings*.php

# Ignore paths that contain user-generated content.
sites/*/files
sites/*/private

cache 

If you already have .htaccess in your version control, then u could ignore it with this command:

sudo git update-index --assume-unchanged .htaccess

Also run the same command in your test/live server and you can have unique .htaccess file for each server.

Additional settings and recommended extra modules

Go to admin/config/system/boost to edit the cache lifetimes and other settings.

Recommended extra modules:

Cache Expiration Expires updated content so you don't serve stale content.
Global Redirect Avoid having multiple URLs representing the same content.
Pathauto Generate path aliases.
Transliteration Takes Unicode texts and tries to represent it in US-ASCII characters.

Posted by Samuli Natri on 16 September 2015