CentOSLinuxTutorials

How To Install Varnish Cache 4 on CentOS 7

Install Varnish Cache 4 on CentOS 7

In this tutorial, we will show you how to install Varnish Cache 4 on CentOS 7. For those of you who didn’t know, Varnish Cache is a powerful open-source HTTP accelerator that can be installed in front of any Webserver such as Apache or Nginx. Varnish Cache can improve the overall performance of your web server by caching content. The Varnish cache stores the copy of user requests and serves the same page when the user revisits the webpage. It makes your website really fast and accelerates your website performance up to 300 – 1000x (means 80% or more).

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of Varnish Cache on CentOS 7.

Prerequisites

  • A server running one of the following operating systems: CentOS 7.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Varnish Cache 4 on CentOS 7

Step 1. First, you need to enable the EPEL repository on your system.

## RHEL/CentOS 7 64-Bit ##
# wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
# rpm -ivh epel-release-7-5.noarch.rpm

Step 2. Install Varnish using the Yum command.

yum -y update
yum -y install varnish

Start varnish and make varnish start at boot:

systemctl start varnish.service
systemctl enable varnish.service

To check the status of varnish, run the following command:

systemctl status varnish

Step 3. Configuring Varnish.

Edit the Varnish configuration under /etc/varnish:

### nano varnish.params
# Default address and port to bind to. Blank address means all IPv4
# and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
# quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=192.168.1.5
VARNISH_LISTEN_PORT=80

# Admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=192.168.136.131
VARNISH_ADMIN_LISTEN_PORT=6082

Edit default.vcl:

### nano default.vcl
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "192.168.136.131";
    .port = "8080";
}

Edit apache web server configuration and change listen port to 8080:

### nano /etc/httpd/conf/httpd.conf
Listen 8080

Edit firewall to allow the HTTP service:

firewall-cmd --permanent --zone=internal --add-service=http
firewall-cmd --reload

Restart the Apache and Varnish service for the changes to take effect:

systemctl restart varnish
systemctl restart httpd

Step 4. Testing Varnish.

The test consists of making an HTTP request via curl and verifying that it is handled by Varnish:

[root@idroot.us ]# curl -I 192.168.146.131
 HTTP/1.1 403 Forbidden
 Date: Mon, 02 Jun 2015 23:04:10 GMT
 Server: Apache/2.4.6 (CentOS) PHP/5.4.16
 Last-Modified: Thu, 16 Dec 2014 19:30:58 GMT
 ETag: "1321-5058a1e728280"
 Accept-Ranges: bytes
 Content-Length: 4897
 Content-Type: text/html; charset=UTF-8
 X-Varnish: 32779
 Age: 0
 Via: 1.1 varnish-v4
 Connection: keep-alive

Congratulations! You have successfully installed a varnish cache. Thanks for using this tutorial for installing Varnish 4 on CentOS 7 system. For additional help or useful information, we recommend you to check the official Varnish on the website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button