More

    How to Install and Configure Varnish with Apache on CentOS 7

    Introduction

    Varnish is an open source reverse HTTP proxy, an HTTP accelerator, and a useful tool for speeding up an Apache server. Varnish is also known as front-end web caching software that you put in front of an Apache web server to speed it up.

    Varnish can increase the performance of your website and prevent the Apache server from overloading in case of high server traffic.

    Varnish features

    • Reduces CPU load on the Apache web server.
    • Web browser will load your website faster since the cache is stored in RAM.
    • Supports load balancing.

    In this article I will explain how to install and use Varnish as a front-end to an Apache web server.

    Requirements

    • A server running CentOS 7 with Apache installed
    • A static IP address for your server

    Install Varnish

    Before installing Varnish, you will have to install the EPEL repository. You can do this by running the following command:

    sudo yum install -y epel-release
    

    Once the installation is finished you will be able to install Varnish.

    sudo yum install -y varnish
    

    After installation, you will need to start Varnish and enable it to start on boot.

    To do this, run:

    sudo systemctl start varnish
    sudo systemctl enable varnish
    

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

    sudo systemctl status varnish
    

    Check the version of Varnish that is running:

    sudo varnishd -V
    

    Configure Varnish

    The Varnish configuration file will be located in the /etc/varnish directory in CentOS 7. To make Varnish work in front of Apache, you will need to set up some basic configurations.

    By default Varnish listens on port 6081. You will need to change port 6081 to 80 so that website requests access the Varnish cache first. You can do this by editing the varnish.params config file.

    sudo nano /etc/varnish/varnish.params
    

    Change VARNISH_LISTEN_PORT from 6081 to 80:

    VARNISH_LISTEN_PORT=80
    

    Save the file, then open the default.vcl file. This file tells varnish to look for the server content:

    sudo nano /etc/varnish/default.vcl
    

    Tell Varnish to get the content on port 8080.

    backend default {
    .host = "127.0.0.1";
    .port = "8080";
    }
    

    Configure Apache to work with Varnish

    By default Apache listens on port 80. Now, you need to make Apache to run behind Varnish caching by changing the default Apache port to 8080.

    To do this, edit the httpd.conf config file:

    sudo nano /etc/httpd/conf/httpd.conf
    

    Search for Listen 80 and replace it with Listen 8080:

    Listen 8080
    

    Save and close the file, then restart Apache and Varnish to reflect the changes.

    sudo apachectl restart
    sudo systemctl restart varnish
    

    Test Varnish

    Now you should have Varnish and Apache running together. To verify that Varnish is on and working, you can use the curl command to view the HTTP header:

    curl -I http://localhost
    

    You should see the output something like this:

    HTTP/1.1 200 OK
    Date: Wed, 04 Nov 2015 10:21:07 GMT
    Server: Apache/2.4.6 (CentOS)
    Last-Modified: Fri, 02 Oct 2015 10:36:53 GMT
    ETag: "6c-5211cdbf61c14"
    Content-Length: 108
    Content-Type: text/html; charset=UTF-8
    X-Varnish: 32770
    Age: 0
    Via: 1.1 varnish-v4
    Connection: keep-alive
    

    Additional information on Varnish can be located at the Official Varnish Website

    By Victor C.