Drupal 6 Performance Optimization Using DB Maintenance and Boost: Part 1

Exclusive offer: get 50% off this eBook here
Drupal 6 Performance Tips

Drupal 6 Performance Tips — Save 50%

Learn how to maximize and optimize your Drupal framework using Drupal 6 best practice performance solutions and tools

$23.99    $12.00
by TJ Holowaychuk Trevor James | March 2010 | MySQL Content Management Drupal Open Source PHP

In this two-part article by Trevor James and T J Holowaychuk, authors of Drupal 6 Performance Tips, we're going to install, configure, and utilize the DB Maintenance and Boost modules. DB Maintenance will allow us to maintain and optimize our MySQL database from within the Drupal admin interface. Boost will help us to speed up page load times on our site for our anonymous site users by using its sophisticated and advanced page, HTML, CSS, and JavaScript caching mechanisms. Both of these contributed modules will help you diagnose problems on your site and server as well as help to keep your site running smoothly and in an optimized fashion.

By the end of this article you will know how to install, configure, and use the following contributed performance modules:

  • DB Maintenance module
  • Boost

These are not required modules, but rather are recommended modules to add to your Drupal performance arsenal. The way this article will work is that we'll outline the purpose of each module, install and configure it, and then use it on a specific topic, for example, within your site. This will give you some practice using contributed Drupal modules and also a look at the variety of performance based modules that are available from the Drupal project community.

Using the DB Maintenance module

The DB Maintenance module can be used to optimize your MySQL database tables. Depending on the type of database you are running, the module allows you to use a function called OPTIMIZE TABLE, which troubleshoots and then optimizes various errors in your MySQL tables. For MyISAM tables, the OPTIMIZE TABLE will repair your database tables if they have deleted rows. For BDB and InnoDB types of tables the function will rebuild the entire table. You can use this module in tandem with phpMyAdmin to determine if you do or do not need to optimize your database tables. The benefit of this module is that it allows you to keep your database optimized and defragmented, similar to keeping your computer hard drive optimized and defragmented so that it runs faster, and you can do all this from the Drupal administrative interface.

The project page where you can download the module is here: http://drupal.org/project/db_maintenance.

Download the module tar.gz and extract it to your desktop. Then, upload the files through FTP, or upload and extract using a cPanel utility if your host provides this. The module should go in your /sites/all/modules directory.

Once you upload and extract the module folder, enable the module on your modules admin page and save your configuration. We'll use the version that's recommended for Drupal 6.x, which is 6.x-1.1. You can try out the beta version, but you should not run this beta version on a production level website unless you've tested it sufficiently in a sandbox environment.

Drupal 6 Performance Tips

Once you save your module configuration, you'll notice that the module adds a link to its settings and configuration page under your main Site configuration section. Go to Site configuration | DB maintenance to access the configuration admin screen for the module. The DB maintenance screen will contain a checkbox at the top allowing you to log OPTIMIZE queries. If you check this box, your watchdog log entries module will log all table optimization entries and give you detailed information on the tables that were optimized.

At the time of writing this article, the 1.1 version of the DB Maintenance module contained bugs that caused glitches with the method of adding this module's queries to the recent log entries or prevented this entirely. You may also experience these glitches. The module's developers are aware of the issues because they have been posted to the issue queue at http://drupal.org/ on the module project page.

Let's go ahead and check this box. You can then select the frequency with which you would like to run the optimization. The choices are daily, Run during every cron, Hourly, Bi-Hourly, Daily, Bi-Daily, Weekly, Bi-Weekly, Monthly, and Bi-Monthly. You can also click on the Optimize now link to force the optimization to occur immediately without scheduling in advance. We'll click on this link for the purpose of this demo, but in future you may want to schedule the optimization. We'll then run a cron job through the Status report, or a module such as Poormanscron, and the tables will be optimized.

Drupal 6 Performance Tips

Next, you can select the tables in your Drupal database that you want to optimize. A nice feature of this module is that it allows you to multi select database tables, only select a few tables, or just one table. This gives you the same flexibility and functionality as your phpMyAdmin tool, but you can run everything from within your Drupal interface. It's like a phpMyAdmin lite version right in your Drupal site. This is a preferred option for those developers who may not have immediate access to a client's phpMyAdmin or a host's database management utility.

