In this tutorial we will cover the steps needed to install and configure Varnish with the Apache web server on your CentOS 6.4, Debian or Ubuntu platform.

Varnish is an HTTP accelerator designed for content-heavy dynamic web sites. It can drastically increase the performance of your website and prevent the server from overloading in case of high server traffic.

Requirements:

  • CentOS 6.4, Debian or Ubuntu installed on your computer/server
  • SSH access (Command line access to the server)
  • root privileges
  • Basic skills for working on a Linux environment

We will begin with the installation of the Apache web server which is the most popular HTTP server these days. In order to install the Apache service we will use the default package manager included inside the corresponding Linux distribution:

CentOS 6.4:

Ubuntu/Debian:

You will be prompted to accept the installation and addition package dependencies will be displayed.

Test the functionality of the Apache service

Once the installation is completed you can test the Apache service using your browser:

or

where X.X.X.X is your server's IP address, i.e. http://1.2.3.4/

You should be able to see the default Apache page inside your browser.

An easy way to find your server's IP address is to execute the following command:

Install Varnish

Prior the installation you will need to add the Varnish official repository provided on the Varnish official page:

CentOS 6.4:

Debain:

Ubuntu:

CentOS 6.4:

Ubuntu/Debain:

Configure Varnish and Apache

Once the installation of both services is completed you can continue with their configuration. The Varnish service should listen on the default HTTP port 80 while the Apache server will be configured with port 8080

CentOS 6.4

Open the following file:

and edit the lines from the section “# Alternative 2, Configuration with VCL” using the following syntax:

In case some of the lines related to Alternative 1, 3 or 4 are not commented you should comment them by placing a pound sign (#) at the beginning. This way you will avoid Varnish misconfiguration.

When you are ready save and close the file.

The next Varnish file which should be edited is:

Set the following configuration inside it:

This way the Varnish service will communicate with the Apache server on port 8080.

Now we can continue with the Apache configuration and set the service to operate on port 8080. First we will edit the following file:

and change the default Listen value from 80 to 8080:

At the bottom of the file you should find the following lines:

If the lines are commented then you don't need to make any changes to them. Otherwise you should set the port from 80 to 8080.

In order for the changes to take effect both the Apache and Varnish services should be restarted:

Ubuntu/Debain:

Open the following file:

and edit the lines from the section “## Alternative 2, Configuration with VCL” using the following syntax:

When you are ready save and close the file.

The next Varnish file which should be edited is:

Set the following configuration inside it:

This way the Varnish service will communicate with the Apache server on port 8080.

Now we can continue with the Apache configuration and set the service to operate on port 8080. First we will edit the following file:

Save and close the file. The next file which should be edited is:

Perform the same operation inside it and set port 8080 as default.

When you are ready save and close the file.

The changes will take effect after restart of both the Varnish and Apache services:

Test the functionality of the Varnish service

Once ready you can test the functionality of the Varnish service using the following command:

You should receive output similar to the following one:

Detailed information about the Varnish service can be found on the official project web page.

You can also use the following command on the server in order to receive Varnish statistics in real time :

Start Up Apache/Varnish automatically on server boot

In order for the services to start automatically upon server boot up you can execute the following commands:

CentOS 6.4

Ubuntu/Debain:

At the end you should have a working hosting environment which includes the Apache HTTP server configured to work with the Varnish caching reverse proxy.