Configure Redis for Magento 2

Install Redis

apt-get install redis-server

vi /etc/redis/redis.conf
Enable Unix sockets by uncommenting:
unixsocket /var/run/redis/redis.sock
unixsocketperm 777

pecl install redis
vi /etc/php/7.0/fpm/conf.d/10-redis.ini
# Add:
extension=redis.so
service redis start

Configure from shell:

# Better connect through Unix socket:
php7.2 bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=/var/run/redis/redis.sock --cache-backend-redis-db=0;
php7.2 bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=/var/run/redis/redis.sock --page-cache-redis-db=1;
php7.2 bin/magento setup:config:set --session-save=redis --session-save-redis-host=/var/run/redis/redis.sock --session-save-redis-log-level=3 --session-save-redis-db=2;

# Or connect through TCP/IP socket:
php7.2 bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0;
php7.2 bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1;
php7.2 bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-log-level=3 --session-save-redis-db=2;

Or add manually to env.php:

vi app/etc/env.php

Add cache node with:
'cache' =>
array(
   'frontend' =>
   array(
      'default' =>
      array(
         'backend' => 'Cm_Cache_Backend_Redis',
         'backend_options' =>
         array(
            'server' => '127.0.0.1',
            'database' => '0',
            'port' => '6379'
            ),
    ),
    'page_cache' =>
    array(
      'backend' => 'Cm_Cache_Backend_Redis',
      'backend_options' =>
       array(
         'server' => '127.0.0.1',
         'port' => '6379',
         'database' => '1',
         'compress_data' => '0'
       )
    )
  )
),

Replace session node with:
'session' =>
array (
  'save' => 'redis',
  'redis' =>
  array (
    'host' => '127.0.0.1',
    'port' => '6379',
    'password' => '',
    'timeout' => '2.5',
    'persistent_identifier' => '',
    'database' => '2',
    'compression_threshold' => '2048',
    'compression_library' => 'gzip',
    'log_level' => '1',
    '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'
  )
),
Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp