WPEngine Affiliate ProgramUpdate: There is now a much more complete tutorial to installing Varnish with W3 Total Cache and Amazon CloudFront here.

[teaser]This post will help you speed up and optimize your WordPress blog on Amazon AWS EC2 with Apache, Varnish and W3 Total Cache.[/teaser]

I recently set up this website on WordPress to consolidate my reference materials with my blogging efforts. For a long time, I’d used TypePad but as that service is no longer in active development, I decided to give WordPress another try.

Overall, I’m impressed with the progress WordPress has made over the years. However, running it on Amazon AWS EC2 with a sophisticated template (MySiteMyWay) turned out to be quite slow.

Now, as you can tell, my blog is quite speedy. Here are the solutions I ended up using.

Basically, I run Varnish on port 80, the public http port. I run Apache on an internal port e.g. 8080. All public requests come into Varnish – if the page does not exist in the cache, it requests the page from Apache.

I set up W3 Total Cache to cache pages from my sitemap in the background. So, quite often, pages are loaded from the Varnish cache instantly. I’m storing my images, theme files and minified JS/CSS files in the Amazon CloudFront S3 bucket. In W3 TC, I have page cache, minify, object cache, browser cache, Varnish purging and CDN activated.

This setup is quite speedy and I’m very satisfied. The configuration files I’ve used are listed below the notes.


  • CloudFlare: I may also add CloudFlare configuration soon – though it’s CDN features are redundant to CloudFront.
  • NGINX: I experimented with NGINX but as I’m not as expert at this, I did not have much success.
  • Amazon CloudFront: I found better performance by using only one CNAME rather than four. It seemed that browser clients were getting held up when I spread my CDN over multiple domains.
  • Do not use multisite: I initially set it up on WordPress Multisite. I think in general the disadvantages of Multisite outweigh the advantages. It was difficult and time consuming, but I decided to switched back to single site model. Many plugins don’t work well on multisite which is also a problem.

Configuration Files

Apache Ports Configuration
Apache Site Configuration for WordPress

Default Varnish Configration e.g. /etc/default/varnish
Varnish VCL Configuration e.g. /etc/varnish/default.vcl

Posted by Jeff Reifman

Jeff is a technology consultant based in the Pacific Northwest.


  1. I have had CloudFront CDN setup with W3 Total Cache on a few sites for a few months, but I have not put in ServerAlias *.yourdomain.com in the Apache configuration. I can’t anyway, as there are other sites using a subdomain.

    What is the ServerAlias for? Does Apache really need to accept the CDN CNAMEs, as it seems to run fine with the ServerAlias.


    1. Using CNAME’s is not necessary, it just provides you with nicer looking url’s for you static content.


      1. I have multiple CNAMEs (cnd1. cdn2 cdn3. etc) so that files are downloaded concurrently by the browser. In W3 Total Cache it reserves certain CNAMEs for javascript in head/body and css.


  2. When using Varnish, do I still need to use disk cache or disk enhanced for page cache?


  3. Seems to be some issue with the above as starting varnish gave the following error:

    * Starting HTTP accelerator varnishd [fail]

    Message from VCC-compiler:

    Unterminated string at

    (‘input’ Line 25 Pos 52)

    set req.http.Cookie = regsuball(req.http.Cookie, “(^|;s*)(_[_a-z]+|has_js|is_


    Running VCC-compiler failed, exit 1

    VCL compilation failed


  4. Please ignore my below message – was a copy paste error due to a next line character getting in – working very well – thanks!


Leave a reply

Your email address will not be published. Required fields are marked *