Choose a selection of tables that you want to optimize, or select all the tables. For this demo I'm going to optimize all of my content type tables, so I'll select all of those. I'll also optimize my block tables:

blocks
blocks_roles
content_type_blog
content_type_book
content_type_forum
content_type_page
content_type_photo
content_type_poll
content_type_story
content_type_webform

Once you've selected the tables you want to optimize, click on the Optimize now link.

Drupal 6 Performance Tips

As with any module or optimization enhancement that you make to your Drupal site, it is good practice to run a full backup of your MySQL database before performing any maintenance, including optimizing tables using the DB Maintenance module. This way you will have a full backup of your data if you run into any issues that the module could potentially create. It's better to play it safe and perform the backup first.

Once you click on the Optimize now link, you should receive a message notifying you that the Database tables are optimized.

This concludes our discussion and walkthrough of using the DB Maintenance module. Let's now turn to the Boost module and use it to speed up our site page and content loads.

Using the Boost module

We' re going to turn our attention to the Boost module in this section. Boost is a contributed module that allows you to run incredibly advanced static page caching on your Drupal site. This caching mechanism will help to increase performance and scalability on your site, especially if it gets heavy traffic and anonymous page visits, and it is on a shared hosting environment. This is usually the first contributed performance-based module to turn to for help when you host your Drupal site on a shared server. Developers running Drupal sites on shared servers and running sites that serve predominantly anonymous Drupal users will definitely want to try out this module. It's also a fun module to use from a technical standpoint because you can see the results immediately, as you configure it.

The Drupal project page for the module is here: http://drupal.org/project/boost. There is a wealth of detailed information about the module on this project page, including announcements about upcoming conference presentations that focus on the Boost module, testimonials, install instructions, and links to documentation and associated modules that you may want to run alongside Boost. It is very popular and has quite a following in the Drupal development community. I definitely recommend reading about this module and all of its install and configuration instructions in detail before attempting to use it.

The install paragraph suggests reading through the module README.txt file before running the install for details on how the module works. There are also detailed instructions and documentation on the module here: http://drupal.org/node/545664.

Note that the one requirement to use this module is that your Drupal site must have clean URLs configured and enabled. It's a good idea to make sure you are running clean URLs on your site before you start installing and configuring Boost.

Additionally, there are some recommended modules that the developers encourage you to install in tandem with the Boost module. We will install two of these modules: Global Redirect and Transliteration. The Global Redirect module runs a number of checks on your website including the following:

  • Checks the current URL for a Drupal path alias and does a 301 redirect to the URL if it is not being used.
  • Checks the current URL for a trailing / and removes the slash if it's present in Drupal URLs.
  • Checks if the current URL is the same as the site's front page and redirects to the front page if it locates a match.
  • Checks to see if you are using clean URLs. If you do have clean URLs enabled, this module ensures URLs are accessed using the clean URL method rather than an unclean method (for example, ?q=user).
  • Checks access to the URL. If a user does not have permissions to view the URL, then no redirects are allowed. This helps to protect private URL aliases.
  • Checks to ensure the alias matches the URL it is aliasing. So, if you have a URL alias such as /about and this directs to node/23, then a user on your site can access the page using either of those URLs.
  • The Transliteration module removes white space and non-ASCII characters in your URLs. For example, it will try and add underscores to fill white space in a URL.

Installing and enabling these two modules will help remove glitches and errors in your site's path structure.

If you haven't already, we'll also take the time now to install the Poormanscron module and set up and configure automatic cron runs instead of having to continue running cron manually. We'll return to installing and configuring Poormanscron later in this article, but just keep it on your radar for now.

Let's go ahead and install the Boost module and take a closer look at some of its features.

Drupal 6 Performance Tips Learn how to maximize and optimize your Drupal framework using Drupal 6 best practice performance solutions and tools
Published: February 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

Installing and configuring Boost

Installation and configuration instructions are provided on drupal.org and also in the module's README file. Read these in detail before installing and enabling the module: http://drupal.org/node/545908.

Additionally, the module notes specify that the module will install and enable using smart defaults and these should work fine in most shared server environments.

