Install W3 Total Cache

Purchase a pre-installed image of this tutorial and check out our new knowledge base WordPress Now.

W3 Total Cache

Installing the W3 Total Cache Plugin

W3 Total Cache is an outstanding WordPress plugin. It’s free and provides a tremendous performance optimization for your site. You can learn more about W3 Total Cache at its plugin site.

Log in to your WordPress administration page at http://yourdomain.com/wp-admin using the username and password you configured during setup. Select Plugins -> Add New from the left sidebar menu. Or, visit http://yourdomain.com/wp-admin/plugin-install.php. Search for W3 Total Cache.

Here’s what the process will look like:

wordpress tutorial image
wordpress tutorial image
wordpress tutorial image
wordpress tutorial image

WP Engine

Configure W3 Total Cache Settings

Because of its power and sophistication, W3 Total Cache has a wide variety of settings which can seem overwhelming. Below, I provide some recommended settings that make getting started easier. I’ve also provided instructions for downloading my settings and importing them if you’d like to start that way. I’ve documented the Amazon CloudFront CDN configuration separately as it requires a series of distinct steps.

Don’t get overwhelmed. Start with the General Settings shown below and go from there.

Alternatively, W3 Total Cache offers paid installation and support from $75 – $200 and up. You can reach them through its plugin Dashboard page or their website. This is a great plugin so I feel good about recommending them although I do not have experience with their support. You may also want to review the W3 Total Cache installation overview and FAQ on the plugin website for more details.

At a high level, I recommend activating the following settings which I detail more fully below:

  • Page Caching – Caches pages for faster loading and more efficient delivery (separate from Varnish)
  • Minify – Shrinks and combines JS and CSS files for faster loading and more efficient delivery.
  • Object Cache – Speeds up generation of content by WordPress.
  • Browser Cache – Properly tags content so that browser clients minimize downloads of pages.
  • Reverse Proxy – Instructs W3TC to purge the Varnish cache when posts, pages, et al. are updated.
  • Amazon CloudFront CDN – Ensures the delivery of static files and images quickly, reliably and geographically near to your visitors.
  • Miscellaneous – Use Sitemap for caching to pre-cache high priority pages.
Installing Supporting Components

Before we configure W3 Total Cache, let’s install and activate a number of Apache components that it will require. Again, connect to your instance and perform the following steps:

sudo apt-get install php5-memcache memcached php-pear build-essential php5-tidy php5-curl apache2-dev
sudo pecl install memcache
sudo pecl install apc

Edit your php.ini file to activate memcache:

sudo nano /etc/php5/apache2/php.ini

In the Dynamic Extensions section, add the lines below:

extension=apc.so
extension=memcache.so

Tell Apache to activate these modules:

sudo a2enmod rewrite
sudo a2enmod expires
sudo a2enmod headers
sudo service apache2 restart

Edit your wp-config.php file to enable caching:

sudo nano /var/www/wordpress/wp-config.php

Add the following WP_CACHE definition above the stop editing line:

define('WP_CACHE', true);
/* That's all, stop editing! Happy blogging. */
Reviewing Configuration Status

At this point, it’s useful to visit the W3TC plugin Dashboard and check that all the proper components are active. From the left sidebar, click Performance -> Dashboard, then click the Compatibility Check button. It should look something like this:

wordpress tutorial image
wordpress tutorial image

I also prefer to enable Fancy Permalinks for URL formats optimized for search engines (SEO). From the WordPress Admin Dashboard, select Settings -> Permalinks, choose Day and Name and click Save Changes:
permalinks

Note: you may save some time if you choose a theme and install the other plugins you plan to use before completing the rest of the W3 Total Cache configuration process. Changing themes and adding plugins will likely require you to purge your W3 Total Cache and re-export media files to your CDN.

General Settings

W3TC generally works well with its default settings. So, we’ll primarily need to configure the General Settings page and not go into each individual feature area.

Visit the W3TC General Settings page. From the left sidebar, click Performance -> General Settings e.g. http://yourdomain.com/wp-admin/admin.php?page=w3tc_general. We can perform high level set up here but then we’ll also do more detailed set up on individual settings pages. I leave unchecked/off “Toggle all caching types on or off (at once)” in the general box.

Enable Page Cache with Opcode: Alternative PHP Cache (APC):
general-page-cache

Enable Minify with the following settings. I use manual mode because minify can break some themes if your settings are too aggressive (however, your chosen theme may work fine):
general-minify

I leave unchecked/off Database Caching as this is recommended only if you do not activate Object Caching.

Enable the Object Cache:
general-object-cache

Enable the Browser Cache:
general-browser-cache

We will activate the CDN but we describe it separately: Activating Amazon CloudFront.

Enable Reverse Proxy:
Configure W3TC to regularly purge the Varnish Cache from 127.0.0.1 (localhost).

Reverse Proxy - Purge Cache

Reverse Proxy – Purge Cache

Page Cache Settings

