|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +group: config-guide |
| 4 | +title: Use Redis for session storage |
| 5 | +version: 2.3 |
| 6 | +github_link: config-guide/redis/redis-session.md |
| 7 | +functional_areas: |
| 8 | + - Configuration |
| 9 | + - System |
| 10 | + - Setup |
| 11 | +--- |
| 12 | + |
| 13 | +## Prerequisite {#reds-cache-prereq} |
| 14 | + |
| 15 | +If Redis will be used for default or page caching, you must [install Redis]({{page.baseurl}}config-guide/redis/config-redis.html#config-redis-install) before continuing. . |
| 16 | + |
| 17 | +## Configure Magento to use Redis for session storage {#config-redis-config} |
| 18 | + |
| 19 | +Magento now provides command line options to configure Redis session storage. In previous releases, you edited the `<Magento install dir>app/etc/env.php` file. The command line provides validation and is the recommended configuration method, but you can still edit the `env.php` file. |
| 20 | + |
| 21 | +Run the `setup:config:set` command and specify Redis-specific parameters. |
| 22 | + |
| 23 | +```bash |
| 24 | +bin/magento setup:config:set --session-save=redis --session-save-redis-<parameter_name>=<parameter_value>... |
| 25 | +``` |
| 26 | + |
| 27 | +where |
| 28 | + |
| 29 | +`--session-save=redis` enables Redis session storage. If this feature has already been enabled, omit this parameter. |
| 30 | + |
| 31 | +`--session-save-redis-<parameter_name>=<parameter_value>` is a list of parameter/value pairs that configure session storage: |
| 32 | + |
| 33 | +<table> |
| 34 | +<tbody> |
| 35 | +<tr> |
| 36 | +<th>Command line Parameter</th> |
| 37 | +<th>Parameter name</th> |
| 38 | +<th>Meaning</th> |
| 39 | +<th>Default value</th> |
| 40 | +</tr> |
| 41 | +<tr> |
| 42 | +<td>session-save-redis-host</td> |
| 43 | +<td>host</td> |
| 44 | +<td> |
| 45 | +<p>Fully qualified host name, IP address, or absolute path if using UNIX sockets.</p> |
| 46 | +</td> |
| 47 | +<td>localhost</td> |
| 48 | +</tr> |
| 49 | +<tr> |
| 50 | +<td>session-save-redis-port</td> |
| 51 | +<td>port</td> |
| 52 | +<td>Redis server listen port.</td> |
| 53 | +<td>6379</td> |
| 54 | +</tr> |
| 55 | +<tr> |
| 56 | +<td>session-save-redis-password</td> |
| 57 | +<td>password</td> |
| 58 | +<td>Specifies a password if your Redis server requires authentication.</td> |
| 59 | +<td>empty</td> |
| 60 | +</tr> |
| 61 | +<tr> |
| 62 | +<td>session-save-redis-timeout</td> |
| 63 | +<td>timeout</td> |
| 64 | +<td> |
| 65 | +<p>Connection timeout, in seconds.</p> |
| 66 | +</td> |
| 67 | +<td>2.5</td> |
| 68 | +</tr> |
| 69 | +<tr> |
| 70 | +<td>session-save-redis-persistent-id</td> |
| 71 | +<td>persistent_identifier</td> |
| 72 | +<td> |
| 73 | +<p>Unique string to enable persistent connections (for example, <code>sess-db0</code>).</p> |
| 74 | +<p><a href="https://github.com/nicolasff/phpredis/issues/70" target="_blank">Known issues with <code>phpredis</code> and <code>php-fpm</code></a>.</p> |
| 75 | +</td> |
| 76 | +<td>empty</td> |
| 77 | +</tr> |
| 78 | +<tr> |
| 79 | +<td>session-save-redis-db</td> |
| 80 | +<td>database</td> |
| 81 | +<td> |
| 82 | +<p>Unique Redis database number, which is recommended to protect against data loss.</p> |
| 83 | +<p><strong>Important</strong>: If you use Redis for more than one type of caching, the database numbers must be different. It is recommended that you assign the default caching database number to 0, the page caching database number to 1, and the session storage database number to 2. </p> |
| 84 | +</td> |
| 85 | +<td>0</td> |
| 86 | +</tr> |
| 87 | +<tr> |
| 88 | +<td>session-save-redis-compression-threshold</td> |
| 89 | +<td>compression_threshold</td> |
| 90 | +<td> |
| 91 | +<p>Set to <code>0</code> to disable compression (recommended when <a href="http://suhosin.org/stories/howtos.html#encryption-features" target="_blank"><code>suhosin.session.encrypt = On</code></a>).</p> |
| 92 | +<p><a href="https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/issues/18" target="_blank">Known issue with strings of more than 64KB</a>.</p> |
| 93 | +</td> |
| 94 | +<td>2048</td> |
| 95 | +</tr> |
| 96 | +<tr> |
| 97 | +<td>session-save-redis-compression-lib</td> |
| 98 | +<td>compression_library</td> |
| 99 | +<td>Options: <code>gzip</code>, <code>lzf</code>, <code>lz4</code> or <code>snappy</code>.</td> |
| 100 | +<td>gzip</td> |
| 101 | +</tr> |
| 102 | +<tr> |
| 103 | +<td>session-save-redis-log-level</td> |
| 104 | +<td>log_level</td> |
| 105 | +<td> |
| 106 | +<p>Set to any of the following, listed in order from least verbose to most verbose:</p> |
| 107 | +<ul> |
| 108 | +<li><code>0</code> (emergency: only the most severe errors)</li> |
| 109 | +<li><code>1</code> (alert: immediate action required)</li> |
| 110 | +<li><code>2</code> (critical: application component unavailable)</li> |
| 111 | +<li><code>3</code> (error: runtime errors, not critical but must be monitored)</li> |
| 112 | +<li><code>4</code> (warning: additional information, recommended)</li> |
| 113 | +<li><code>5</code> (notice: normal but significant condition)</li> |
| 114 | +<li><code>6</code> (info: informational messages)</li> |
| 115 | +<li><code>7</code> (debug: the most information for development or testing only)</li> |
| 116 | +</ul> |
| 117 | +</td> |
| 118 | +<td>1</td> |
| 119 | +</tr> |
| 120 | +<tr> |
| 121 | +<td>session-save-redis-max-concurrency</td> |
| 122 | +<td>max_concurrency</td> |
| 123 | +<td> |
| 124 | +<p>Maximum number of processes that can wait for a lock on one session. For large production clusters, set this to at least 10% of the number of PHP processes.</p> |
| 125 | +</td> |
| 126 | +<td>6</td> |
| 127 | +</tr> |
| 128 | +<tr> |
| 129 | +<td>session-save-redis-break-after-frontend</td> |
| 130 | +<td>break_after_frontend</td> |
| 131 | +<td> |
| 132 | +<p>Number of seconds to wait before trying to break the lock for frontend (that is, storefront) session.</p> |
| 133 | +</td> |
| 134 | +<td>5</td> |
| 135 | +</tr> |
| 136 | +<tr> |
| 137 | +<td>session-save-redis-break-after-adminhtml</td> |
| 138 | +<td>break_after_adminhtml</td> |
| 139 | +<td> |
| 140 | +<p>Number of seconds to wait before trying to break the lock for an adminhtml (that is, Magento Admin) session.</p> |
| 141 | +</td> |
| 142 | +<td>30</td> |
| 143 | +</tr> |
| 144 | +<tr> |
| 145 | +<td>session-save-redis-first-lifetime</td> |
| 146 | +<td>first_lifetime</td> |
| 147 | +<td> |
| 148 | +<p>Lifetime, in seconds, of session for non-bots on the first write, or use <code>0</code> to disable.</p> |
| 149 | +</td> |
| 150 | +<td>600</td> |
| 151 | +</tr> |
| 152 | +<tr> |
| 153 | +<td>session-save-redis-bot-first-lifetime</td> |
| 154 | +<td>bot_first_lifetime</td> |
| 155 | +<td> |
| 156 | +<p>Lifetime, in seconds, of session for bots on the first write, or use <code>0</code> to disable.</p> |
| 157 | +</td> |
| 158 | +<td>60</td> |
| 159 | +</tr> |
| 160 | +<tr> |
| 161 | +<td>session-save-redis-bot-lifetime</td> |
| 162 | +<td>bot_lifetime</td> |
| 163 | +<td> |
| 164 | +<p>Lifetime, in seconds, of session for bots on subsequent writes, or use <code>0</code> to disable.</p> |
| 165 | +</td> |
| 166 | +<td>7200</td> |
| 167 | +</tr> |
| 168 | +<tr> |
| 169 | +<td>session-save-redis-disable-locking</td> |
| 170 | +<td>disable_locking</td> |
| 171 | +<td> |
| 172 | +<p>Disable session locking entirely.</p> |
| 173 | +</td> |
| 174 | +<td>0 (false)</td> |
| 175 | +</tr> |
| 176 | +<tr> |
| 177 | +<td>session-save-redis-min-lifetime</td> |
| 178 | +<td>min_lifetime</td> |
| 179 | +<td> |
| 180 | +<p>Minimum session lifetime, in seconds.</p> |
| 181 | +</td> |
| 182 | +<td>60</td> |
| 183 | +</tr> |
| 184 | +<tr> |
| 185 | +<td>session-save-redis-max-lifetime</td> |
| 186 | +<td>max_lifetime</td> |
| 187 | +<td> |
| 188 | +<p>Maximum session lifetime, in seconds.</p> |
| 189 | +</td> |
| 190 | +<td>2592000 (720 hours)</td> |
| 191 | +</tr> |
| 192 | +</tbody> |
| 193 | +</table> |
| 194 | + |
| 195 | +### Example command |
| 196 | + |
| 197 | +The following example sets Redis as the session data store, sets the host to `redis.example.com`, sets the log level to 3, and sets the datbase number to 2. All other parameters are set to the default value. |
| 198 | + |
| 199 | +```bash |
| 200 | +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 |
| 201 | +``` |
| 202 | + |
| 203 | +### Result |
| 204 | + |
| 205 | +Magento adds lines similar to the following to `<your Magento install dir>app/etc/env.php`: |
| 206 | + |
| 207 | +``` php?start_inline=1 |
| 208 | + 'session' => |
| 209 | + array ( |
| 210 | + 'save' => 'redis', |
| 211 | + 'redis' => |
| 212 | + array ( |
| 213 | + 'host' => 'redis.example.com', |
| 214 | + 'port' => '6379', |
| 215 | + 'password' => '', |
| 216 | + 'timeout' => '2.5', |
| 217 | + 'persistent_identifier' => '', |
| 218 | + 'database' => '2', |
| 219 | + 'compression_threshold' => '2048', |
| 220 | + 'compression_library' => 'gzip', |
| 221 | + 'log_level' => '3', |
| 222 | + 'max_concurrency' => '6', |
| 223 | + 'break_after_frontend' => '5', |
| 224 | + 'break_after_adminhtml' => '30', |
| 225 | + 'first_lifetime' => '600', |
| 226 | + 'bot_first_lifetime' => '60', |
| 227 | + 'bot_lifetime' => '7200', |
| 228 | + 'disable_locking' => '0', |
| 229 | + 'min_lifetime' => '60', |
| 230 | + 'max_lifetime' => '2592000' |
| 231 | + ) |
| 232 | + ), |
| 233 | +``` |
| 234 | + |
| 235 | +## Basic verification {#redis-verify} |
| 236 | + |
| 237 | +{% include config/redis-verify.md %} |
| 238 | + |
| 239 | +#### Related topics |
| 240 | + |
| 241 | + * [Create or extend configuration types]({{page.baseurl}}config-guide/config/config-create.html) |
| 242 | + * [Magento's deployment configuration]({{page.baseurl}}config-guide/config/config-php.html) |
0 commit comments