Follow these steps to install the module(s):

  • Download the Boost module along with Transliteration, Global Redirect, and Poormanscron to your desktop. Unzip them and then upload to your /sites/all/modules directory. The latest version of Boost is 6.x-1.13, Transliteration is 6.x-2.1, and Global Redirect is 6.x-1.2.
  • Check to make sure you have clean URLs enabled and working correctly in your site at the settings page here: /admin/settings/clean-urls.
  • Go to your modules admin list and enable the Boost, Transliteration, Global Redirect, and Poormanscron modules. Boost will be in its own Caching section of the modules list.

Once enabled, you will see a series of messages loading on the modules page telling you that:

  • Existing filenames have not been transliterated.
  • Boost has been successfully installed. There will be a link to the module configuration settings page and a notification telling you that two blocks can be enabled to help you administer Boost, as well as a block to support stats.

It's nice that the Transliteration module informs you that it has not changed the URL paths of your previously posted Drupal pages. It's only going to affect new pages, and only if you enable and configure the module.

Click on the configuration settings link to launch the Boost admin page. You'll notice that the Boost configuration page is a tab that's part of the overall Drupal Performance admin section of the site. You can also get to the configuration page by going to Site configuration | Performance | Boost Settings.

Boost settings

Boost settings are split up into the following sections:

  • Boost File Cache
  • Boost cacheability settings
  • Boost directories and file extensions
  • Boost advanced settings
  • Boost crawler
  • Boost Apache
  • Clear Boost's Database

All of these sections and settings are explained in detail on the Drupal module project Installation and Settings page here: http://drupal.org/node/545908. We're going to look at most of these configuration settings in detail.

Boost File Cache settings

Most of the default settings will work for us here, but let's run through them all. We want to make sure that we have Boost – Static page cache set to Enabled. What this will do is store all of our Drupal-generated nodes and pages as static HTML files in a special cache directory in our Drupal site directory. Caching pages will help our site to deliver its pages and content in the fastest possible manner without turning to PHP or Drupal. This will provide us with improved performance, but you need to bear in mind that this type of caching will mostly benefit anonymous users of your website and not logged-in authenticated users who will be depending more on your Drupal functionality. In some cases, if you have a site that functions on multiple levels (for anonymous and authenticated users), it will be a trade-off to use this module, it is more advantageous to use it on sites that have more anonymous user access. On popular sites that allow mostly anonymous user visits, this module is a necessity.

Gzip page compression setting should be set to Disabled for our example. Page compression is normally handled by the Apache web server itself and you will not need to enable any additional page compression. If Apache is compressing pages, this can actually interfere with the Drupal page compression and crash your website, so do not enable this until you are sure Apache or the web server you are using is not performing page compression.

We' ll leave the Boost – HTML – Default minimum cache lifetime set to 1 hour for HTML, XML, and JSON.

You will also notice that you can easily clear your Boost cached data by clicking on the Clear ALL Boost cached data button, and your expired cached data by clicking on that button. Your screen should look something like this:

Drupal 6 Performance Tips

Boost cacheability settings

Scroll to this section and leave the defaults enabled—this includes Cache pages that contain URL Variables and Cache HTML documents that will cause Boost to cache all content in your Drupal pages. You can choose here to Cache your XML and JSON. When you select cache XML and JSON, the corresponding Boost – XML and Boost – JSON minimum cache lifetimes will be selectable in the Boost File Cache section. Let's go ahead and check the box to cache our XML and RSS feeds.

Let's leave the Cache .css and Cache .js checked, so our CSS and JavaScript files are also cached.

Optionally, you can specify which pages on your site to cache by adding the page URLs to the Pages box and then by specifying that you only want to cache those pages. You can also enable PHP code here if you have specific PHP code snippets you want to run. This is similar to how the core Drupal Block configuration works. You can see that the module developers spent a lot of time working out the best workflow for the configuration so that it matches other core Drupal module configuration pages in usability and workflow.

At this point your screen should look similar to this:

Drupal 6 Performance Tips

Boost directories and file extensions

This is a very important section. Here you tell Boost module where to store your cached data and files. The README file also explains this section in detail and the corresponding tweaks we will eventually run to our HTACCESS file to enable Boost to work.

We need to create a folder in our site's directory for our cached material. Our folder will be called cache, but it needs to exist on the site. So if this folder and the subsequent path folders do not exist, you need to create them and make sure they are writeable on the web server. The /cache folder should be created in the root directory of your Drupal site. Create a folder called cache at the root of your Drupal installation alongside your other Drupal folders such as /files, includes, misc, and so on. Make sure the /cache folder is writeable. This means setting your permissions for the /cache folder to 777. Drupal or the Boost module may create the folder automatically if you do not create the folder in advance, but it's a good idea to create it anyway.

