Varnish cache 4.x installation and configuration in CentOS/RHEL

What is varnish cache ?

Varnish (HTTP accelerator) is an open source reverse HTTP proxy that will work on front of apache & nginx to speeds up your website/webserver performance. Especially this was designed for content-heavy dynamic web sites such as facebook, twitter, etc.. I can dam sure varnish cache will improve your webserver performance 500% or 1000% compare with earlier stage. How it will work ? Varnish speeds up your website and reduce the web server load by storing a copy of the web page served by the web server. If any new request coming, varnish will check the cached pages, if it’s in cache then it will serve the web page directly without sending request to web server.

What is new in varnish cache 4.x & advantage of varnish cache

  • Improved security features (jails)
  • Support for PROXY protocol
  • Warm and cold VCL states.
  • Backends defined through VMODs.
  • default.vcl file is not configured, so go through the Varnish cache 4.x documentation for further details
  • Improve web server performance by serving the request from cache
  • support load balancing (round robin and a random director)
  • Plugin support available
  • Gzip Compression and Decompression

1) Install dependencies for varnish cache

Install dependencies for varnish cache to avoid dependencies issue.

# Install dependencies for varnish cache #
[[email protected]]#  yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx graphviz

For more details @ varnish dependencies

2) Checking available version of varnish cache

Checking available version of varnish cache because i’m going to install latest version of varnish (4.x).

# varnish cache available version checking (It shows older version) #
[[email protected]]#  yum list varnish
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base:
 * epel:
 * extras:
 * updates:
Available Packages
varnish.x86_64            2.1.5-5.el6        epel

# Adding latest version of varnish to repo #
[[email protected]]#  rpm -i

# Again checking varnish cache version #
[[email protected]]#  yum list varnish
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base:
 * epel:
 * extras:
 * updates:
varnish-4.1                    | 951 B      00:00
varnish-4.1/primary            | 3.5 kB     00:00
varnish4.1                     8/8
Available Packages
varnish.x86_64                 4.1.0-1.el6   varnish-4.1

3) Installing varnish cache

Now, i got latest version of varnish cache and going to install it.

[[email protected]]#  yum install varnish

4) Configure varnish cache

In earlier version of varnish, we need to make alot of changes but in 4.x not like that. You can make it work by doing small modification.

# Start varnish cache service #
[[email protected]]#  /etc/init.d/varnish start
[[email protected]ek]#  service varnish start

# Checking varnish cache default listening port #
[[email protected]]#  netstat -tanp|grep varnish

# Change varnish default listening port to 80 
(By default varnish runs in port no. 6021 and we need to change the port no. 80.
So, that varnish will cache web application and run in port no. 80) #
[[email protected]]#  nano /etc/sysconfig/varnish

# Checking beckend default configuration from default.vcl 
file (This was build with properly. So, change the apache listening port to 8080 
because varnish already listening port no. 80 and apache will work on backend and 
varnish will work on front of apache) #
[[email protected]]#  nano /etc/varnish/default.vcl
backend default {
    .host = "";
    .port = "8080";

# Modifying apache default listening port to 8080 
which was build with dafualt.vcl file #
[[email protected]]#  nano /etc/httpd/conf/httpd.conf
Listen *:8080

# Change all VirtualHost file also #
[[email protected]]#  nano /etc/httpd/conf/httpd.conf

	ServerAdmin [email protected]
	DocumentRoot /var/www/html/
	ErrorLog logs/magesh_co_in_error.log
	CustomLog logs/magesh_co_in_access.log common

# Restart varnish cache service #
[[email protected]]#  /etc/init.d/varnish restart
[[email protected]]#  service varnish restart

# Restart apache service #
[[email protected]]#  /etc/init.d/httpd restart
[[email protected]]#  service httpd restart

5) Testing Varnish cache

I’m going to check on my server whether varnish cache is working fine or not.

[[email protected]]#  curl -I http://localhost
HTTP/1.1 200 OK
Date: Fri, 02 Oct 2015 15:28:30 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Vary: Accept-Encoding
Cache-Control: max-age=0, no-cache
Content-Length: 49
Content-Type: text/html; charset=UTF-8
X-Varnish: 6
Age: 0
Via: 1.1 varnish-v4
Accept-Ranges: bytes
Connection: keep-alive

6) Enable Varnish cache logs

By default varnish wont write logs and we need to start the varnish log daemon.

# Start varnishlog service #
[[email protected]]#  /etc/init.d/varnishlog start

# Start varnishncsa service #
[[email protected]]#  /etc/init.d/varnishncsa start

WOW…! Varnish cache working amazing, now i’m getting 500x to 1000x speed compare with earlier stage.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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