How to Install Varnish Cache 5.1 for Nginx on CentOS 7
Varnish Cache (also referred to as Varnish) is an open source, high-performance HTTP accelerator designed for speeding up web servers. In our last article, we’ve explained how to setup Varnish Cache for Apache web server on CentOS 7.
In this article, we will show you how to install and use Varnish Cache 5.1 as a front-end to Nginx HTTP server in CentOS 7. This guide should also work on RHEL7.
Step 1: Install Nginx Web Server on CentOS 7
1. Start by installing Nginx HTTP server from the default CentOS software repositories using the YUM package manager as follows.
# yum install nginx
2. When the installation completes, start Nginx service for now and enable it to start automatically at system boot.
# systemctl start nginx # systemctl enable nginx # systemctl status nginx
3. Now modify system firewall rules to permit inbound packets on port 80 using the commands below.
# firewall-cmd --zone=public --permanent --add-service=http # firewall-cmd --reload
Step 2: Install Varnish Cache on CentOS 7
4. Unfortunately, there are no pre-compiled RPM packages for latest version of Varnish Cache 5 (i.e 5.1.2 at the time of writing), so you need to build it from its source files as shown below.
First you need to enable the EPEL repository to install various dependency packages with the command below.
# yum install epel-release # yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx graphviz -y
5. Now download Varnish and compile it from source as follows.
# wget https://repo.varnish-cache.org/source/varnish-5.1.2.tar.gz # tar -zxvf varnish-5.1.2.tar.gz # cd varnish-5.1.2 # sh autogen.sh # sh configure # make # make install # ldconfig
6. Once you have compiled Varnish Cache from source, the main executable will be installed as /usr/local/sbin/varnishd.
Check to confirm that the Varnish installation was successful using the command below.
# /usr/local/sbin/varnishd -V
Step 3: Configure Nginx to Work With Varnish Cache
7. In this step, you need to configure Nginx to work with Varnish Cache. By default Nginx listens on port 80, you need change the default Nginx port to 8080 so it runs behind Varnish caching.
Open the Nginx configuration file /etc/nginx/nginx.conf and find the line listen 80 and change it to listen 8080 as in the server block as shown in the screen shot below.
# vi /etc/nginx/nginx.conf
Then save and close the file.
8. With all the necessary configurations in place, restart Nginx services as follows.
# systemctl restart nginx
9. Now start Varnish daemon manually by typing following command instead of calling systemctl start varnish, because certain configuration are not in place when its installed from source.
# /usr/local/sbin/varnishd -a :80 -b localhost:8080
Step 4: Test Varnish Cache on Nginx
10. Finally, test if Varnish cache is enabled and working with the Nginx service using the cURL command below to view the HTTP header.
# curl -I http://localhost
You can find additional information from the Varnish Cache Github Repository: https://github.com/varnishcache/varnish-cache
In this tutorial, we explained how to setup Varnish Cache 5.1 for Nginx HTTP server on CentOS 7. Use the comment form below to send us any queries or extra ideas.