Home > HowTo, Linux > How to install varnish

How to install varnish

How to install varnish

This is a step-by-step on howto setup and run varnish web cache.
This was done for Linux RedHat 6 64bits, same works for CentOS 5.5 64bits with varnish 3.0

This install is made of Varnish and Apache.
Varnish will listen on port 0.0.0.0:80 and apache on localhost:8080

I have written other two howto’s on older version’s check them here:

 

1. Pre-Requisites,
Create user and folder,set permissions
# useradd varnish
# mkdir /opt/varnishadm
# chown varnish.root -R /opt/varnish/
Downlaod and install pre-requisites
# yum update
# yum -v -y install automake autoconf libtool gcc ncurses-devel libxslt groff pcre-devel pkgconfig httpd
-(Optional docutils, wget http://docutils.sourceforge.net/docutils-snapshot.tgz uncompress and run setup.py install)

2. Compile and Install
# ./configure –prefix=/opt/varnish –exec-prefix=/opt/varnish –bindir=/opt/varnish/bin –mandir=/usr/share/man –enable-stack-protector
# grep -i WARN config.log
(The only one I could not solve was configure: “WARNING: libedit not found, disabling libedit support”)
# make all
# make install

set Path
# export PATH=/opt/varnish/bin:/opt/varnish/sbin:$PATH

3. Set apache to listen on port 8080
# vim /etc/httpd/conf/httpd.conf
Change the line Listen 80 to:
Listen 127.0.0.1:8080
# service httpd restart

4. Fix the default.vcl
mv /opt/varnish/etc/varnish/default.vcl /opt/varnish/etc/default.vcl
# vim /opt/varnish/etc/default.vcl
uncomment the lines
backend default {
.host = “127.0.0.1”;
.port = “8080”;
}

5. Run the varnishd
# varnishd -f /opt/varnish/etc/default.vcl -s malloc,1G -T 127.0.0:1234 -a 0.0.0.0:80 -u varnish
Make sure its listening on correct port
# netstat -anp |grep varnishd

6.(optional) Set the firewall correctly

# iptables-save^C
# vim /etc/sysconfig/iptables
Add the line:
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
# iptables-restore < /etc/sysconfig/iptables
# iptables

7. Test if all is working
Keep an eye if varnish is answering the conection
# varnishlog |grep -i -v ng
Go into a web browser and type the IP of the varnish/apache server

 

8. Optional (setup daemon)
The follow is what I currently use for a production enviroment:

COPY and PASTE
# vim /etc/init.d/varnish

	#!/bin/bash
	#
	# chkconfig: 345 99 8
	# cache: Starts Varnish Cache Service
	# description: Varnish Reverse Cache for Web Sites
	# process name: cache
	# Source function library.
	. /etc/init.d/functions
	# Configurado por Felipe Ferreira @ 20/07/11

	#Edit here to the name fo your .vcl FILE
	SITEID=default
	CONFIG="/opt/varnish/etc/varnish.conf" # not used

	RETVAL=0

	OPTIONS="
	-f /opt/varnish/etc/$SITEID.vcl
	-a 0.0.0.0:80
	-l 512M
	-s malloc,15g
	-h classic,72227
	-w 500,3000,30
	-P /var/run/varnishd.pid
	-t 120
	-T :1234
	-p connect_timeout=2
	-p pipe_timeout=10
	-p listen_depth=128
	-p lru_interval=60
	-p sess_workspace=32768
	-p thread_pool_add_delay=1
	-u varnish
	-i $SITEID "



	getpid() {
	pid=`ps -ef | grep 'varnishd ' | grep -v grep |head -n 1 | awk '{print $2}'`
	}

	start() {
	     echo -n $"Starting Varnish Cache Daemon: "
	    getpid
	    if [ -z "$pid" ]; then
		 # in case of unclean shutdown
		       nohup /bin/su root -c "/opt/varnish/sbin/varnishd $OPTIONS"
		       RETVAL=$?
	    fi
	    if [ $RETVAL -eq 0 ]; then
		echo_success
	    else
		    echo_failure
	    fi
	    echo
	    return $RETVAL
	}

	stop() {
	    echo -n $"Stoping Varnish Cache ID: $SITEID: "
	    getpid
	    RETVAL=$?
	    if [ -n "$pid" ]; then
		kill $pid
	    sleep 6
	    getpid
	    if [ -z "$pid" ]; then
		rm -f /var/run/$SITEID
		echo_success
	    else
		echo_failure
	    fi
	    else
		echo_failure
	    fi
	    echo
	    return $RETVAL
	}

	# See how we were called.
	case "$1" in
	  start)
		start
		;;
	  stop)
		stop
		;;
	  status)
		getpid
		echo $pid
		if [ -n "$pid" ]; then
		        echo "Varnish Daemon $SITEID (pid $pid) is running..."
		else
		        RETVAL=1
		        echo "Varnish $SITEID is stopped"
		fi
		;;
	  stats)
		 /opt/varnish/bin/varnishstat -1 |more
		;;
	  restart)
		stop
		start
		;;
	  *)
		echo $"Usage: $0 {start|stop|status|stats|restart}"
		exit 1
		;;
	esac
	exit $RETVAL

# chmod +x /etc/init.d/varnish
# service varnish restart
# chkconfig –level 345 varnish on

Good
Ref. https://www.varnish-cache.org/docs/3.0/installation/install.html#compiling-varnish-from-source

VAMOVE *

*