Recommended VPS Complete list of best VPS hosting providers.

Install Varnish 3 To Run With Apache 2 On CentOS Server

How To Install and Configure Varnish Cache on CentOS with Apache Web Server. – Varnish is a well-known free software licensed under a two-clause BSD licence aimed to boost server performance. Shortly defining, it is a web accelerator can be installed in front of any server that speaks HTTP (e.g: Apache and Nginx). Many users have installed Varnish on their live / production servers to handle millions users with ease. Why Apache is not enough? I will not tell you some technical answers but shortly speaking, many users ditch Apache because it performs really bad when the server is getting traffic peak. There are also many users ditched Apache and start using either Nginx, LiteSpeed or any other HTTP server like Lighttpd. But however there is a downside: Nginx is free but doesn’t support .htaccess file, LiteSpeed do supports .htaccess but it is not free while many users are not familiar enough with Lighttpd.

WordPress users may still use Apache and try to minimize server load by installing PHP-based caching plugin like W3 Total Cache, WP Super Cache, Hyper Cache, and so on. However the performance is not as sleek and fast as Nginx and LiteSpeed. So then another alternative comes up allowing users to still using Apache plus all PHP-based caching plugin they like but with increasing performance boosted up by 300 -1000x. That alternative is Varnish Cache.

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.

Prerequisite

  1. A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
  2. Windows users should download Putty while Mac and Linux users can simply use Terminal
  3. You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
  4. Your server should already has necessary software to host a website. In this case are: Apache, PHP5, and MySQL, (optional: Postfix and Cyrus).
  5. Read and follow my previous guide about how to add and setup new site in Apache on CentOS server.
  6. About 15 minutes of your time and a cup of coffee if you like.

How to install Varnish on CentOS

Step 1 – Login to your server and follow my previous guide about Basic setup for CentOS before you build a live web server. You may and may not follow that tutorial but if you followed, it will give you some basic security tweak to your server.

Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch login as root.

switch-root-login

You may also need to type this command to go to the root directory:

cd ~

Step 2 – The latest version of Varnish till this article posted is Varnish Cache v3.0.3 but installation process remains similar. Before you proceed, make sure you issue following command first to add repository:

rpm -Uvh http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.noarch.rpm

It should finish in seconds:

add repository

p.s: In case if you found 404 when adding rpm, please check here for latest url location.

Step 3 – So here it is the main command to install Varnish on CentOS:

yum install varnish

Here’s the screenshot:

yum install varnish

Hit Y if it asks you to confirm. Once done, it looks like following:

install complete

From example above, I’ve installed Varnish Cache version 3.0.2.

How to Configure Varnish to work with Apache

Step 1 – So you installed Varnish. It is the time for you to setup some basic configuration to make Varnish work with Apache behind it. In CentOS and Fedora, Varnish configuration file located in “/etc/sysconfig/varnish”. You can use either vi or Nano editor to edit that file:

nano /etc/sysconfig/varnish

The first part of the config file looks like this:

varnish config

Step 2 – Now scroll down the page and you’ll notice there are 4 alternative configurations available and the currently active one is “Alternative 3”. Now add # symbol to all lines under Alternative 4 sections. It should look like this:

comment alternative 3

Step 3 – By adding the # symbol above means you are going to disable alternative 3. Now remove all # in Alternative 2 config.

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

Change “-a : 6081 \” to “-a : 80 \” as shown above. Shortly, make sure your config looks like mine above. You can also change “256m” to higher numbers depending on how much RAM allocated to your server. As in this case my server has only 512MB then I want to allocate half of it for the caching purpose.

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

p.s: Different release version of Varnish may also have different default setup enabled. In version 3.0.2 it uses “Alternative 3” enabled by default. In my previous tutorial I was using version 3.0.3 on Ubuntu and it uses “Alternative 2” enabled by default. The point is, you may also use Alternative 1, 2, 3 or 4 but my personal opinion says that Alternative 2 is the easiest one.

note: Varnish allows you to use either Disk or Memory to store the cache files. However memory (RAM) is considered faster than Disk because it has no spinning part. To save your memory, you can alternatively use SSD-based Disk but however using RAM is still faster.

Step 4 – Configure Varnish default VCL file located in /etc/varnish/. That VCL file holds configuration to tell varnish where to look for the webserver content (in this case is to fetch from Apache in port 8080). Use Nano editor to edit it:

nano /etc/varnish/default.vcl

In that file you can set the backend server, I mean the server that Varnish will look for the content.

Change this default value:

default vcl

change the port to 8080.

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 was previously used by Apache.

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

How to configure Apache to work with Varnish

Step 1 – Open Apache default configuration file:

nano /etc/httpd/conf/httpd.conf

Step 2 – Then look for the line that says “Listen 80” and change it to 127.0.0.1:8080:

listen port 80

so it will look like this:

listen port 8080

Step 3 – Also look for this line:

name virtual host

Change it to:

NameVirtualHost 127.0.0.1:8080

Step 4 – Now scroll down the page to the bottom to find the VirtualHost entry for your website. Change its port to 8080:

virtual host file 8080

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

Step 5 – Finally restart Apache and Varnish service.

Command to restart Apache:

 

service httpd restart

and this command can restart Varnish:

service varnish restart

screenshot:

restart service

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.

varnish cache

example output:

via varnish cache

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.

Most common commands you can use to monitor varnish performance:

# varnishstat : Provides all the info you need to spot cache misses and errors.
# varnishhist : Provides a histogram view of cache hits/misses.
# varnishlog : Provides detailed information on requests.
# varnishtop : It reads varnishd shared memory logs and presents a most commonly occurring log entries.
# varnishadm : Command-line varnish administration used to reload vcl and purge urls.

Optional: You can also execute following command so Varnish Cache can automatically running every time your server boots or reboots.

chkconfig --level 345 varnish on

That’s all. Enjoy using varnish.

57 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.