Install Varnish Cache 5.1 to Boost Apache Performance on CentOS 7
Varnish Cache (commonly known as Varnish), is an open source, popular reverse-proxy HTTP accelerator intended for speeding up web servers. It is engineered for excessively utilized API endpoints and also for dynamic sites that serve massive-content and experience high-traffic.
It basically helps to scale down CPU load; supports load balancing on web servers and enables a web browser to quickly load sites as a result of storing the cache in RAM. A number of big name companies use it including Facebook, Twitter and Wikipedia just to mention but a few.
In this article I will explain how to install and use Varnish Cache 5.1 as a front-end to an Apache web server in CentOS 7 (also works on RHEL7).
Step 1: Install Apache Web Server on CentOS 7
1. First install Apache HTTP server from the default CentOS software repositories using the YUM package manager as follows.
# yum install httpd
2. Once Apache installed, start it for the time being and enable it to start automatically at system boot.
# systemctl start httpd # systemctl enable httpd # systemctl status httpd
3. Next update 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), therefore you need to build it from its source packages.
Before compiling from source, first you need to enable the EPEL repository to install several dependency packages as shown.
# yum install -y epel-release # yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx graphviz -y
5. Next download Varnish and compile it from source as shown.
# 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. After compiling Varnish Cache from source, the main executable will be installed as /usr/local/sbin/varnishd.
You can confirm that the Varnish installation was successful using the following command.
# /usr/local/sbin/varnishd -V
Step 3: Configure Apache to Work With Varnish Cache
7. Now configure Apache to work in conjunction with Varnish Cache. By default Apache listens on port 80, you need change the default HTTPD port to 8080 – this will ensure that HTTPD runs behind Varnish caching.
You can use the sed command to change port 80 to 8080 as shown.
# sed -i "s/Listen 80/Listen 8080/" /etc/httpd/conf/httpd.conf
Then save and close the file.
8. After performing all the necessary configurations, restart HTTPD and Varnish cache to effect the above changes.
# systemctl restart httpd
9. Now start varnish daemon manually by typing following command instead of calling service varnish start, because some configuration are not in place for source installation.
# /usr/local/sbin/varnishd -a :80 -b localhost:8080
Step 4: Test Varnish Cache on Apache
10. Lastly, test if Varnish is enabled and working working with the HTTPD service using the cURL command below can be used to view the HTTP header.
# curl -I http://localhost
For more information, check out Varnish Cache Github Repository: https://github.com/varnishcache/varnish-cache
In this tutorial, we explained how to setup Varnish Cache 5.0 proxy for Apache HTTP server on CentOS 7. In case you have any queries or additional ideas to share, use the feedback form below to write back to us.