Install Varnish On the Raspberry Pi


Installing Varnish On Raspberry Pi

Varnish is an http accelerator (reverse proxy) that can be used to store copies of web pages that will be used to serve the web surfer instead of copying the page from the server. Essentially, websites that are delivered from the cache will display much faster than those that are not. Although Varnish can accelerate the page delivery, the ISP (Internet Service Provider) upload speed, page content, and browser caching will have very high impacts on the page load speed too.

Since Varnish can store the pages in memory, a small website that is optimized for performance can be quite efffective; especially if its size is well under the cache size that you set.

Although Varnish and a Raspberry Pi can be used to host a website under aquequate conditions, it can also be an excellent starting point to experiment with Varnish and its features before making major changes to a live server with many Virtualhosts and addon domains.

Here are the steps to setting up Varnish on the Raspberry Pi.

To install Varnish on the Raspberry Pi,

1) Become a sudoer to execute root commands.

 pi@raspberrypi:# sudo -s 

2) Open the /var/tmp folder

 root@raspberrypi:# cd /var/tmp 

3) View the files in the repository at http://repo.varnish-cache.org/source/.

4) Download a file like varnish-3.0.5.tar.gz.

5) Open the /var/tmp folder

 root@raspberrypi:# wget http://repo.varnish-cache.org/source/varnish-3.0.5.tar.gz 

6) Extract the file and change directory. When you compile and install from source you work from within the top folder. In this case, the folder is varnish-3.0.5.

 root@raspberrypi:# tar xzf varnish-3.0.5.tar.gz root@raspberrypi:# cd varnish-3.0.5 

7) Install dependencies

 root@raspberrypi:# apt-get install autotools-dev autoconf libpcre3-dev libedit-dev automake libtool groff-base python-docutils pkg-config 

8) Run autogen file and continue compiling and installing from source.

 root@raspberrypi:# sh autogen.sh 

9) Configure

 root@raspberrypi:# sh configure --enable-diagnostics --enable-debugging-symbols 

10) Make

 root@raspberrypi:# make 

11) Make install

 root@raspberrypi:# make install 

Note: If anything went wrong and you are doing a second attempt, run make uninstall and make clean. Make uninstall will remove the previously installed package.

12) Setup the necessary links

 root@raspberrypi:# ldconfig -n /usr/local/lib/ 

13) Edit the configuration file default.vcl.

 root@raspberrypi:#  vi /usr/local/etc/varnish/default.vcl 

or

 root@raspberrypi:#  nano /usr/local/etc/varnish/default.vcl 

Note: You will need to uncomment some lines such as;

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

Note: The port 8080 will be used by the web server.

14) Change the port from the web server. Normally, Apache or Lighttpd will use port 80. Change this to something else like port 8080.

 root@raspberrypi:# vi /etc/lighttpd/lighttpd.conf 

15) You can make a file to start the service or you can type or copy and paste the following command to start the Varnish service.

 root@raspberrypi:# /usr/local/sbin/varnishd -f /usr/local/etc/varnish/default.vcl -a :80 -P /var/run/varnish.pid -s malloc,35m 

Note:
The -f option tells Varnish where the configuration file is located. In the case, the file is located at /usr/local/etc/varnish/default.vcl
The -a option is the port that Vanish will use. In this case, Vanish will use port 80. Your router will allow port forwarding on the port.
The -P option is the PID file location which records the process id for Varnish. When you know the process id, it makes it easier to kill the service, etc.
The -s option tells where the cache is located and the size of the cache. In this case, the cache is 35m.
Find more about options at https://www.varnish-cache.org/trac/wiki/Performance.

Check if Varnish is Running

 root@raspberrypi:# /usr/bin/pgrep -lf varnish 
 

Check if Varnish is Working Properly

PHP Users can make a file called view_headers.php and use the get_headers() function to output the headers which hopefully includes some information about Varnish. The samples below show how to get headers from a URL.

 print_r(get_headers("http://localhost", 1)); print_r(get_headers("https://example.org", 1)); print_r(get_headers("myIPaddress", 1)); 

You can run the file in a browser or via the command line. Running the PHP file at the command line is shown below.

 root@raspberrypi:# php ./view_headers.php 

If all goes well, the array will contain the line below. [Via] => 1.1 varnish PERL users can install the libwww-perl package to view headers.

 root@raspberrypi:# apt-get install libwww-perl root@raspberrypi:# GET -Used http://localhost:80/ 

If all is working fine, you should something similar to the output below.

GET http://localhost:80/
User-Agent: lwp-request/6.03 libwww-perl/6.04

200 OK
Connection: close
Date: Sat, 11 Jan 2014 18:23:19 GMT
Via: 1.1 varnish
Age: 0
ETag: "3260290251"
Server: lighttpd/1.4.31
Vary: Accept-Encoding
Content-Type: text/html
Last-Modified: Sat, 14 Sep 2013 23:19:36 GMT
Client-Date: Sat, 11 Jan 2014 18:23:19 GMT
Client-Peer: 127.0.0.1:80
Client-Response-Num: 1
Client-Transfer-Encoding: chunked
Title: Welcome page
X-Varnish: 361868210