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.
You can install Apache on your server by running the following commands.
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 -
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 -
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.
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 -
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 -
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.
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
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
Change Listen 80 to Listen 8080 and save the file. Now restart Apache and Varnish by running the following commands -
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.
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.
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...
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...