Linux palvelimena – Homework 7 – Server optimization

Week 7 – Server optimization

The seventh and final week had us install and test Varnish, a reverse proxy.

Course homepage: http://terokarvinen.com/2012/aikataulu-%E2%80%93-linux-palvelimena-ict4tn003-3-ja-ict4tn003-5-kevaalla-2012

Assignment

Install Varnish
Try Firebug for Firefox

Environment

I’m using Ubuntu 12.04 @ Virtualbox. Username is ‘antero’. Virtualbox has access to 2 CPU cores (of my Core 2 Quad @ 3.4GHz) and 2048MB of RAM.

Sources

Everything done in this post is based on our previous class (see course homepage above).

Varnish

Varnish is a very useful reverse proxy, which can greatly improve web server performance. We make Varnish listen to TCP port 80, so that it will serve the requested websites instead of Apache. (Varnish does get them from Apache but greately reduces load.) Pages are essentially served as static websites thanks to this, which makes everything *very* fast.

I’m picking things up from last week, when I installed WordPress. https://a1100320.wordpress.com/2012/10/01/linux-palvelimena-homework-6-wordpress/

Prerequisite steps

First I’m going to change Apache’s listening port to 8080.

sudo nano /etc/apache2/ports.conf

If you’ve configured any virtual hosts, be sure to edit their files in /etc/apache2/sites-available like so: <VirtualHost *:8080>

I restart apache2 and try to access my WordPress.

Oh no, it doesn’t appear to work anymore. Luckily installing Varnish will fix that.

Installing Varnish

The metapackage ‘varnish’ installs libvarnishapi1 and varnish, which are exactly the things I need.

sudo apt-get install varnish

I need to edit /etc/default/varnish. One of the configurations therein is uncommented by default, which is what I’ll use. DAEMON_OPTS has numerous flags and values. The only one which needs to be changed is -a, since we don’t want varnish to listen to port 6081 but port 80.

sudo nano /etc/default/varnish

See above for the changes. After that, I need to restart varnish.

sudo service varnish restart

Varnish is now set up and WordPress works once more. We’ll get to actual performance tests later, but first…

Firebug

Time to install Firebug, the Firefox addon. I open Firefox and go to Tools -> Add-ons. (Or just press Shift-Ctrl-A). There’s a search field in the upper right corner:

Typing ‘firebug’ finds the addon I’m after.

Once the addon is installed, I go to my WordPress’ “Hello world” post while having Firebug open, and take a look at how fast various parts of the page load.

HTML generally seems to load quite fast:

Images take a bit longer: (Also, what’s 0.gravatar.com?)

AB

Apache Bench is a tool for testing web server performance. It can create huge amounts of traffic, so be sure to try it out on your localhost only, unless you want to appear like you’re attempting to ddos attack a website.

I’ll create a small HTML page for testing purposes, called fast.html, whose contents are:

<!doctype html>
<html>
<head>
<title>Lightning-fast static webpage</title>
<meta charset="utf-8" />
</head>
<body>
This page ought to be very lightweight!
</body>
</html>

I also have index.php, which has phpinfo(); on it. So essentially I’ll run the same ab command for the sites at port 80, which means Varnish, and 8080 which means plain Apache.

PHP with Varnish

ab -n 1000 -c 10 http://localhost/~antero/index.php

No failed requests. ~1590 requests per second, longest request 46ms.

Static page with Varnish

ab -n 1000 -c 10 http://localhost/~antero/fast.html

No failed requests, ~3500 requests per second, longest request 15ms.

Php without Varnish

ab -n 1000 -c 10 http://localhost:8080/~antero/index.php

No failed requests, ~700 requests per second, longest request ~300ms.

Static page without Varnish

ab -n 1000 -c 10 http://localhost:8080/~antero/fast.html

No failed requests, ~1790 requests per second, longest request 42ms.

Analysis

Here’s the data for easy comparison, with rounded numbers:

index.php Varnish No failed requests. 1600 requests per second, longest request 50ms
index.php Apache  No failed requests, 700  requests per second, longest request 300ms

fast.html Varnish No failed requests, 3500 requests per second, longest request 15ms
fast.html Apache  No failed requests, 1800 requests per second, longest request 40ms

Varnish was consistently faster, but moreso when it comes to PHP. Static pages are rather light to begin with, but still provides a considerable boost in performance.

Best for last: AB meets Varnish meets a WordPress post

I thought it’d be interesting to see a proper, conclusive result, so I tried benchmarking my WordPress’ performance with and without Varnish. I modify /etc/default/varnish and change its listening port, and temporarily switch off Varnish off.

(Changing the listening port might not be necessary, now that I think of it, seeing as it won’t be listening to anything anyway.)

sudo service varnish stop

And modify /etc/apache2/ports.conf to its original state, so that WordPress works normally with port 80, and restart Apache:

sudo service apache2 restart

To clarify: If one doesn’t stop Varnish first, port 80 won’t be available for apache2 to listen.

WordPress without Varnish

ab -n 1000 -c 10 http://localhost/~antero/wordpress/2012/10/hello-world/

Still no failed requests, but the test tooks ages. Requests per second 10, longest request 1998ms. Note: time taken for tests 98 seconds!

WordPress with Varnish

I re-enable Varnish and run the test again.

ab -n 1000 -c 10 http://localhost/~antero/wordpress/2012/10/hello-world/

No failed requests. 1333 requests per second. Longest request 51ms. Time taken for tests 0.75 seconds.

No Varnish
Requests per second: 10    Longest request (ms): 1998  Time taken for tests: 98 seconds

Varnish
Requests per second: 1333  Longest request (ms): 51    Time taken for tests:  0.75 seconds

In a nutshell: Varnish results in roughly 100X performance when it comes to heavier sites like WordPress, which use SQL and PHP.

Advertisements

About a1100320

IT student, musician, gamer. Beep boop.
This entry was posted in Linux palvelimena ICT4TN003-4. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s