You can create the cache folder through FTP or a file manager utility like cPanel. The directory we create must be named cache, and the Cache file path is set to cache/normal/variantcube.com/fire. Then make sure that this directory and its sub folders are created on the site and are writeable. So I'll go ahead and create the cache, normal, variantcube.com, and fire folders.

Your screen should look something like this:

Drupal 6 Performance Tips

You can also specify storage locations for your HTML, XML, and AJAX output, and static storage for your CSS and JS files if you want to keep those in a specific location of the cache folder. I will leave these as the default settings for our examples and let Boost decide where it wants to store each set of files within the path and directory structure I've created.

Before moving on to enabling the Boost default admin blocks, save the configuration and then create the .htaccess file. First, save configuration on your main Boost settings configuration page. This will save the configuration work we've just enabled. Having done this, let's move on to make a required tweak to our .htaccess file.

HTACCESS file tweaks

There is one important tweak that we need to carry out on our .htaccess file. We need to do this in order to make our Boost configuration work. So far in this section on the Boost module, this is the most complex configuration step we've taken. Follow these steps in order to tweak your .htaccess (this is also explained in detail in the README file in the module folder):

  1. Back up your original .htaccess file in your Drupal install directory so that you have a backup in case of problems.
  2. Copy the custom generated htaccess rule from Administer | Site configuration | Performance | htaccess rules generation or by clicking on the tab in your Performance section that says Boost htaccess rules generation.
  3. When you click on the button, you'll see a text box with the rules presented as a big block of code. Copy this code and paste it into your .htaccess file (through editing mode in either FTP or cPanel). The module help text here tells you to copy this rule and paste it below the # RewriteBase / and above the # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.

    So you should have something in your current .htaccess that looks like the following:

    # RewriteBase /
    ------------ paste the rules here ------------
    #Rewrite URLs of the form 'x' to the form 'index.php?q=x'
  4. Paste the code in the position indicated paste the rules here. Make sure you look for the # RewriteBase / commented line of code and then paste in your rewrite rules immediately following that comment line. Go ahead and do this now.

The resulting code looks like this:

### BOOST START ###
AddDefaultCharset utf-8
<FilesMatch "(.html|.xml)$">
<IfModule mod_headers.c>
Header set Expires "Sun, 19 Nov 1978 05:00:00 GMT"
Header set Cache-Control "no-store, no-cache, must-revalidate,
post-check=0, pre-check=0"
</IfModule>
</FilesMatch>
<IfModule mod_mime.c>
AddCharset utf-8 .html
AddCharset utf-8 .xml
AddCharset utf-8 .css
AddCharset utf-8 .js
</IfModule>
<FilesMatch ".html.gz$">
ForceType text/html
</FilesMatch>
<FilesMatch ".xml.gz$">
ForceType text/xml
</FilesMatch>
<FilesMatch ".css.gz$">
ForceType text/css
</FilesMatch>
<FilesMatch ".js.gz$">
ForceType text/javascript
</FilesMatch>
# Gzip Cookie Test
RewriteRule boost-gzip-cookie-test.html cache/perm/boost-
gzipcookie-test.html.gz [L,T=text/html]
# NORMAL - Cached css & js files
RewriteCond %{DOCUMENT_ROOT}/fire/cache/perm/%{SERVER_
NAME}%{REQUEST_URI}_.css -s
RewriteRule .* cache/perm/%{SERVER_NAME}%{REQUEST_URI}_.css
[L,QSA,T=text/css]
RewriteCond %{DOCUMENT_ROOT}/fire/cache/perm/%{SERVER_
NAME}%{REQUEST_URI}_.js -s
RewriteRule .* cache/perm/%{SERVER_NAME}%{REQUEST_URI}_.js
[L,QSA,T=text/javascript]
# 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$ [OR]
RewriteCond %{REQUEST_URI} (^/fire(admin|cache|misc|modules|s
ites|system|themes|node/add))|(/(comment/reply|edit|user|user/
(login|password|register))$) [OR]
RewriteCond %{HTTP_COOKIE} DRUPAL_UID [OR]
RewriteCond %{REMOTE_ADDR} ^74.220.207.144$ [OR]
RewriteCond %{HTTPS} on
RewriteRule .* - [S=2]
# NORMAL
RewriteCond %{DOCUMENT_ROOT}/fire/cache/normal/%{SERVER_
NAME}%{REQUEST_URI}_%{QUERY_STRING}.html -s
RewriteRule .* cache/normal/%{SERVER_NAME}%{REQUEST_URI}_%{QUERY_
STRING}.html [L,T=text/html]
RewriteCond %{DOCUMENT_ROOT}/fire/cache/normal/%{SERVER_
NAME}%{REQUEST_URI}_%{QUERY_STRING}.xml -s
RewriteRule .* cache/normal/%{SERVER_NAME}%{REQUEST_URI}_%{QUERY_
STRING}.xml [L,T=text/xml]
### BOOST END ###