From the left sidebar, click Performance -> Page Cache. Scroll down to the Cache Preload box. Enable “Automatically prime the page cache”. Enter your sitemap URL. Note: You will need to have installed the Google XML Sitemaps plugin before this will function properly (see Install Themes and Plugins).

cache-preload

If you use a CAPTCHA on contact forms, you’ll want to include the path to these pages in the cache exclusion. Scroll down to the Advanced Setting box. Add your file path e.g. contacts here:

advanced-never-cache

User Agent Groups

Because my theme is responsive, I cache mobile and tablet user agents separately. If you wish to do this, enable High and Low User Agent Groups:

useragentgroups

Referrer Groups

I also enable Referrer Groups for search engines:

referrer-groups

Minify Settings

Minify compresses your HTML, JS and CSS code and combines files when possible. This speeds up delivery of your pages and reduces bandwidth. However, it can sometimes break theme code.

You may have to adjust the settings for your chosen theme. I found that by turning off inline CSS minification, my theme works well. From the left sidebar, click Performance -> Minify:

minify

Importing Example Settings (optional)

If you prefer to start by importing my settings file, follow these steps:

  1. Download the example settings file
  2. Visit the W3TC General Settings e.g. left sidebar, Performance -> General Settings and scroll to the bottom area with Import/Export Settings (see below).
  3. Beside Import configuration: click Choose File and select the example settings file you downloaded. Then, click upload.
  4. Browse your W3TC settings and ensure they make sense for your WordPress site
  5. You may need to customize settings for your site after the import e.g. Google PageSpeed, Amazon CloudFront CDN, etc.

import-configuration

CDN

It’s definitely worthwhile to activate a CDN. Follow the instructions on Activating the Amazon CloudFront to do so.

Monitoring

If you’re interested in performance monitoring with New Relic, follow the instructions described on Optimizing Your Instance Using New Relic.

If you’re interested in adding support for Google Page Speed, follow the instructions described on Optimizing Your Instance – Google Page Speed.

What’s Not Active

I’m not currently using CloudFlare, but would like to try it in the near future. If you’ve had experience with the service, please share your experience with us via comments. Since we’re using Amazon’s CloudFront for CDN, CloudFlare’s benefits are fewer than if we used it exclusively.

I’ve also not tried activating some of the custom performance optimization libraries supported by W3 Total Cache e.g. xcache and eAccelerator.

Please feel free to post corrections, questions or comments below. You can also follow me on Twitter @reifman or email me directly.

Continue to Activate Amazon Cloudfront CDN

16 Comments

  1. When using “sudo apt-get install apache2-dev” I receive: E: Unable to locate package apache2-dev

    Any idea what’s going on there?

    Reply

    1. Is it perhaps called “apache2-devel” ??

      Reply

      1. apache2-dev worked for me on Ubuntu 12.04.

        Reply

    2. I think the package name should be apache2-prefork-dev
      At least this is what I found for Ubuntu 13.04

      Reply

  2. try sudo apt-get update to update your packages

    Reply

    1. I had to install php5-dev instead of apache2-dev. Are you sure that’s not what you meant?

      I also got this error:

      In file included from /tmp/pear/temp/APC/apc.c:45:0:
      /usr/include/php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h: No such file or directory
      compilation terminated.

      I also had to run this command:

      apt-get install libpcre3-dev

      Reply

      1. It’s been a while since I ran through this on a blank machine. I don’t believe there is an error – but it’s possible. It could also be differing versions of unix. If you’re on 10.04 let me know and I’ll add php5-dev to the fragment

        Reply

        1. I think maybe the problem is that I used the Ubuntu 12.10 AMI instead of the Ubuntu 12.04 LTS image.

          Reply

      2. cheers for this

        Reply

  3. Mathew Davies June 16, 2013 at 1:09 am

    I found it necessary to install the phpize package, but that was straightforward. Great guide here!

    Reply

  4. this was a great help, thank you.

    Reply

  5. excellent info… thanks for posting

    Reply

  6. Naveen Nallagatla September 20, 2013 at 11:26 am

    Hi,
    I followed this procedure step by step upto installing the supporting components for W3Total Cache. Then I realized I has missed out installing the W3TC plugin from the wordpress admin area. Now I am getting a server error (500) when I try to access the site or the admin panel. Could anyone help me solve this? Thank you in advance.

    Reply

  7. Hi Jeff,
    Thanks for the awesome post. I have been playing with EC2 for the past few days and came upon your tutorial.

    I ran into an error during:

    sudo pecl install apc

    a “make” error

    once I am in W3, the only cache method I see is the memcached.
    The APC is not available.

    Can you provide some input regarding the error step above?

    Thanks!

    Reply

    1. Restart Apache:
      sudo service apache2 restart
      Install APC with the proper command:

      apt-get install php-apc

      That should fix it.

      Reply

  8. This is very complete guide.

    Thanks very much for sharing.

    Reply

Leave a reply

Your email address will not be published.