Installing and configuring Varnish
As they say on their web page http://www.varnish-cache.org/:
Varnish is a state-of-the-art, high-performance HTTP accelerator
This small guide is about installing and configuring Varnish cache, if you do not know what Varnish is used for here is the short description taken from wikipedia.
Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. In contrast to other HTTP accelerators, many of which began life as client-side proxies or origin servers, Varnish was designed from the ground up as an HTTP accelerator.
First of all you will need a Linux distribution installed, I am assuming that you already have this ready before you start – I have used Ubuntu myself for this example. In this small guide I am covering everything that I needed to install and configure in order to get Varnish up and running, from a blank slate of a Ubuntu distribution.
Installation and configuration of JAVA and Tomcat
-
The first thing I needed to do was to install java, this was done by running the following command using the terminal:
apt-get install sun-java6-jdk -
After having installed java, the next step is to download and install Tomcat, to download Tomcat I ran the following command from the terminal:
wget http://apache.idnr.ws/tomcat/tomcat-6/v6.0.24/bin/apache-tomcat-6.0.24.tar.gzAfter you have downloaded Tomcat, you need to extract it which can be done with the following command:
tar xvzf apache-tomcat-6.0.24.tar.gzFinally you can move the folder to a directory of your choosing, I chose to place Tomcat in the /usr/local/tomcat folder – the move was done with the following command:
sudo mv apache-tomcat-6.0.24 /usr/local/tomcat -
Now you need to add the JAVA_HOME environment variable to the .bashrc file, I did this by the use of the text editor nano by running the following command:
nano ~/.bashrcThen you need to add the JAVA_HOME environment variable export to the end of the file, the line you add should look like, or similar depending on the java version
:
export JAVA_HOME=/usr/lib/jvm/java-6-sun -
You can now start Tomcat using the following command from the tomcat/bin folder:
./startup.shAnd you can stop Tomcat again using the following command:
./shutodwn.sh
Adding automatic startup to Tomcat:
To make it so that Tomcat starts up automatically with Ubuntu you need to edit the init.d for Tomcat. This is done by running the following command:
sudo nano /etc/init.d/tomcat
In the file you paste the following code:
# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pidexport JAVA_HOME=/usr/lib/jvm/java-6-suncase $1 in
start)
sh /usr/local/tomcat/bin/startup.sh
;;
stop)
sh /usr/local/tomcat/bin/shutdown.sh
;;
restart)
sh /usr/local/tomcat/bin/shutdown.sh
sh /usr/local/tomcat/bin/startup.sh
;;
esac
exit 0
Now the script needs to be made executable by running the following chmod command:
sudo chmod 755 /etc/init.d/tomcat
Finally you need to create a couple of symbolic links, which is done with the following two commands:
sudo ln -s /etc/init.d/rc1.d/K99tomcatsudo ln -s /etc/init.d/rc2.d/K99tomcat
Installation of Varnish
Varnish have their own “Getting started guide” that describes the installation process, so many of the things I cover here will be mostly repetition from this guide.
Firstly you will need to download Varnish, I did this from their installation site. When you have downloaded the installation file you will need to unpack it to a directory of your choosing before you can begin the installation process.
Once you have unpacked Varnish, go to the folder and run the autogen.sh script – this will generate the config file. You run the autogen.sh script from the terminal using the following command:
./autogen.sh
Once this is done you need to execute the generated config script, to do so you need to run the following command:
./configure --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking
Finally after this has been done you install Varnish with the following command:
make install
Avoid the ‘libvarnish.so.1: cannot open shared object file: No such file or directory’ error:
After having installed Varnish I kept getting errors about libvarnish.so.1, saying no such file or directory. After some troubleshooting this issue the solution presented itself (with some inspiration from this thread), for some reason the “/usr/local/lib” path is not by default included in the “/etc/ld.so.conf” (this might just be an Ubuntu issue).
However the solution itself is simple, just add /usr/local/lib to the /etc/ld.so.conf and everything is fixed – this can be done as shown below:
sudo ldconfig /usr/local/lib/
sudo sh -c "echo /usr/local/lib > /etc/ld.so.conf.d/locallib.conf"
sudo ldconfig
Starting and stopping Varnish
Installation and configuration is now done, at least the initial part required to start Varnish. Now comes all the real hairy stuff with URL rewriting and VCL programming. As for starting Varnish there is a short introduction here, and if you are not much for reading through it you can just start it up by writing the following command:
varnishd -a :80 -b localhost:8080 -T localhost:6082
The above command means that Varnish is listening on port 80, forwards traffic to port 8080 and that the administration interface can be reached on port 6082.
Stopping Varnish can be done by killing the process, but to kill the process you first need to find the process id (pid) – this can be done with the following command:
ps aux | grep varnish
Now that you have the pid you can stop (or “kill”) Varnish by using the kill command:
kill [pid]
|
|







