WordPress under Nginx and Varnish with W3TC

When it comes to my personal Internet presence, I am an inveterate tinkerer. I like to experiment and I like to run things my way. (This is in contrast to my professional recommendations as a consultant, where I like conservative and tested.)

As such, despite having no problems with my previous host (Laughing Squid, which I still recommend), I decided to switch to a Virtual Private Server (VPS) so that I could have more flexibility and control over my server environment. I selected VM Storm based on a review of  “low-end” VPS providers (since this is my personal tinkering platform I don’t need to pay extra for a high-end name). I then added Nginx as my Web server, Varnish as a front-end cache, WordPress for blogging, and W3TC as a WordPress performance enhancer.

The Virtual Private Server

Remember that with a basic VPS, no one administers the system for you (unless you pay extra, of course). You need to make your own size estimates, then install and configure your Web server and everything else (the OS itself was installed by the provider). This, of course, is exactly what I was looking for, since I wanted to experiment.

While I probably could have made it work with an even more compact environment, I went with 512 MB of memory to give me some breathing room. I decided on Ubuntu 11.10 Server because I’m familiar and comfortable with Ubuntu, and because there are lots of online Ubuntu resources and help available.

The Web Server

I’m most familiar with Apache, a free, open-source, incredibly flexible and powerful Web server. But since I was interested in experimenting, I decided to try an alternative: Nginx(“engine x”), a fast and compact HTTP server that now powers sites like WordPress.comand CloudFlare. It’s known for its resource efficiency and speed, so it’s a perfect choice for smaller VPS setups (and it too is open source and free).

(Note: I thought about Lighttpd, another fast and compact Web server, but Nginx proved be easier for me to get going the way I liked.)

The Front-End Cache

Nginx is fast, but I wanted more. So I put Varnish in front of Nginx. What’s Varnish?

Varnish Cache is an open source, state of the art web application accelerator. You install it on your web server and it makes your website fly.

Basically, it’s a server-side caching proxy. It listens for HTTP requests, serves them really fast if they’re in its cache already, or forwards the request to Nginx if they’re not. Want to survive a Slashdotting? Use Varnish. (And then add CloudFlare for good measure!)

The Database

I went with MySQL (of course). It works. I added phpMyAdmin to help administer it, and put that behind Nginx running SSL with a self-signed certificate. I could have bought an SSL cert, but what’s the point? I know who I am, and no one else needs SSL access to my install anyway. (I’m not selling stuff through my site.)

The Software

While I could have experimented with other solutions for blogging, I wanted to stick withWordPress. It’s flexible, extensible, and nice to work with. It isn’t always the fastest or most optimized thing out there, but that’s what Nginx and Varnish are for!

WordPress was really designed for a LAMP environment: Linux, Apache, MySQL, and PHP. So replacing Apache with Nginx had the potential to create some challenges–which it did! But fortunately, Nginx is an increasingly popular choice, and one of the most powerful WordPress plugins out there (W3TC–W3 Total Cache) has added support for it, which made everything easier.

W3 Total Cache

W3TC is more than a cache. It bills itself as a “performance framework,” and although I often prefer the Unix “small-tool” philosophy, W3TC really shines. To be honest, it’s options for performance enhancements encouraged me to think about using a VPS in the first place, just so I could play with them. It handles disk and database or object caching (using memcache or PHP APC), content-delivery networks (like Amazon CloudFront), and even Varnish cache purging.

Resources and How-To Guides



Post comment as twitter logo facebook logo
Sort: Newest | Oldest
Chris Gilligan 5 pts

kris, I found the issue: cookie. using your suggested reading list, I implemented the vcl from sample varnish config and was able to get age and cache hit to work properly. thank you for sharing your resources.

krisnelson 5 pts moderator

 Chris Gilligan Excellent! I'm glad you were able to find a solution. I was about to suggest that I *thought* it was varnish taking care of this--but I also switched a few weeks ago to QuickCache instead of W3TC (simply because it is simpler, and with Varnish in the mix, I didn't feel I needed all that W3TC offered), so I was just about to try to figure out how that might have impacted our situations. Anyway, glad you figured it out!

My latest conversation: Nineteenth-century America was not a libertarian utopia

Chris Gilligan 5 pts

 krisnelson   Chris Gilligan thanks kris. here's a link to the topography of the AWS system I am setting up for a Wordpress hosting server:

http://chrisgilligan.com/consulting/load-balancing-virtualmin-wordpress-hosting-server-varnish-aws/

 

I will have to try this with nginx as well... I have a development VPS server set up with nginx and it is working well... so I will have to move it into production soon.

Chris Gilligan 5 pts

kris, how have you been able to get w3 total cache to properly set age?

I only ever see

X-Cache: MISS

age: 0

in my headers.

I am running apache backend, but since 3tc is setting the headers, should this not work same as yours?

Maybe it's a php setting? I remember reading something about php settings causing age:0

Related Posts


About Kristopher Nelson


I'm currently a graduate student of the history of law and technology at the University of California, San Diego. I also provide law and technology consulting services. Additionally, I'm a non-practicing lawyer and former developer/sysadmin at a biotech non-profit. For more about me and my work, see krisnelson.org or my Google Profile.

Post Metadata


Post title: WordPress under Nginx and Varnish with W3TC

Authored by: Kristopher Nelson

Categorized as: blognews

Tagged with:

Permalink: http://inpropriapersona.com/wordpress-under-nginx-and-varnish-with-w3tc/

Shortlink: http://wp.me/pxgNP-1c9