In today tutorial we show How to install and configure Varnish cache on CentOS 7.
Varnish cache is a high-performance and open source cache server that increase speed of your web server performance by storing web data in a cahce.
Here we configure it as a reverse proxy cache server for Apache.
Simply when client send request for a content, Varnish cache server send this request to origin server and then cache returned objects from origin server and serve it to client.
Next time client requests for same content, Varnish serve it from its cache not origin server.
Here is our environment:
OS: CentOS 7 linux on VMware
Varnish IP address: 192.168.147.128
Client IP address: 192.168.147.129
1- Install Apache
refer to this article to know about How to install AMP on CentOS 7 linux.
2- Configure Apache
by default Apache is listening on port 80. because here we want Varnish be in front of Apache, we should change default Apache port.
so we open Apache config file and change default port:
# vim /etc/httpd/conf/httpd.conf
find “Listen 80” and change it to “Listen 8080”.
then restart Apache service:
# systemctl restart httpd
3- Install Varnish cache
3.1 Install prerequisites
because varnish version on EPEL 7 repository is too old, we compile and install Varnish from source code.
Varnish 6 needs python greater than 3.4. here we install python 3.6. run the following commands one by one:
# yum groupinstall "Development Tools" # wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz # tar -xJf Python-3.6.4.tar.xz # cd Python-3.6.4 # ./configure # make # make install
then we install required packages to compile Varnish:
yum install make autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx graphviz git
then update curl and nss packages:
# yum update curl nss
Now clone latest Varnish code from git repository:
git clone https://github.com/varnishcache/varnish-cache
3.2 Compile and install Varnish
then run these commands one by one:
# cd varnish-cache # sh autogen.sh # sh configure # make # make check # make install
after varnish cache being installed, we can check its version:
# varnishd -V
default Varnish config file is named default.vcl. by running the following commands we create it:
# mkdir /etc/varnish/ # cp /usr/local/share/doc/varnish/example.vcl /etc/varnish/default.vcl
3.3 Make service file
to start Varnish cache automatically on system start, we must create a service file. so create a service file:
# vim /lib/systemd/system/varnish.service
then paste the following contents in it:
[Unit] Description=Varnish Cache Server After=network.target [Service] Type=forking ExecStart=/usr/local/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m Restart=on-failure [Install] WantedBy=multi-user.target
then we enable and start Varnish cache service:
# systemctl enable varnish # systemctl start varnish
3.4 Configure SElinux
if SElinux prevented executing Varnish service file, we must run these commands to allow access to /etc/varnish/default.vcl file to varnishd:
# grep varnishd_t /var/log/audit/audit.log | audit2allow -M myvarnish # semodule -i myvarnish.pp
then restart Varnish service.
4- Configure Firewall
Here we must open port 80. so issue these commands:
# firewall-cmd --add-service=http --permanent # firewall-cmd --reload
5- Test Varnish cache
for testing Varnish functionality, we open our website IP address in browser and check response headers.
also we can test it with curl:
# curl -I http://192.168.147.128
6- Useful Varnish commands
we can administer Varnish instance with varnishadm.
with varnishlog, we can have access to request-specific data, for example information about specific clients and requests.
varnishstat is used to access overall statistics such as the number of total requests, number of objects, and more.
varnishtop reads Varnish logs and shows continuously updated list of the most commonly occurring log entries.
Also for complete lists of commands and documentation you can refer to Varnish documentation.