ManagedCloud Servers

High performance handled and monitored by us 24/7/365. A complete solution to provide you with our in house expertise 24/7 tailored to your specific needs. We'll setup a bespoke server for your site using the latest tehnologies so you can get the most out of your hardware and get your website loading quickly and reliably. Find out more..

cPanelCloud Servers

Recommended - High performance cloud servers with no technical knowledge required. If you're hosting multiple websites already and you're looking to consolidate, or if you're looking to isolate yourself from the shared hosting environment but you don't have the time or knoweldge to manage a server, then the Managed cPanel Servers are for you. Find out more..

UnmanagedCloud Servers

Our unmanaged range gives you complete control at rock bottom prices and our cloud platform boasts super fast multipath 40Gb/s network, the latest Intel Xeon V3 CPUs and enterprise grade redundant SSDs. If you're a sysadmin look no further, we offer some of the best specification to price ratio servers available. Find out more..

Want your very own server? Get our 1GB memory, Xeon V4, 20GB SSD VPS for £10.00 / month.

View Plans

How to use Varnish Cache with Apache on CentOS 7

Varnish Cache accelerates the speed of your website by caching its content. Varnish fetches the content from the web server and stores it as static files. So if somebody requests the same content from the web server, Varnish serves the static pages to them. This way the web server does not have to regenerate the same content on each user's request. Basically it provides a reverse proxy for your HTTP server.

Varnish Cache is very fast and performs very well, even on very high traffic sites. In addition to its performance you can also configure the software easily with the help of VCL language, which is its configuration language. VCL enables you to write policies on how incoming requests should be handled. In such a policy you can decide what content you want to serve, from where you want to get the content and how the request or response should be handled.

Varnish Cache is used by many popular and high-traffic websites like Wikipedia, New York Times, Facebook, Twitter and many more.

In this tutorial we will learn to install Varnish Cache with Apache on CentOS. The only requirement is that you have a CentOS 7 server or VPS with root access on it. In this tutorial we will be using the root account to run the commands. If you are logged in as non root user, use the sudo command at the start of the commands we are going to run.

Installing Apache

You can install Apache on your server by running the following commands.

    yum update
    yum install httpd

The first command will update the packages available on your system. The next command will install Apache on your server.

Now start the Apache server using the following command -

    systemctl start httpd.service

You can run the following command to start Apache every time server boots up -

    systemctl enable httpd.service

Now browse to your website using your favorite browser, you should see the default CentOS test page.

Installing Varnish Cache

Varnish does not come with the default yum repository of CentOS so you will need to add EPEL repository to your server by running the following commands -

    yum install epel-release
    rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.1.el7.rpm

Now install Varnish Cache by running the following command -

    yum install varnish

This will install Varnish Cache along with the dependencies it require. To run Varnish Cache execute the following command -

    systemctl start varnish

To start Varnish at the time of boot, run the following command.

    systemctl enable varnish

Configuring Varnish Cache

As we have both Varnish and Apache installed, we can now configure them to make our website faster. By default Varnish listens to port number 6081 and Apache listens to 80. We will configure Varnish to run on port 80, while fetching content from Apache, which will run on port 8080.

Edit default configuration file of Varnish Cache, varnish.params using your favorite text editor. We will be using nano here -

    nano /etc/varnish/varnish.params

Now find the following lines -

    # VARNISH_LISTEN_ADDRESS=192.168.1.5
    VARNISH_LISTEN_PORT=6081

And change VARNISH_LISTEN_PORT to 80, it should look like this -

    # VARNISH_LISTEN_ADDRESS=192.168.1.5
    VARNISH_LISTEN_PORT=80

You can also configure the amount of RAM which Varnish can use for storing it's cache. Find the following lines in the configuration.

    # Backend storage specification, see Storage Types in the varnishd(5)
    # man page for details.
    VARNISH_STORAGE="malloc,256M"

If your server has lots of RAM you can increase the RAM Varnish can uses. For example, if you want to allocate 512MB RAM to Varnish, change the above value to VARNISH_STORAGE="malloc,512M". If you want to allocate 2GB then use VARNISH_STORAGE="malloc,2G"

If you want Varnish to use a file on disk to store its cache, you can change VARNISH_STORAGE value to VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,2G". Using the RAM for storing varnish data is recommended because Varnish will have faster access over data, thus it will be able to serve pages quickly. Save the file and exit from text editor.

Now edit the Default VCL file of Varnish Cache using the following command -

    nano /etc/varnish/default.vcl

And find the setting for backend default, in newer versions you will find the that it is already pointing to 8080 on which we will be pointing our Apache to. If not change the setting to make it similar to the following -

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

Here the host we are using is the Loopback IP which always points to the local server and for the port we are using 8080 on which Apache will be running. Save the file once done.

Now we will need to configure Apache to run on port number 8080. Edit the configuration file of Apache using the following command.

    nano /etc/httpd/conf/httpd.conf

Scroll down to find the following lines

    # Listen: Allows you to bind Apache to specific IP addresses and/or
    # ports, instead of the default. See also the 
    # directive.
    #
    # Change this to Listen on specific IP addresses as shown below to
    # prevent Apache from glomming onto all bound IP addresses.
    #
    #Listen 12.34.56.78:80
    Listen 80

Change Listen 80 to Listen 8080 and save the file. Now restart Apache and Varnish by running the following commands -

    systemctl restart httpd.service
    systemctl restart varnish

Verifying Varnish Cache

You can easily verify if Varnish is running by running the following command.

    curl -I localhost

You will see output similar to this -

    HTTP/1.1 403 Forbidden
    Date: Sat, 25 Jun 2016 18:40:35 GMT
    Server: Apache/2.4.6 (CentOS)
    Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
    ETag: "1321-5058a1e728280"
    Accept-Ranges: bytes
    Content-Length: 4897
    Content-Type: text/html; charset=UTF-8
    X-Varnish: 65563
    Age: 0
    Via: 1.1 varnish-v4
    Connection: keep-alive

In above output we can see X-Varnish: 65563 and Via: 1.1 varnish-v4. This shows that Varnish is working properly and the page sent to you was cached and processed by Varnish.

Conclusion

In this tutorial we have learnt how to install Varnish Cache to work with Apache on CentOS 7. Now you can easily install Varnish cache and configure it for use with Apache to enable caching for your website.

Want your very own server? Get our 1GB memory, Xeon V4, 20GB SSD VPS for £10.00 / month.

View Plans