Varnish is an HTTP accelerator, a special type of reverse proxy that caches static and dynamic content to ease the burden on web servers. It’s used by many heavily-trafficked websites, such as Facebook, Twitter and Wikipedia among others.
Varnish is well maintained, being updated every 2 to 3 years since 2008. The newest version, Varnish 4.0, was released in 2014. Best of all, it’s open source under a two-clause BSD license so that anyone can use it to improve the speed of their website.
The Varnish installation process is simple and will help protect your server and keep the quality of site performance high in case of a sudden spike in traffic. It’s time to install Varnish.
Getting Started
To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) with CentOS 7 installed.
• LAMP stack with Apache installed
• Type all commands in root
Install Varnish
Add the Extra Packages for Enterprise Linux, or EPEL, repository to your system. This is where Varnish lives.
yum -y install epel-release
Install the Varnish package. Ideally, ensure that your system is fully up-to-date beforehand.
yum -y install varnish
Upon installation, Varnish will be set to listen on port 6081 as its default. You’ll want to switch the default port that Varnish uses to an HTTP port. We’ll choose 8080 for the purposes of this tutorial.
There are three values in three files that you’ll need to change in order to do this.
First, open the Varnish configuration file in an editor.
nano /etc/varnish/varnish.params
Find the line VARNISH_LISTEN_PORT and change the value
VARNISH_LISTEN_PORT=6081
to
VARNISH_LISTEN_PORT=80
Next, open up the Varnish VCL configuration file.
nano /etc/varnish/default.vcl
Make sure the port is set to 8080 in this block.
backend default {
.host = "127.0.0.1";
.port = "8080";
Finally, open your Apache configuration file.
nano /etc/httpd/conf/httpd.conf
Look for the line Listen 80.
#Listen 12.34.56.78:80
Listen 80
Change it to read Listen 8080.
#Listen 12.34.56.78:80
Listen 8080
After you’ve made the above changes, restart both Apache and Varnish. This will make sure that they recognize the port change.
systemctl restart httpd.service
systemctl restart varnish.service
Check that your webserver and Varnish are working.
curl -I http://your_ip <---------------- Change for your main IP
HTTP/1.1 200 OK
Date: Tue, 21 Jun 2016 10:15:43 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: "1321-5058a1e728280"
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8
X-Varnish: 98321
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive
Check your Varnish stats.
varnishstat
Conclusion
Now that installation is concluded, Varnish is serving all site content to users. If your website is particularly heavy, you should see an immediate improvement in access speed. If this guide was helpful to you, kindly share it with others who may also be interested.