Testing Nginx with APC, Varnish, WordPress and W3 Cache on a 128MB VPS

Finally got a bit of time (OK, I have forced myself to take some time) to get my VPS WordPress dream config sorted out and wrap it up in a handy bash script so that I can roll it out across my clients websites easily.

After following various tutorials and picking certain peoples brains, I think I finally have arrived at the bees knees as they say (or do they?)!

The configuration

The dream config breaks down as follows:

  • 128MB VPS (Virtualbox for testing, KVM or XEN for production
  • Debian 6 (Squeeze)
  • PHP5 with FPM (via dotdeb)
  • Nginx web server
  • APC Optcode cache
  • Varnish Caching Proxy
  • WordPress with W3 Cache plug-in
  • and for good measure Cloudflare CDN/Proxy

I will write up an easy to follow, cut-and-past tutorial soon – promise! And publish my script once it’s done and working. Until then, please check out the following comparison between Nginx+Varnish and Nginx without Varnish. The difference is clear!

This is with WP (latest) and W3 Cache active, running on APC with browser caching.

The test were run with Apache Bench (apt-get install apache2-utils).

Nginx (with APC & W3C)

ab -kc 10 -n 1000 http://localhost:8080/

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)

Server Software: nginx/1.0.2
Server Hostname: localhost
Server Port: 8080

Document Path: /
Document Length: 0 bytes

Concurrency Level: 10
Time taken for tests: 40.789 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Keep-Alive requests: 0
Total transferred: 263000 bytes
HTML transferred: 0 bytes
Requests per second: 24.52 [#/sec] (mean)
Time per request: 407.894 [ms] (mean)
Time per request: 40.789 [ms] (mean, across all concurrent requests)
Transfer rate: 6.30 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.2 0 37
Processing: 235 407 30.6 404 534
Waiting: 235 407 30.6 404 534
Total: 235 407 30.7 405 534

Percentage of the requests served within a certain time (ms - milliseconds)
50% 405
66% 417
75% 424
80% 428
90% 444
95% 459
98% 484
99% 496
100% 534 (longest request)

Nginx and Varnish (with APC & W3C)

ab -kc 10 -n 1000 http://localhost/

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)

Server Software: nginx/1.0.2
Server Hostname: localhost
Server Port: 80

Document Path: /
Document Length: 6054 bytes

