How to Install Varnish Cache with Apache on CentOS 7

    Posted in Windows Servers on Oct 09, 2020

    How to Install Varnish Cache with Apache on CentOS 7

    Varnish is a proxy server focused on HTTP caching. It's designed as an HTTP accelerator and can act as reverse proxy for your web server Apache or Nginx. Varnish has been used for high-profile and high-traffic websites,

    Step 1 - Install and configure Apache on CentOS 7

    We will install Apache/httpd web server from the CentOS 7 repository, and then configure the server that will be running on port 8080. Install Apache/httpd using the yum command below.

    yum -y install httpd

    After the installation is complete, start the httpd service and enable it to run at system boot using the commands below.

    systemctl start httpd
    systemctl enable httpd
    

    So the httpd service is now running. Check it using netstat or curl command as shown below.

    netstat -plntu
    curl -I localhost
    

    Step 2 - Configure Apache on port 8080

    Vi /etc/httpd/conf/httpd.conf

    Change the default port 80 to 8080 on the 'Listen' line.

    Listen 8080

    Now test the configuration and make sure there is no error. Then restart the apache/httpd service.

    apachectl configtest

    Step 3 - Install Varnish on CentOS 7

    Install EPEL repository using the following command.

    yum -y install epel-release

    Now install Varnish from the EPEL repository using yum command below.

    yum -y install varnish

    After the installation is complete, start Varnish and add it to launch at system boot using the systemctl commands below

    systemctl start varnish
    systemctl enable varnish
    

    Step 4 - Configure Varnish as a reverse proxy for Apache

    So Varnish is installed, and now we will configure it as a reverse proxy for the Apache web server. Varnish will run on HTTP port 80 by default. Go to the Varnish configuration directory and edit the default configuration in 'default.vcl'.

    cd /etc/varnish
    vim default.vcl
    

    Define the default backend section. We are using Apache web server running on port 8080, so the configuration as below.

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

    Step 5 Save and exit. Now restart Varnish

    Next, configure Varnish to run with HTTP port 80. Edit the 'varnish.params' file present in the Varnish configuration directory. In this file, change the value of the 'VARNISH_LISTEN_PORT' line to HTTP port 80. VARNISH_LISTEN_PORT=80

    vim varnish.params

    Step 6 - Testing

    To test Varnish, first execute the following curl command.

    curl -I hakase-labs.co