Redis is a popular key-value storage database and Magento 2 includes support for both page cache and sessions out of the box. In this guide we will cover how to configure Redis for Magento 2 through the command line.
Before configuring Redis, we will need the IP, a value we will later use as the Redis host. To find the correct path to your Redis instance, log in to the portal, click on the site you are configuring and under the Environment tab you will find you Redis IP:
Once we have this value, we will configure Redis default caching by running:
php bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=10.75.17.192 --cache-backend-redis-port=39706 --cache-backend-redis-db=0
Now, we do the same for Redis page caching:
php bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=10.75.17.192 --page-cache-redis-port=39706 --page-cache-redis-db=1
As a result, you should see something like the following in your app/etc/env.php file:
'cache' => [ 'frontend' => [ 'default' => [ 'id_prefix' => '14c_', 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => [ 'server' => '10.75.17.192', 'database' => '0', 'port' => '39706', 'password' => '', 'compress_data' => '1', 'compression_lib' => '' ] ], 'page_cache' => [ 'id_prefix' => '14c_', 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => [ 'server' => '10.75.17.192', 'database' => '1', 'port' => '39706', 'password' => '', 'compress_data' => '0', 'compression_lib' => '' ] ] ] ],
Once we are done with default and page cache, we will configure Redis for session storage:
php bin/magento setup:config:set --session-save=redis --session-save-redis-host=10.75.17.192 --session-save-redis-port=39706 --session-save-redis-log-level=4 --session-save-redis-db=2
Once completed, you should see something like the following in your app/etc/env.php file:
'session' => [ 'save' => 'redis', 'redis' => [ 'host' => '10.75.17.192', 'port' => '39706', 'password' => '', 'timeout' => '2.5', 'persistent_identifier' => '', 'database' => '2', 'compression_threshold' => '2048', 'compression_library' => 'gzip', 'log_level' => '4', 'max_concurrency' => '6', 'break_after_frontend' => '5', 'break_after_adminhtml' => '30', 'first_lifetime' => '600', 'bot_first_lifetime' => '60', 'bot_lifetime' => '7200', 'disable_locking' => '0', 'min_lifetime' => '60', 'max_lifetime' => '2592000', 'sentinel_master' => '', 'sentinel_servers' => '', 'sentinel_connect_retries' => '5', 'sentinel_verify_master' => '0' ] ],
There are extra configurations and parameters you can use for Redis in Magento 2, read the official documentation for a better understanding about when and how to use them.
A simple way to verify everything is working as expected is to navigate to var/cache, var/page_cache and var/session and check if the folders are empty. If they are and you can normally navigate your site, Redis has been correctly configured. If you still see files being generated inside those folders, something might have been wrongly installed.
You might be asked to overwrite old values in the app/etc/env.php file. In the case you aren’t sure what to do, answer No and create a backup of app/etc/env.php before repeating the process.
Our award-winning customer care team is here for you.Contact Support