How to configure Varnish and Redis in Magento 2

Updated 7 October 2024

Varnish and Redis are great tools for increasing the performance of your Magento2 store.

Varnish is an HTTP accelerator that is used to increase the speed of the site by caching the static contents.

Redis server is a database server that stores frequently asked queries in the cache so users do not have to query the main database server.

In this tutorial, we will show you how to configure Varnish and Redis in Magento 2.

Before you start you must have configured Magento 2 and varnish must be enabled from the Magento backend.

Configure Varnish

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

You can define the port as per your requirement, and update the port in the varnish file accordingly.

To install the service run the following commands.


To check its version, run the below command –
# varnishd -V

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


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


We are using this custom vcl file instead of using the default vcl file for configuring varnish version 6.

Restart the service


Now verify service is caching the page. Go to your Magento root directory and clear the cache.


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


If you see MAIN.cache_hit, that means the varnish cache is working fine.

As you reload your page MAIN.cache_hit will increase. If you restart the service, the cache will be cleared.

Configure Redis Server

Install the Redis server.


To check the Redis version, run the below command-
# redis-server -v

Run the below command to check whether the Redis service is installed properly or not

#redis-cli
ping

A ‘pong’ response will come which shows the Redis is working fine.

Go to your Magento root directory and run the below command to update the env.php file


Restart the Redis server


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


If you see this keyspace and some values, then it means your Redis is configured properly.

If you do not see anything below Keyspace then Redis have not cached anything. In this case, you need to cross-check your Redis configuration

To flush the Redis cache run the following command


Now your site has been optimized with Varnish and Redis cache.

Need Support?

Thank You for reading this Blog!

For further more interesting blogs, keep in touch with us. If you need any kind of support, simply raise a ticket at https://webkul.uvdesk.com/en/.

You may also visit our Odoo development services and quality Odoo Extensions.

For further help or queries, please contact us or raise a ticket.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home