• Get In Touch
June 27, 2016

How to use Varnish Cache with Apache on CentOS 7

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

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, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]