Making WordPress Faster with Apache, Varnish and W3 Total Cache on Amazon AWS EC2 with CloudFront CDN
Update: There is now a much more complete tutorial to installing Varnish with W3 Total Cache and Amazon CloudFront here.
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.
- Amazon AWS EC2, CloudFront CDN, S3 – Three year reserved Small instance. Estimated monthly cost about $10 per month.
- WordPress 3.x
- Apache 2 on Ubuntu 12.0.4
- Varnish 3.x
- W3 Total Cache Plugin
- Google Sitemap Plugin
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.
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