How to Install & Configure Varnish Cache in Magento 2

The Most Popular Extension Builder for Magento 2

With a big catalog of 234+ extensions for your online store

There are many important factors that make up a valuable website; caching is one of them. It is an essential part of any website. It’s even more urgent for websites running on the Magento 2 platform. Thanks to the advantages of caching, such as reducing page load time, increasing website loading speed, it can bring customers the best experience when shopping online.

One of the top caching techniques that the Magento team highly recommends to users in Magento 2 is Varnish. Although each website has a built-in cache for the whole page, it runs very slowly compared to integrating other software. Varnish plays a vital role in managing the cache mechanism, which significantly reduces the workload on the server. Magento 2 and Varnish are a proven combination helping boost sales effectively.

In this blog, let’s explore a step-by-step guide about How to Install & Configure Varnish Cache in Magento 2.

Table of Contents

Varnish Cache overview

First of all, let’s take a quick look at the Varnish Cache solution to see what it is specifically?

Varnish Cache, also known as an HTTP accelerator or caching HTTP reverse proxy, is a web application accelerator built on open source code. Varnish Cache helps reduce web page response time and network bandwidth consumption for future equivalent requests. Unlike other web servers like Apache or Nginx, Varnish is designed for use with HTTP protocol.

Currently, Magento 2.4.2 has been tested with Varnish 6.4. And of course, Magento 2.4.x is fully compatible with Varnish 6.x.

The importance of Varnish Cache for Magento 2 stores

Thanks to the integration of Varnish, customers will no longer have to spend a lot of time and effort waiting for the page load when purchasing or searching for products. As a result, the customer’s user experience improves dramatically. If you are still not convinced, some key reasons below will give you a better understanding which advantages Varnish Cache brings.

  • Reduce the page loading time and show the contents 10X faster. This will help increase sales effectively.
  • Increase more traffic and your brand reputation. This helps your website get high reliability and ranking on the Search Engine Results Page.
  • Thanks to Varnish Configuration Language, you can customize the way that Varnish handles incoming and outgoing requests.
  • Offer multi-layer servers that allow you to enable the high availability of a cluster processing solution.

How to install Varnish Cache in Magento 2

Despite the fact that Magento has integrated a caching mechanism, it’s not enough. Because of this, Varnish is suggested to apply for every Magento 2 store. To run Varnish on Magento 2, installing Varnish and deploying Varnish Configuration File is urgent.

Please run the following command lines as a root user (sudo su)

apt-get install apt-transport-https
curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -
echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" \
     >> /etc/apt/sources.list.d/varnish-cache.list
apt-get update
apt-get install varnish

After that, you will see the Varnish version below if everything is correct.

varnished -V

Congratulations! You have successfully installed Varnish on Magento 2.

How to configure Varnish Cache in Magento 2

Step 1: Configure your web server

After installing Varnish, you need to configure the current web server. You had better configure your web server to listen on a port 8080 other than the default port 80.

Now it’s time to find your web server and change the listen port to 8080.


 
server {
    listen 8080 default_server;
    server_name your-magento2-store.dev;
    ...
}

Remember that we use port 8080 as an example in this section.

Step 2: Modify Varnish System Configuration

To modify Varnish System Configuration, please open your Varnish Configuration File in a text editor:

/etc/default/varnish

Then, modify the file to get a similar code below:

START=yes
 
# Maximum number of open files (for ulimit -n)
NFILES=131072
 
# Maximum locked memory size (for ulimit -l)
# Used for locking the shared memory log in memory. If you increase log size,
# you need to increase this number as well
MEMLOCK=82000
 
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -p http_resp_hdr_len=64000 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Step 3: Modify default.vcl

  1. First and foremost, you need to backup the old default one:

cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak

  1. Next, open /etc/varnish/default.vcl in a text editor
  2. Then, modify the lines under ‘backend default’:

# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}

  1. You need to replace the value of .host with a qualified hostname or IP address as well as the listen port of the Varnish backend or origin server. It’s the server offering the content that Varnish will gain. Commonly, it’s your web server.
  2. You need to replace the value of .port with the listen port (8080) of your web server.

Step 4: Configure Magento to use Varnish

To configure Magento for using Varnish, you need to enable Full Page Cache. Please go to Stores > Configuration > Advanced > System > Full Page Cache, you will see the below image:

Enable Full Page Cache
Enable Full Page Cache

Then, choose Varnish Cache from the Caching Application list.

Caching Application
Caching Application

Next step, you can change the value in TTL for public content field by increasing or decreasing the lifetime value of the public content cache if you want.

TTL for public content
TTL for public content

Afterward, by expanding the Varnish Configuration and entering the following details, you can enable advanced options.

  • Determine the IP or Host that is allowed to delete the Varnish Cache in the “Access list”. Then, please add your domain name of the application in this field.
  • Determine the Backend Host.
  • Backend Port is the port of the Apache server.
  • Define the Grace period to know the time Varnish servers stale content suppose the backend is not responding.

Finally, please click on the Save Config button if you are done. Another way to enable Magento 2 Varnish, you can do it through the command line interface via SSH. If doing this way, please use the following commands:

bin/magento 
config:set--scope=default--scope-code=0system/full_page_cache/caching_application 2

How to Verify Varnish in Magento 2

1. Check suppose Varnish is listening to port 80

To check if Varnish is listening to port 80, please run the command below:

netstat -tulpn | grep varnished

Verify Varnish in Magento 2
Verify Varnish in Magento 2

2. Verify your contents in var/cache, var/page_cache folder is not recreated

Once FPC is cleared, cache folders (var/cache, var/page_cache) should be empty because FPC is set up to serve the content from Varnish.

3. Verify the HTTP response headers

To verify the HTTP response headers, you need to load your Magento 2 Frontend and check the response from the browser console. Afterward, you will see a similar screenshot below:

Magento 2 Frontend
Magento 2 Frontend

That’s it. You have completed the Varnish Cache configuration in Magento 2. Let your online store fly!

Final words

To sum up, integrating Varnish Cache in Magento 2 is an ideal solution for your online store if you want to instantly reduce the page load time and increase the page load speed. Thanks to this integration, your website will not only enhance the user experience, but it also helps boost sales dramatically. Only with some minimal changes can you beautify your online business quickly.

If I have missed any key points related to this topic, or you have any contribution to any documentation for our blog, please feel free to comment below.

Thank you for reading!

Looking for
Customization & Development Services?

8+ years of experiences in e-commerce & Magento has prepared us for any challenges, so that we can lead you to your success.

Get free consultant
development service

Explore Our Products:

Subscribe

Stay in the know

Get special offers on the latest news from Mageplaza.

Earn $10 in reward now!

Earn $10 in reward now!

comment
iphone
go up