Recommended VPS Complete list of best VPS hosting providers.

Install Varnish 3 To Run With Apache 2 On Ubuntu Server

Complete guide how to install Varnish Cache 3 HTTP accelerator and configure it to run with Apache 2 on Ubuntu server. Using Apache itself as a web / HTTP server is considered not enough to run and host a production website in that server. Even for WordPress users, using caching plugin like W3 Total Cache, Hyper Cache, or WP Super Cache can sometimes not enough to reduce server load when the site getting its traffic peak. Most users simply ditch Apache and start learning and using Nginx because it is also free, faster, and lightweight. But since Nginx do not support .htaccess, many users think twice before using Nginx. So another alternative comes up, meet Varnish Cache, a free HTTP accelerator specially designed for dynamic website with heavy content and massive traffics beyond what a normal Apache web server is capable to serve.

What’s the difference between Nginx and Varnish 3? While Nginx is primarily an origin web server (like Apache), Varnish is simply an HTTP accelerator so it can also run with Nginx and Apache. You can read more about Varnish in Wikipedia.

Why use Varnish?

  1. To reduce server load, especially CPU works.
  2. To make a website load faster, because cache stored in RAM.
  3. To gain more visitors.

As I said, Varnish can be installed and run along with either Nginx or Apache but this time I will show you how to install and setup Varnish on Apache-based Ubuntu server. Remember that each time I say “server” it refers to both VPS or Dedicated server.

Prerequisite

Setup and Configure Varnish with Apache

Step 1 – Login to your server via SSH. You can use Terminal in Mac or Linux but as I’m using Windows, Putty is the perfect tool. You can download Putty and read how to use it. You have to login with root or sudo privilege. If you disabled root login (following my previous guide) then once logged in simply type “su” (without quotes), hit Enter then type the password of your root.

Step 2This is how to install Varnish 3. But first you have to add official Varnish repo url. Ubuntu by default comes with Varnish already but it might be outdated. Issue following command:

curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -

It will then look similar like this:

varnish repo

Now edit “source/list” file then add the repo url there. Use this command to edit the file:

nano /etc/apt/sources.list

then add this line there:

deb http://repo.varnish-cache.org/ubuntu/ lucid varnish-3.0

It looks like this:

varnish add repo

Once done hit Control+O to save then Control+X to exit Nano editor.

Now it is time to install Varnish, issue commands below:

apt-get update

apt get update

then,..

apt-get install varnish

apt-get install varnish

Wait few seconds for the installation process to finish with output something like this:

installed varnish

Step 3 – This is how to setup Varnish. So you’ve installed Varnish on your server and it runs already but it is not running properly, yet. You have to follow other few steps to configure Varnish so your server will be faster than before it your visitors will feel how fast it loads. What are you gonna do? All you have to do is making Varnish to run on port 80 while Apache on port 8080. First configuration file you need to edit is located at “/etc/default/varnish”. Use Nano to edit it:

nano /etc/default/varnish

Now look for these lines there under “Alternative 2”:

DAEMON_OPTS="-a :6081 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

edit varnish config

Found it? Now edit those lines to match following to make it works in port 80:

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

It looks like this:

edit varnish port 80

Once done hit Control+O to save then Control+X to exit.

Step 4 – As you will use Varnish Configuration Alternative 2 (with VCL), so you have to also the default.vcl file which tells varnish where to look for the webserver content (in this case is to fetch from Apache in port 8080). This step is also called as to set the backend server. Use Nano again to edit it:

nano /etc/varnish/default.vcl

Now you’ll see some lines like below. If not, edit it to match as following:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

The config above says that Apache as the backend server is available on localhost at port 8080 and Varnish will run in front of it listening on port 80 that previously used by Apache.

varnish setup file

Now you can exit Nano editor screen.

Step 5 – Now you’ve setup Varnish configuration to work on port 80. Next step, you have to also make Apache to work on port 8080. Without doing this step, Apache will still running on port 80 and so Varnish. Now edit Apache port setting located at “/etc/apache2/ports.conf”

nano /etc/apache2/ports.conf

Once Nano editor screen appears, find “NameVirtualHost” and “Listen” lines and replace 80 with 8080. Find:

NameVirtualHost 127.0.0.1:80
Listen 127.0.0.1:80

edit it to:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

screenshot:

varnish edit apache listen port

Once done hit Control+O to save then Control+X to exit.

Step 6 – Now edit the virtual hosts file for your site’s domain name (in case if you host more than one website in your server). I assume you knew already what is Virtual Hosts file and how to set it up. If not, read my previous guide about how to add new site in Apache server. Alternatively you can edit default Apache hosts file at first:

nano /etc/apache2/sites-available/default

then edit this line:

<VirtualHost *:80>

and make it like this (replace 80 with 8080):

<VirtualHost *:8080>

or you can also make it like this (alternatively):

<VirtualHost 127.0.0.1:8080>

Once done hit Control+O to save then Control+X to exit.

edit hosts file

Step 7 – That’s it. Final step, restart Apache and Varnish service:

service apache2 restart

then

service varnish restart

It will look similar like mine below:

varnish restart

Done.

Now you can test your server and it should work fine now. Using steps explained above your site will load faster and your server will be lighter in load. Also, by default as configured above in step 3, Vanish will use 256mb of your allocated RAM to store all cache files generated by the service. Please edit it to match your server specs. Edit that value to lower number If you are on a VPS with low amount of RAM. But you may also allocate x gb of your RAM if you are on Dedicated server.

Q: How to know / detect a server is running Varnish? Now once you access your website in browser it should be served through Varnish. You can use Firebug to test response header or simply use Centralops domain scanner service and check mark the “service scan” option. You’ll see message saying: Via: 1.1 varnish.

test server response

output (in the bottom):

test varnish output

Q: I want to see my website not via Varnish, how to access through Apache directly?

Simply add port “8080” at the end of your domain / ip address. Example:

http://servermom.wpengine.com:8080/

Q: How to know Varnish performance?

You can know how Varnish performance by seeing its real time stats. You can see its Hit rate ration, Hit rate average, number of client connections accepted, and so on. Issue this command syntax:

varnishstat

Example below taken from my playground server (not a production server running live website) so there is only me accessing the site.

varnish stat

That’s it. Enjoy your blazing speed server. Also catch my complete guide how to build and configure Ubuntu server from zero.

p.s: You can simply install Nginx or Apache + Varnish cache easily using Tuxlite. Read my previous guide about Tuxlite.

10 Comments

Add a Comment

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

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.