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.
If you find your cPanel disk space filling up, or an email address has hit its disk space quota, cPanel has a helpful built in Email Disk Usage tool. This will provide you with a simple to understand breakdown of how much disk space each folder for a particular email...
Although WHM will normally automatically keep itself up to date, you may want to manually check for server updates / push through an update that is pending, or it may be that you have automatic updates switched off on your cPanel server. In this guide we will show you how...
You may sometimes need to manually adjust the PHP settings on your cPanel server – for example if a site is hitting the PHP memory, or file size upload limit. WHM allows you to quickly change the settings of any PHP version installed on the server when needed, using the...