Installing Varnish on Centos 7 Apache server

Varnish is an HTTP accelerator,  a server side client cache that can help speed up your Centos 7 Apache server. Varnish can increase the performance of your website and prevents the Apache  server from overloading in case of high server traffic times.

Varnish features

  • Reduce CPU stress on the Apache server.
  • Browsing will load your website quicker because  the cache is stored in RAM.
  • Supports load balancing.

 

Let me explain how to install and use Varnish in front of an Apache server.

You are required to have the following:

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

Step 1:

You need to install the EPEL repository before you install Varnish. Simply follow the command below:

 

sudo yum install -y epel-release

 

Step 2:

Once the EPEL repository is installed you are able to install Varnish. Follow the command below:

 

sudo yum install -y varnish

 

Step 3:

Once Varnish is installed you will need to initiate and enable it. Let’s run this command:

sudo systemctl start varnish
sudo systemctl enable varnish

Step 3:

 

To verify if Varnish is installed. Run this command:

 

sudo systemctl status varnish

 

Step 4:

Verify the version of Varnish you installed. Use this command:

 

sudo varnishd -V

Step 5: Configuration

 

Your Varnish file can be found in the /etc/varnish directory in CentOS 7. To make Varnish work in the front-end of Apache, some basic configurations will need to be configured. Varnish only listens on port 6081. You will need to change port 6081 to port 80 so that http requests accesses the Varnish cache first. This can be done by editing the varnish.params configuration file. Follow the command below:

 

sudo vi /etc/varnish/varnish.params

Now let’s change the VARNISH_LISTEN_PORT from port 6081 to port 80:

 

VARNISH_LISTEN_PORT=80

  • Save the file
  • Open the default.vcl file.

 

This file tells varnish to search for the server information:

 

sudo vi /etc/varnish/default.vcl

Command Varnish to get the content on port 8080.

 

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

Configuring Varnish to work with Apache:

Apache listens to port 80.  Let’s make it listen to port 8080.

 

Let’s edit the httpd.conf file:

 

sudo vi /etc/httpd/conf/httpd.conf

Search for Listen 80 and replace it with Listen 8080:

 

Listen 8080

Save and close the file by hitting the esc button :wq restart Apache and Varnish to reflect the changes.

 

sudo apachectl restart
sudo systemctl restart varnish

Test Varnish

Now Varnish and Apache should be working together. To check the status of Varnish and verify if it is working, you can use the curl command to view the HTTP header:

 

curl -I http://localhost

The output should look like this below:

 

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

If you need more information, check out the official Varnish site at Varnish Website.

Leave a Comment