5

Magento 2's CLI has a setup:install command that will let you generate an initial configuration

php bin/magento setup:install --admin-email astorm@example.com --admin-firstname Alan --admin-lastname Storm --admin-password 'whymagentowhy?@###@' --admin-user username --backend-frontname admin.bak --base-url http://magento.example.com --db-host 127.0.0.1 --db-name db_name --db-password yeahright --db-user notroot --session-save files --use-rewrites 1 --use-secure 0 -vvv

Is there [a version of this command, another command, or some third party project] that will automatically generate the needed configuration nodes for a redis cache and redis sessions?

1
  • Would seem a little counterproductive to offer the driver, but not settings! Still even in 2021!
    – MackieeE
    Jun 11 at 15:43
2

I just have starter env file what i copy to 'app/etc/env.php' before running the installer command:

<?php
return array(
    'session' =>
        array(
            'save' => 'redis',
            'redis' =>
                array(
                    'host' => '127.0.0.1',
                    'port' => '6379',
                    'password' => '',
                    'timeout' => '2.5',
                    'persistent_identifier' => '',
                    'database' => '0',
                    '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',
                ),
        ),
    'cache' =>
        array(
            'frontend' =>
                array(
                    'default' =>
                        array(
                            'backend' => 'Cm_Cache_Backend_Redis',
                            'backend_options' =>
                                array(
                                    'server' => '127.0.0.1',
                                    'port' => '6379',
                                    'database' => '1',
                                ),
                        ),
                    'page_cache' =>
                        array(
                            'backend' => 'Cm_Cache_Backend_Redis',
                            'backend_options' =>
                                array(
                                    'server' => '127.0.0.1',
                                    'port' => '6379',
                                    'database' => '1',
                                    'compress_data' => '0',
                                ),
                        ),
                ),
        )
);

The install shouldn't replace anything from the env.php if you don't tell it to.

1

For a local environment setup, I put into puppet some commands for adding Redis to the local environment.

Two files:

<?php // magento-configured-with-redis.php

$file = '/var/www/Magento/app/etc/env.php';

if (!file_exists($file)) {
    exit(1);
}

$data = require($file);

if (!isset($data['session']) || !isset($data['session']['save']) || $data['session']['save'] != 'redis') {
    exit(2);
}

if (!isset($data['cache'])
    || !isset($data['cache']['frontend'])
    || !isset($data['cache']['frontend']['default'])
    || !isset($data['cache']['frontend']['default']['backend'])
    || $data['cache']['frontend']['default']['backend'] != 'Cm_Cache_Backend_Redis'
) {
    exit(3);
}

exit(0);

and

<?php // magento-configure-redis.php

$file = '/var/www/Magento/app/etc/env.php';

if (!file_exists($file)) {
    exit(1);
}

$data = require($file);

const REDIS_HOST = '127.0.0.1';
const REDIS_PORT = '6379';

$data['session']['save'] = 'redis';
$data['session']['redis'] = [
    'host' => REDIS_HOST,
    'port' => REDIS_PORT,
    'database' => 0,
    'compression_threshold' => 0,
];

$data['cache']['frontend']['default'] = [
    'backend' => 'Cm_Cache_Backend_Redis',
    'backend_options' => [
        'server' => REDIS_HOST,
        'port' => REDIS_PORT,
        'database' => 1,
        'compression_threshold' => 0,
    ],
];

$export = [
    '<?php',
    'return '.var_export($data, true).';',
    ''
];

file_put_contents($file, implode(PHP_EOL, $export));

The puppet line being:

  exec { 'Configure Magento with Redis':
    require => [Exec['Install Magento'], Tools::Loadtool['magento-configure-redis.php'], Class['redis']],
    command => '/usr/bin/php /home/vagrant/tools/magento-configure-redis.php',
    unless  => '/usr/bin/php /home/vagrant/tools/magento-configured-with-redis.php',
    user    => 'vagrant',
  }

As you can see, magento-configured-with-redis is designed to throw a non-zero code if and only if Magento is not configured with redis, and a 0 code if and only if Magento is configured with redis.

magento-configure-redis.php is then setup to take the data in the env.php file, and add the default redis configuration to it.

It's far from what you seem to be looking for, but it's a start.

1

I have a patch for app/etc/env.php (smth like this):

Index: app/etc/env.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- app/etc/env.php (revision )
+++ app/etc/env.php (revision )
@@ -1,5 +1,27 @@
 <?php
 return array (
+    /* SUPPLZ-321 */
+    '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'
+                )
+            )
+        )
+    ),
   'backend' => 
   array (
     'frontName' => 'admin',

and I apply this patch on deploy:

patch ${DIR_MAGE}/app/etc/env.php ${DIR_DEPLOY}/patch/XXX.patch
1

You can use the following commands to enable Redis (taken from this page and this page in DevDocs):

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=redis.example.com --cache-backend-redis-db=0
bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=redis.example.com --page-cache-redis-db=1
bin/magento setup:config:set --session-save=redis --session-save-redis-host=redis.example.com --session-save-redis-log-level=3 --session-save-redis-db=2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.