Concurrency Level: 10
Time taken for tests: 1.299 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 1000
Total transferred: 6419989 bytes
HTML transferred: 6054000 bytes
Requests per second: 769.98 [#/sec] (mean)
Time per request: 12.987 [ms] (mean)
Time per request: 1.299 [ms] (mean, across all concurrent requests)
Transfer rate: 4827.39 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 13 113.2 1 1139
Waiting: 0 13 113.2 1 1139
Total: 0 13 113.2 1 1139

Percentage of the requests served within a certain time (ms - milliseconds )
50% 1
66% 1
75% 2
80% 2
90% 2
95% 3
98% 19
99% 1137
100% 1139 (longest request)

Summary

On its own Nginx (with APC and W3C) takes 40s to run a test with 1000 requests. With Varnish in front of it is only takes 1.2s to serve the same number!

On its own Nginx will handle 24.52 requests per second, whereas with Varnish in front it will handle 769.98!

On its own Nginx will transfer 6.30 Kbytes/sec with Varnish it will push 4,827.39 Kbytes/sec!

The majority of requests (90%) were served within 444ms with just nginx but just 2ms with varnish. This is from localhost of course.

Now imagine the whole thing protected and CDN’d by Cloudflare and you got yourself a website that can truly fly.

Cushty! ;D

PS: Thanks to Daniel Miesler’s post for inspiration. There are more people to thank but I’ll save that until the next post.

Coming Soon

Related posts in this (little) series:

If you like this page, please share it

  • http://twitter.com/javipas JaviPas

    Really promising! I also read the Daniel Miesler post and I wanted to set up a server this way, but I’d appreciate to be able to follow a detailed step by step guide. Daniel’s post is great, but he doesn’t elaborate some of the steps and that can lead to mistakes and poor behaviour :(

    I am really looking forward to see that detailed guide. I have several WordPress blogs on Linode VPS, but their performance is not that perfect. 

    I use Nginx, PHP-FPM and APC, but I must optimize everything. It would be great to know what should be the changes when using a 256 or 512 MB RAM machine, with 2-core or 4-core configurations (I know this makes a difference on config files). 

    Regards!

    • http://www.axelsegebrecht.com/ Axel Segebrecht

      Hi Javi, thanks for your feedback. I share your comments on Daniel’s post and many others like it. But then there is never a perfect solution that suits all cases.

      I have spent some time reading up and trying out lowendbox.com ideas but somehow most write ups omit bits and pieces which then lead to hours/days of fun trying to figure out what’s gone wrong.

      Anyway, I’m just completing a beta VPS for this website and noted down all the steps. That’s all of them, with instructions but no waffling. So anyone following them will end up with what I got.

      Once that’s done and the tweaking finalised (if that can ever be the case!), I’ll turn it into a shell script. The trick is finding the right settings for MySQL, nginx and fpm primarily, and apc and varnish secondly to fit a 128MB KVM or XEN VPS.

      • http://twitter.com/javipas JaviPas

        Yes, most of the times when someone writes a guide to set up a server they do from their specific case, that’s completely normal, and when you try to apply those steps, something often goes wrong or simply works but not how you could expect. 

        So if you could write your case, but also what would you try to do/modify on a different server (more memory, more cores?) would be specially interesting. I’ve seen several scripts (Linode has its own StackScripts, I don’t know what VPS you’re using) but I prefer to do everything step by step, just to avoid problems that are due to different configurations. 

        So… let’s publish that great guide soon! :D Any ETA?

  • Dudeist

    Nice writeup! I’m also looking forward to a detailed step by step tutorial and/or a bash script to accomplish the installation. As JaviPas before me mentioned, the Daniel Meissler post leaves some important bits out and after a few hours of trying I still can’t get it to work. I’m using a LNMP script and tried to add varnish on top of it but think my default.vcl is not configured correctly. Oh well, I’m done for today.

  • http://www.axelsegebrecht.com/ Axel Segebrecht

    Thanks for the feedback and interest in this folks. I’m just tweaking my ‘tutorial’ and have gone through it a couple of times now on a few low-end vps of various providers. Pitfalls are that some packages won’t install via apt because of the lack of memory (not no xen but on openvz/kvm).

    Thinking of installing from source instead which should also make the whole thing more streamlined and faster. However this would mean loosing the easy-of-use aspect for some people.

    I’m also getting a new dedicated server to run my customers vps on, which I will incorporate in the tutorial. For those interested in setting up their own vps hosting.Turns out the margins are tiny though and unless you charge a decent amount (~£5 incl for a 128MB) you are looking at not making anything. So how are all those cheap vps providers still in business?

    Simple, if you have several boxes and thousands of customers it does work but the profits are still small for a lot of work. Easy to say ‘no support’ but when customers need it they will demand it regardless of whether you offer it or not! Also, cheap hardware is the key to running this which in turn will bring its own set of problems.So watch out and don’t fool yourself that you can run a customers or your own business site on a tiny, cheap vps with no support on low-end hardware. It’s all good fun until cheap hardware breaks and thousands of customers cry their hearts out on twitter et al… .

    ETA on tutorial: another week or two
    ETA on script: another month

  • http://twitter.com/javipas JaviPas

    Great news Axel, and your comments on cheap VPS are interesting as well. I personally use Linode (different  configurations depending on the web site running on them) but what you say it0s absolutelly true. Everything is nice and pretty, until a problem arises and the client wants your head :(

    The tutorial will be a fantastic starting point, I’m sure. I’ll try to set up the server these days even though the tutorial isn’t yet published. Let’s see if I can go somewhere. 

    Regards!

  • RavanH

    Hi Axel, thanks for sharing your experience with Nginx / APC / Varnish. :)

    Do you have any impression of drop in performance when running without W3TC/APC?

    Oh, and I would REALLY appreciate that script… And the tuto would be very welcome too. Any news?

  • Erlend Sogge Heggen

    Very interested in that tutorial.

    Also, What is the point of running W3TC if you’re already running APC? After we started running APC on our site we didn’t see a reason to use W3TC any longer; APC seemed to have it all covered.

  • Pingback: Install Nginx with APC, Varnish, Wordpress and W3 Cache on a 128MB VPS | Axel Segebrecht's Blog

  • http://www.axelsegebrecht.com/ Axel Segebrecht

    Hi all, just to let you know that I have (finally) found (nay, taken) spare time to get that promised tutorial online! :)

    Had to split out some info into two more posts which will duly follow soon. Enjoy.

  • Pingback: Benchmark Nginx and Varnish for a Wordpress Site | Axel Segebrecht's Blog

  • Pingback: Make Money from Offering VPS Hosting | Axel Segebrecht's Blog