How to configure Varnish and Redis in Magento 2

Varnish and Redis are great tools for increase the performance of your Magento2 store. Varnish is a http accelerator which is used increase the speed of the site by caching the static contents. Redis server is a database server which stores frequently asked queries in the cache so users do not have to query the main database server. In this tutorial I will show you how to configure Varnish and Redis in Magento 2.

Before you start you must have configured Magento 2.

Configure Varnish

If you are running your server at port 80 you should run the server at different port because we are using varnish to listen at port 80.

To install varnish run the following commands.

Open /etc/default/varnish and change the following.

You can download Varnish configuration file from the store.

  1. Log in to admin panel of your Magento2 store.
  2. Go to store –> configuration –> advance –> system –> Full page cache
  3. change the field built-in-cache to varnish cache
  4. After filling the details save the configuration.
  5. choose Export VCL for Varnish3 or Export VCL for Varnish4
  6. It will download varnish.vcl file.

Replace the file /etc/varnish/default.vcl with varnish.vcl. Make sure that configuration file name should be default.vcl

Restart Varnish

Now verify  varnish is caching the page. Go to your magento root directory and clear the cache.

Now open your website in the browser and check the varish status in the terminal with this command.

If you see MAIN.cache_hit , then  cache is working fine. As you reload your page MAIN.cache_hit will increase.If you restart varnish , cache will be cleared.

Configure Redis Server

Install Redis server.

Go to your magento root directory and open app/etc/env.php and add the below codes just before the last line.


Restart the Redis server

Check whether Redis server is working or not. Open your website and view some pages then  run the command

If you see this  keyspace. Then your Redis is configured properly. If you do not see anything below Keyspace then Redis have not cached anything. In this case check your configuration



To flush the Redis cache run the following command

Now your site is optimized with Varnish and Redis. If you have any doubt regarding configuration, you can ask me in the comment.


Category(s) magento Uncategorized
. . .


Add Your Comment

Be the first to comment.