In this tutorial we will learn about,how to install and configure Varnish Cache server on CentOS/RHEL 6.x. We will install varnish 3.0 version in CentOS 6.x . I am writing this basic tutorial to setup your first varnish in server.Here we will use rpm packages for installation.The rpm packages are available as per your operating system architecture.
Here I am using noarch for example.
NOTE: Read new update – Install Varnish 4 on CentOS 7 / RHEL 7
Varnish is a famous web accelerator which helps to speed up your server by web caching technique.
I am setting the varnish in same server where web server is running
Download Link: http://repo.varnish-cache.org/redhat/
Note: Always select latest available stable package
My Operating System details:
Operating System:CentOS release 6.4 (Final)
arch : x86_64
IP Address: 192.168.1.22
Web Server: Apache version 2.2.15-29
To install and setup the varnish follow tthe given steps
Step 1: Download the rpm package with wget into your system (Login with root in your system)
wget http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release-3.0-1.el6.noarch.rpm
Step 2: Install the varnish by using yum command
yum install varnish
Step 3: Take the backup of original default.vcl file
cp -p /etc/varnish/default.vcl /etc/varnish/default.vcl.orig
Step 4: Bydefault varnish runs in port no. 6021
To check in which port no. it is running use netstat command. Before this start the varnish
/etc/init.d/varnish start
[root@varnish ~]# netstat -tanp|grep varnish tcp 0 0 0.0.0.0:6081 0.0.0.0:* LISTEN 1640/varnishd tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 1639/varnishd tcp 0 0 :::6081 :::* LISTEN 1640/varnishd [root@varnish ~]#
Step 5: Because bydefault webserver runs in port no. 80 we will change this port no. so that varnish can cache the web application.And we will run the Varnish in port no. 80.
Hence there is customization of port in both Varnish and Apache
Change the port no. of Varnish to port 80
Edit the file /etc/sysconfig/varnish and search for keyword VARNISH_LISTEN_PORT=6021. Here we will change the port no. 6021 to 80.Now the value of VARNISH_LISTEN_PORT must be 80.See given below
vi /etc/sysconfig/varnish VARNISH_LISTEN_PORT=80
Now check the default.vcl file and given below lines must be uncommented.Because we are doing practical in same server where apache is running hence we are not changing the IP address as 127.0.0.1.
Here in default.vcl file change the port no. 6021 to 8880
FAQ: Why port no. 8880 ?
Answer: Port no. 8880 is not specific one but in next step I am going to change the bydefault port no. of apache web server to 8880 hence I am using it. You can give other port no. but must be same port no. in apache config file also plus it must be above port no. 1024
vi /etc/varnish/default.vcl backend default { .host = "127.0.0.1"; .port = "8880"; }
Step 6: Change the port no. of apache to port 8880.
Note: Virtual hosting in apache is of two types Name based and IP based.You have to configure the web server as per your requirement. For eg. purpose I will use IP based virtual host and write few lines in VirtualHost tag.
If you are using Name based Virtual host then in httpd.conf file ,you have to change port no. in line having keyword VirtualNameHost
Take the backup of original httpd.conf file
cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.orig
Now change the Listening port no. 80 of apache to 8880. Find the line having keyword Listen
vi /etc/httpd/conf/httpd.conf Listen *:8880
Now configuring our first VirtualHost
In last line add the following given lines in /etc/httpd/conf/httpd.conf file
vi /etc/httpd/conf/httpd.confServerAdmin sharad@example.com DocumentRoot /var/www/html/ ErrorLog logs/sample-error.log CustomLog logs/sampleaccess_log common
For testing purpose I am creating a index.html file If you already have index.html file DO NOT FOLLOW this step.
echo "This is a test file for varnish" >> /var/www/html/index.html
Step 7: Now restart the Varnish and apache
/etc/init.d/varnish restart ;/etc/init.d/httpd restart;/etc/init.d/varnish restart
Now check the webpage by typing IP Address of your server in URL of web browser
http://192.168.1.22 (Replace the IP address with your web server IP Address)
From remote system run the below given command
curl -I http://ip-address-of-your-webserver
Below given reference is from my server: Hit the command 2-3 times. The Age value should not be constantly ZERO,it must be changeing always. If it is always showing zero, it means Varnish is not caching.(then again check the all configuration)
In below reference you can also see webpage is delivered by Varnish
Via: 1.1 varnish
linux@tuxworld:/tmp$ curl -I http://192.168.1.22 HTTP/1.1 200 OK Server: Apache/2.2.15 (CentOS) Last-Modified: Wed, 25 Sep 2013 15:59:46 GMT ETag: "24d18-20-4e73757a7d276" Content-Type: text/html; charset=UTF-8 Content-Length: 32 Accept-Ranges: bytes Date: Wed, 25 Sep 2013 16:05:24 GMT X-Varnish: 1980745874 1980745871 Age: 4 Via: 1.1 varnish Connection: keep-alive linux@tuxworld:/tmp$
Enable logging of Varnish
After doing all above configuration you may notice Varnish is not doing logging.
[root@varnish log]# ls -l /var/log/varnish/ total 0 [root@varnish log]#
To enable varnish logging start the service varnishlog and varnishncsa
starting varnishlog service
[root@varnish log]# /etc/init.d/varnishlog start Starting varnish logging daemon: [ OK ] [root@varnish log]# ls -l /var/log/varnish/ total 4 -rw-r--r-- 1 root root 168 Sep 25 21:40 varnish.log [root@varnish log]#
starting varnishncsa service
[root@varnish log]# /etc/init.d/varnishncsa start Starting varnish ncsa logging daemon: [ OK ] [root@varnish log]# ls -l /var/log/varnish/ total 4 -rw-r--r-- 1 root root 1008 Sep 25 21:41 varnish.log -rw-r--r-- 1 root root 0 Sep 25 21:41 varnishncsa.log [root@varnish log]#
Thanks A Lot. 🙂
curl -I http://domain.com
HTTP/1.1 200 OK
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.3
X-Pingback: http://domain.com/xmlrpc.php
X-Mod-Pagespeed: 1.8.31.6-4412
Vary: Accept-Encoding
Cache-Control: max-age=0, no-cache
Content-Type: text/html; charset=UTF-8
Date: Thu, 08 Jan 2015 21:19:13 GMT
X-Varnish: 178144301
Age: 0
Via: 1.1 varnish
Connection: keep-alive
I follow this tutorial and comeback to yours as mine it’s not working.
not sure where to configure this VARNISH_LISTEN_PORT=80
but i have found on my Centos7, it’s located within the file /etc/varnish/varnish.params
is that correct?
Hello Muhaimin,
The new Varnish 4.0 version will be updated today. This post is for varnish version 3.
Currently our blog is running with Varnish 4.0
It is true , for changing port no. of varnish you have to work on /etc/varnish/varnish.params
Regards
Sharad
Here are the settings for Varnish.
Below is reference from our server.
For remote system why we using this commond
curl -I http://ip-address-of-your-webserver , i’m trying to access this through browser through IP, it’s giving miss, but when run curl commond it gives hits.
You have given instruction on how to install.. May be you should include how to remove also..
Thankyou Ravi,
Will work on it.
Regards
Sharad
Hi,
Do you already have documentation on how to properly uninstall Varnish?
Thanks.
Hi Rene,
This is pretty much straight.
And kill the process of Varnish if still running.
Let me know if facing any challenges.
Regards
Sharad
Hey..Mr. How can i connect varnish server from my web browser. Please tell me Mr.
Varnish configuration and management generally done in server with command line.Still so far I have not used any good open source GUI for varnish. For Web GUI varnish has one reference for VAC. You can read about VAC from this URL https://www.varnish-cache.org/trac/wiki/WebGui