/
Pack Hosting Panel

Configure Redis in Magento 2

How do you configure Redis for a Magento 2 environment?


In Magento 2, Redis can be configured easily by using CLI tools of Magento.
In this article, we explain how to do this. We assume that you are familiar with Redis and know how to configure Redis.

Redis cache

To make use of the commands below, you navigate to the root of the webshop: /home/<username>/domains/<domain-name>/application/. Please note with this path, we assume that your Magento 2 shop is located in an application folder, however it might be different. We also assume that the standard Redis socket is being used.

With the following command, you can enable Redis cache:

php bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=/home/<username>/domains/<domain-name>/var/run/redis.sock --cache-backend-redis-db=0

Moreover, you can use Redis as a full page cache solution. Configure the full page cache with the following command:

php bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=/home/<username>/domains/<domain-name>/var/run/redis.sock --page-cache-redis-db=1

The commands above will add the configuration below to the env.php file:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '/home/<username>/domains/<domain>/var/run/redis.sock',
                'database' => '0',
                'port' => '0'
            ],
        ],
        'page_cache' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '/home/<username>/domains/<domain>/var/run/redis.sock',
                'port' => '0',
                'database' => '1',
                'compress_data' => '0'
            ],
        ],
    ],
],

Instead of making use of commands, you can choose to add the configuration above manually in the env.php file.

Redis session

In addition to the cache, sessions can also be saved in Redis. You can set Redis sessions with the following command:

php bin/magento setup:config:set --session-save=redis --session-save-redis-host=/home/<username>/domains/<domain-name>/var/run/redis.sock --session-save-redis-db=2

In the example above, we only provide a limited number of parameters. If parameters are not included, Magento uses the default values. You can find a complete overview of all parameters that you can give to the command here.

The above command puts a similar configuration in the env.php file:

'session' => [
    'save' => 'redis',
    'redis' => [
        'host' => '/home/<username>/domains/<domain>/var/run/redis.sock',
        'port' => '0',
        'password' => '',
        'timeout' => '2.5',
        'persistent_identifier' => '',
        'database' => '2',
        'compression_threshold' => '2048',
        'compression_library' => 'gzip',
        'log_level' => '3',
        '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'
    ],
],

Also for a Redis session, the choice can be made to place the configuration in the env.php file.

Test the configuration

As soon as Redis has been configured for the desired elements, we can test if everything works properly. Therefore, use the following command:

redis-cli -s /home/<username>/domains/<domain-name>/var/run/redis.sock monitor

Then visit the webshop for which Redis is set for. If Redis is set correctly you will see the similar output in the console where you ran the monitor command:

Cache:

1476826133.810090 [0 127.0.0.1:52366] "select" "1"
1476826133.816293 [0 127.0.0.1:52367] "select" "0"
1476826133.817461 [0 127.0.0.1:52367] "hget" "zc:k:ea6_GLOBAL__DICONFIG" "d"
1476826133.829666 [0 127.0.0.1:52367] "hget" "zc:k:ea6_DICONFIG049005964B465901F774DB9751971818" "d"
1476826133.837854 [0 127.0.0.1:52367] "hget" "zc:k:ea6_INTERCEPTION" "d"
1476826133.868374 [0 127.0.0.1:52368] "select" "1"
1476826133.869011 [0 127.0.0.1:52369] "select" "0"
1476826133.869601 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_DEFAULT__10__235__32__1080MAGENTO2" "d"
1476826133.872317 [0 127.0.0.1:52369] "hget" "zc:k:ea6_INITIAL_CONFIG" "d"
1476826133.879267 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL_PRIMARY_PLUGIN_LIST" "d"
1476826133.883312 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL__EVENT_CONFIG_CACHE" "d"
1476826133.898431 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DB_PDO_MYSQL_DDL_STAGING_UPDATE_1" "d"
1476826133.898794 [0 127.0.0.1:52369] "hget" "zc:k:ea6_RESOLVED_STORES_D1BEFA03C79CA0B84ECC488DEA96BC68" "d"
1476826133.905738 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_STORE_DEFAULT_10__235__32__1080MAGENTO2" "d"

Session:

1476824834.187250 [0 127.0.0.1:52353] "select" "0"
1476824834.187587 [0 127.0.0.1:52353] "hmget" "sess_sgmeh2k3t7obl2tsot3h2ss0p1" "data" "writes"
1476824834.187939 [0 127.0.0.1:52353] "expire" "sess_sgmeh2k3t7obl2tsot3h2ss0p1" "1200"
1476824834.257226 [0 127.0.0.1:52353] "select" "0"
1476824834.257239 [0 127.0.0.1:52353] "hmset" "sess_sgmeh2k3t7obl2tsot3h2ss0p1" "data" "_session_validator_data|a:4:{s:11:\"remote_addr\";s:12:\"10.235.34.14\";s:8:\"http_via\";s:0:\"\";s:20:\"http_x_forwarded_for\";s:0:\"\";s:15:\"http_user_agent\";s:115:\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36\";}_session_hosts|a:1:{s:12:\"10.235.32.10\";b:1;}admin|a:0:{}default|a:2:{s:9:\"_form_key\";s:16:\"e331ugBN7vRjGMgk\";s:12:\"visitor_data\";a:3:{s:13:\"last_visit_at\";s:19:\"2016-10-18 21:06:37\";s:10:\"session_id\";s:26:\"sgmeh2k3t7obl2tsot3h2ss0p1\";s:10:\"visitor_id\";s:1:\"9\";}}adminhtml|a:0:{}customer_base|a:1:{s:20:\"customer_segment_ids\";a:1:{i:1;a:0:{}}}checkout|a:0:{}" "lock" "0"