Caching with Varnish
By Fahad Ahammed
- 2 minutes read - 356 wordsVarnish is an HTTP accelerator designed for content-heavy dynamic web sites. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator. Varnish is focused exclusively on HTTP, unlike other proxy servers that often support FTP, SMTP and other network protocols.
Here , I will explain how to get varnish in front of apache2 or accelerate apache2 with varnish .
First do this :
sudo -s;curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
sudo -s;echo “deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0” | sudo tee -a /etc/apt/sources.list
sudo apt-get update
sudo apt-get install varnish
Each line seperately run from terminal . or just (not recommended) :
sudo apt-get install varnish
Now do this :
nano /etc/default/varnish
And search for :
DAEMON OPTS=”-a *:6081
And edit that as :
DAEMON OPTS=”-a *:80
Also edit default.vcl to something else, I took “mysite.vcl” .
To save use ‘ctrl+x’ and then ‘y’ and ‘enter’ .
Now do this :
nano /etc/varnish/mysite.vcl
and put below texts there .
You have to edit above file which is red marked . Above YOUR IP is just your ip to not to allow any other to access a particular folder like wordpress /wp-admin/ . And that red marked “fahad” , you can change that to anything but both should be same . If you have no static ip than you should use Below texts :
Save this .
Now do this :
nano /etc/apache2/ports.conf
Change:
To:
Apache will listen on that port .
You will have to edit your vhosts as well . Do This :
nano /etc/apache2/sites-enabled/000-default
Change:
To:
You should know i am using here a command line text editor named nano which have a key binding of “ctrl+x” to save a file . Now do this :
Now test varnish , open terminal again and do this :
If you see anything written word “Varnish” then your procedure is ok and your varnish cache tool is caching 3 folders elements from your websites css,js and img folder . If you get error please tell me .