Magento 2 Varnish and Redis configuration

Varnish and Redis are used as the effective tools for improve the performance of your Magento 2 store. Varnish is a great tool for speed up your website by caching static contents while Redis is a database server which supports frequently asked questions in the cache for the users to help they save time in your store.

The way to configure Varnish and Redis tool for your Magento 2 store

Varnish

  • Install Varnish by using commands below:
    sudo apt-get install apt-transport-https
    
    sudo curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add -
    
    echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list
    
    sudo apt-get update
    
    sudo apt-get install varnish
  • Open file /etc/default/varnish and change following lines:
    DAEMON_OPTS="-a :80 \  
    
                 -T localhost:6082 \
    
                 -f /etc/varnish/default.vcl \
    
                 -S /etc/varnish/secret \
    
                 -s malloc,2048m"  # -a : <set_port>,  -s malloc,<cache_memory>
  • Download Varnish configuration file from the store:
  • Admin Panel > Store > Configuration > Advance > System > Full page cache
  • Change the field built-in-cache to varnish cache and fill the necessary details
  • Tap Save Config button
  • Select Export VCL for Varnish3 or Varnish4
  • Replace /etc/varnish/default.vcl file with vcl
  • Restart Varnish by following command:

Sudo service varnish restart

  • Verify Varnish as caching in the page ad navigate to magento root directory to clear cache:
    sudo php bin/magento cache:flush
    
    sudo rm -rf var/cache/*
    
    sudo rm -rf var/page_cache/*

Now you can open your Magento website to check the varnish status using command: varnishstat. If it displays MAIN.cache_hit, your cache is working well. When restarting the Varnish, cache will be deleted.

Redis

  • Install Redis:
    sudo add-apt-repository ppa:chris-lea/redis-server
    
    sudo apt-get update
    
    sudo apt-get install redis-server
  • Navigate to your magento root directory, open file app/etc/env.php then insert following codes before the last line:
    'cache' =>
    
      array (
    
        'frontend' =>
    
        array (
    
          'default' =>
    
          array (
    
            'backend' => 'Cm_Cache_Backend_Redis',
    
            'backend_options' =>
    
            array (
    
              'server' => '127.0.0.1',
    
              'port' => '6379',
    
              'persistent' => '',
    
              'database' => '0',
    
              'force_standalone' => '0',
    
              'connect_retries' => '1',
    
              'read_timeout' => '10',
    
              'automatic_cleaning_factor' => '0',
    
              'compress_data' => '1',
    
              'compress_tags' => '1',
    
              'compress_threshold' => '20480',
    
              'compression_lib' => 'gzip',
    
            ),
    
          ),
    
          'page_cache' =>
    
          array (
    
            'backend' => 'Cm_Cache_Backend_Redis',
    
            'backend_options' =>
    
            array (
    
              'server' => '127.0.0.1',
    
              'port' => '6379',
    
              'persistent' => '',
    
              'database' => '1',
    
              'force_standalone' => '0',
    
              'connect_retries' => '1',
    
              'read_timeout' => '10',
    
              'automatic_cleaning_factor' => '0',
    
              'compress_data' => '0',
    
              'compress_tags' => '1',
    
              'compress_threshold' => '20480',
    
              'compression_lib' => 'gzip',
    
            ),
    
          ),
    
        ),
    
      ),
  • Restart Redis by using command:
    Sudo service redis-server restart
    
    

Now you can check the status of Redis server in your Magento 2 store by command: sudo redis-cli info. If you see the # Keyspace, the Redis server is working fine or else check your configuration. If you want to flush Redis cache, use command: sudo redis-cli flushdb

That’s all steps to configure the Varnish and Redis for Magento store. If you have any doubt, send us an email for the answers.

Thanks for reading!