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.
1 2 3 4 5 6 |
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 /etc/default/varnish and change the following.
1 2 3 4 5 6 7 |
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> |
You can download Varnish configuration file from the store.
- Log in to admin panel of your Magento2 store.
- Go to store –> configuration –> advance –> system –> Full page cache
- change the field built-in-cache to varnish cache
- After filling the details save the configuration.
- choose Export VCL for Varnish3 or Export VCL for Varnish4
- 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
1 |
sudo service varnish restart |
Now verify varnish is caching the page. Go to your magento root directory and clear the cache.
1 2 3 |
sudo php bin/magento cache:flush sudo rm -rf var/cache/* sudo rm -rf var/page_cache/* |
Now open your website in the browser and check the varish status in the terminal with this command.
1 |
varnishstat |
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.
1 2 3 4 |
sudo add-apt-repository ppa:chris-lea/redis-server sudo apt-get update sudo apt-get 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
'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 the Redis server
1 |
sudo service redis-server restart |
Check whether Redis server is working or not. Open your website and view some pages then run the command
1 |
sudo redis-cli info |
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
1 |
sudo redis-cli flushdb |
Now your site is optimized with Varnish and Redis. If you have any doubt regarding configuration, you can ask me in the comment.
Be the first to comment.