Once it's pasted in my .htaccess file, I should see the code start below my #RewriteBase / and it will look like this (this is just an excerpt):

# RewriteBase /
### BOOST START ###
AddDefaultCharset utf-8
<FilesMatch "(.html|.xml)$">
<IfModule mod_headers.c>
Header set Expires "Sun, 19 Nov 1978 05:00:00 GMT"
Header set Cache-Control "no-store, no-cache, must-revalidate,
post-check=0, pre-check=0"
</IfModule>
</FilesMatch>
<IfModule mod_mime.c>
AddCharset utf-8 .html
AddCharset utf-8 .xml
AddCharset utf-8 .css
AddCharset utf-8 .js
</IfModule>
<FilesMatch ".html.gz$">
ForceType text/html
</FilesMatch>
<FilesMatch ".xml.gz$">
ForceType text/xml
</FilesMatch>
<FilesMatch ".css.gz$">
ForceType text/css
</FilesMatch>
<FilesMatch ".js.gz$">
ForceType text/javascript
</FilesMatch>

Refresh your Performance settings page in Drupal. That's it! You're now ready to use the Boost module and start speeding up your page loads. Let's start using it as an anonymous user on our site.

In the next part we will cover testing the Boost configuration, Boost and Poormanscron, Configuring Poormanscron, Clearing the Boost cache and Boost admin and stats blocks.

>> Continue Reading: Drupal 6 Performance Optimization Using DB Maintenance and Boost: Part 2

[ 1 | 2 ]

If you have read this article you may be interested to view :


Drupal 6 Performance Tips Learn how to maximize and optimize your Drupal framework using Drupal 6 best practice performance solutions and tools
Published: February 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


TJ Holowaychuk

TJ Holowaychuk, president of Vision Media is an avid contributor to the Drupal project as well as to other open-source applications. Contributing over 16 Drupal modules, several themes, and having fully designed, developed, and maintained hundreds of Drupal websites he has displayed a wealth of knowledge across different aspects of the industry.

Trevor James

Trevor James is a Drupal developer residing in Middletown, MD, USA. Trevor has been using Drupal intensively since 2007 and designing websites for over 15 years using a combination of HTML, CSS, ColdFusion, PHP, jQuery, and JavaScript.

Trevor's focus is on building Drupal-based web applications and portals for education, non-profit, and medical systems, and small business environments. He is interested in the best methods of integrating web services with Drupal sites, optimizing Drupal sites' performance, and using Drupal content types, Views, Panels, and other contributed modules to develop front-end interfaces that support data intensive websites.

He loves teaching people about Drupal and how to use this excellent open source content management framework. Trevor authored the following Packt books:

Trevor created a 14-hour video tutorial series titled Drupal 7 for Virtual Training Company (VTC) in 2011. The video is available via the VTC website at http://www.vtc.com/products/Drupal-7-Tutorials.htm.

Books From Packt


Drupal 6 Search Engine Optimization
Drupal 6 Search Engine Optimization

Drupal 6 Content Administration
Drupal 6 Content Administration

MySQL Admin Cookbook
MySQL Admin Cookbook

iReport 3.7
iReport 3.7

jQuery 1.4 Reference Guide
jQuery 1.4 Reference Guide

Drupal Multimedia
Drupal Multimedia

Apache Geronimo 2.1: Quick Reference
Apache Geronimo 2.1: Quick Reference

Drupal E-commerce with Ubercart 2.x
Drupal E-commerce with Ubercart 2.x


Your rating: None Average: 5 (1 vote)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
b
X
e
j
a
e
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software