How to configure Redis for Magento 2

    on Nexcess

    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.

    Prerequisites:

    • Cloudhost with Magento 2.x 
    • The host must have Redis installed (All Nexcess servers have Redis installed)

    Before configuring Redis, we will need the path to the sock, 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 sock location:

    Configuration:

    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=/var/run/redis-multi-a6c738c1.redis/redis.sock --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=/var/run/redis-multi-a6c738c1.redis/redis.sock --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' => '/var/run/redis-multi-a6c738c1.redis/redis.sock',
                        'database' => '0',
                        'port' => '6379',
                        'password' => '',
                        'compress_data' => '1',
                        'compression_lib' => ''
                    ]
                ],
                'page_cache' => [
                    'id_prefix' => '14c_',
                    'backend' => 'Cm_Cache_Backend_Redis',
                    'backend_options' => [
                        'server' => '/var/run/Redis-multi-a6c738c1.redis/redis.sock',
                        'database' => '1',
                        'port' => '6379',
                        '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=/var/run/redis-multi-a6c738c1.redis/redis.sock  --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' => '/var/run/redis-multi-a6c738c1.redis/redis.sock',
                'port' => '6379',
                '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.


    Known issues:

    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.



    Was this article helpful?

    Send feedback

    Can’t find what you’re looking for?

    Our award-winning customer care team is here for you.

    Contact Support