Bitnami Magento2 安装Redis并且配置使用Redis

1.安装Redis

为了获得最新版本的Redis,我们将使用apt从Debian官方仓库中安装它。
apt通过键入以下内容来更新本地程序包缓存并安装Redis:

  1. sudo apt update
  2. sudo apt install redis-server

这将下载并安装Redis及其依赖项。然后,在Redis配置文件中进行一项重要的配置更改,该更改是在安装过程中自动生成的。
使用首选的文本编辑器打开此文件:

  1. sudo nano /etc/redis/redis.conf

在文件内部,找到supervised指令。该指令允许您声明一个初始化系统来将Redis作为服务进行管理,从而为您提供对其操作的更多控制。该supervised指令no默认设置为。由于您正在运行使用systemd初始化系统的Debian,请将其更改为systemd:
Tony's blog image
这是您目前唯一需要对Redis配置文件进行的更改,因此请在完成后保存并关闭它。然后,重新加载Redis服务文件以反映您对配置文件所做的更改:

  1. sudo systemctl restart redis

这样,您已经安装并配置了Redis,并且它在您的计算机上运行。不过,在开始使用它之前,先检查一下Redis是否运行正常是明智的。

2.测试Redis

与任何新安装的软件一样,在对其配置进行任何进一步更改之前,最好确保Redis能够按预期运行。我们将介绍几种方法来检查Redis在此步骤中是否正常工作。

  1. 首先检查Redis服务是否正在运行:
    1. sudo systemctl status redis
    如果正在运行而没有任何错误,则此命令将产生类似于以下内容的输出:
    Tony's blog image

在这里,您可以看到Redis正在运行并且已经启用,这意味着它被设置为在每次服务器启动时启动。
注意:对于Redis的许多常见用例,此设置是理想的。但是,如果您希望每次服务器启动时都手动启动Redis,则可以使用以下命令进行配置:

  1. sudo systemctl disable redis

要测试Redis是否正常运行,请使用命令行客户端连接到服务器:

  1. redis-cli

在随后的提示中,使用以下ping命令测试连接性:
Tony's blog image

此输出确认服务器连接仍然有效。接下来,通过运行以下命令检查您是否可以设置密钥:

  1. set test "It's working!"

通过键入以下内容来检索值:

  1. get test

在确认您可以获取该值之后,退出Redis提示符以返回到Shell:

  1. exit

作为最后的测试,我们将检查Redis即使在停止或重新启动之后也能够持久保存数据。为此,请首先重新启动Redis实例:

  1. sudo systemctl restart redis

然后再次与命令行客户端连接,并确认您的测试值仍然可用,完成后再次退出外壳:

  1. redis-cli
  2. get test
  3. exit

Tony's blog image

这样,您的Redis安装就可以完全运行并可以使用了。但是,其某些默认配置设置是不安全的,并为恶意行为者提供了攻击和访问服务器及其数据的机会。本教程的其余步骤介绍了Redis官方网站所规定的缓解这些漏洞的方法。尽管这些步骤是可选的,并且如果您选择不遵循这些步骤,则Redis仍将起作用,但强烈建议您完成这些步骤,以增强系统的安全性。

3.绑定到本地主机

默认情况下,只能从localhost访问Redis 。但是,如果您通过遵循与本教程不同的教程来安装和配置Redis,则可能已更新了配置文件以允许从任何地方进行连接。这不如绑定到localhost安全。
要更正此问题,请打开Redis配置文件进行编辑:

  1. sudo nano /etc/redis/redis.conf

找到此行,并确保其未注释(删除,#如果存在):
Tony's blog image
然后,重新启动服务以确保systemd读取您的更改:

  1. sudo systemctl restart redis

要检查此更改是否已生效,请运行以下netstat命令:

  1. sudo netstat -lnp | grep redis

Tony's blog image
此输出显示该redis-server程序已绑定到localhost(127.0.0.1),反映了您刚刚对配置文件进行的更改。如果您在该列中看到另一个IP地址(0.0.0.0例如,),则应再次检查是否取消注释正确的行,然后再次重新启动Redis服务。
现在,您的Redis安装仅在localhost上进行侦听,对于恶意参与者而言,发出请求或访问您的服务器将更加困难。但是,Redis当前未设置为要求用户在更改其配置或保存的数据之前进行身份验证。为了解决这个问题,Redis允许您要求用户通过密码进行身份验证,然后才能通过Redis客户端(redis-cli)进行更改。
要将Magento安装配置为使用Redis进行会话存储,请打开app/etc/env.php文件并更改/添加以下内容:

  1. 'session' =>
  2. array (
  3. 'save' => 'files',
  4. ),

改成

  1. 'session' =>
  2. array (
  3. 'save' => 'redis',
  4. 'redis' =>
  5. array (
  6. 'host' => '127.0.0.1',
  7. 'port' => '6379',
  8. 'password' => '',
  9. 'timeout' => '2.5',
  10. 'persistent_identifier' => '',
  11. 'database' => '0',
  12. 'compression_threshold' => '2048',
  13. 'compression_library' => 'gzip',
  14. 'log_level' => '1',
  15. 'max_concurrency' => '6',
  16. 'break_after_frontend' => '5',
  17. 'break_after_adminhtml' => '30',
  18. 'first_lifetime' => '600',
  19. 'bot_first_lifetime' => '60',
  20. 'bot_lifetime' => '7200',
  21. 'disable_locking' => '0',
  22. 'min_lifetime' => '60',
  23. 'max_lifetime' => '2592000'
  24. )
  25. ),

设置成生产模式

  1. 'MAGE_MODE' => 'production',

并添加使用Redis作为页面缓存:

  1. 'cache' => [
  2. 'frontend' => [
  3. 'default' => [
  4. 'id_prefix' => 'm2_',
  5. 'backend' => 'Cm_Cache_Backend_Redis',
  6. 'backend_options' => [
  7. 'server' => '127.0.0.1',
  8. 'port' => '6379',
  9. 'persistent' => '',
  10. 'database' => 1,
  11. 'force_standalone' => 0,
  12. 'connect_retries' => 2,
  13. 'read_timeout' => 10,
  14. 'automatic_cleaning_factor' => 0,
  15. 'compress_tags' => 1,
  16. 'compress_data' => 1,
  17. 'compress_threshold' => 20480,
  18. 'compression_lib' => 'gzip'
  19. ]
  20. ],
  21. 'page_cache' => [
  22. 'id_prefix' => 'm2_',
  23. 'backend' => 'Cm_Cache_Backend_Redis',
  24. 'backend_options' => [
  25. 'server' => '127.0.0.1',
  26. 'port' => '6379',
  27. 'persistent' => '',
  28. 'database' => 2,
  29. 'force_standalone' => 0,
  30. 'connect_retries' => 2,
  31. 'read_timeout' => 10,
  32. 'automatic_cleaning_factor' => 0,
  33. 'compress_tags' => 1,
  34. 'compress_data' => 1,
  35. 'compress_threshold' => 20480,
  36. 'compression_lib' => 'gzip'
  37. ]
  38. ],
  39. ]
  40. ]

最后再次刷新缓存:

  1. sudo php bin/magento cache:flush

示例env.php配置代码

  1. <?php
  2. return array (
  3. 'backend' =>
  4. array (
  5. 'frontName' => 'admin',
  6. ),
  7. 'crypt' =>
  8. array (
  9. 'key' => 'key密钥',
  10. ),
  11. 'session' =>
  12. array (
  13. 'save' => 'redis',
  14. 'redis' =>
  15. array (
  16. 'host' => '127.0.0.1',
  17. 'port' => '6379',
  18. 'password' => '',
  19. 'timeout' => '2.5',
  20. 'persistent_identifier' => '',
  21. 'database' => '0',
  22. 'compression_threshold' => '2048',
  23. 'compression_library' => 'gzip',
  24. 'log_level' => '1',
  25. 'max_concurrency' => '6',
  26. 'break_after_frontend' => '5',
  27. 'break_after_adminhtml' => '30',
  28. 'first_lifetime' => '600',
  29. 'bot_first_lifetime' => '60',
  30. 'bot_lifetime' => '7200',
  31. 'disable_locking' => '0',
  32. 'min_lifetime' => '60',
  33. 'max_lifetime' => '2592000',
  34. ),
  35. ),
  36. 'db' =>
  37. array (
  38. 'table_prefix' => '',
  39. 'connection' =>
  40. array (
  41. 'default' =>
  42. array (
  43. 'host' => '数据库地址',
  44. 'dbname' => '数据库名称',
  45. 'username' => '账户',
  46. 'password' => '密码',
  47. 'model' => 'mysql4',
  48. 'engine' => 'innodb',
  49. 'initStatements' => 'SET NAMES utf8;',
  50. 'active' => '1',
  51. ),
  52. ),
  53. ),
  54. 'resource' =>
  55. array (
  56. 'default_setup' =>
  57. array (
  58. 'connection' => 'default',
  59. ),
  60. ),
  61. 'x-frame-options' => 'SAMEORIGIN',
  62. 'MAGE_MODE' => 'developer',
  63. 'cache_types' =>
  64. array (
  65. 'config' => 0,
  66. 'layout' => 0,
  67. 'block_html' => 0,
  68. 'collections' => 0,
  69. 'reflection' => 0,
  70. 'db_ddl' => 0,
  71. 'eav' => 0,
  72. 'customer_notification' => 0,
  73. 'full_page' => 0,
  74. 'config_integration' => 0,
  75. 'config_integration_api' => 0,
  76. 'translate' => 0,
  77. 'config_webservice' => 0,
  78. 'compiled_config' => 1,
  79. 'cart2quote_license' => 0,
  80. ),
  81. 'install' =>
  82. array (
  83. 'date' => 'Thu, 01 Jun 2017 08:25:45 +0000',
  84. ),
  85. 'http_cache_hosts' =>
  86. array (
  87. 0 =>
  88. array (
  89. 'host' => '127.0.0.1',
  90. 'port' => '80',
  91. ),
  92. ),
  93. 'system' =>
  94. array (
  95. 'default' =>
  96. array (
  97. 'dev' =>
  98. array (
  99. 'debug' =>
  100. array (
  101. 'debug_logging' => '0',
  102. ),
  103. ),
  104. 'smile_elasticsuite_core_base_settings' =>
  105. array (
  106. 'es_client' =>
  107. array (
  108. 'servers' => 'localhost:9200',
  109. 'enable_https_mode' => '0',
  110. 'enable_http_auth' => '0',
  111. 'http_auth_user' => '',
  112. 'http_auth_pwd' => '',
  113. ),
  114. ),
  115. ),
  116. ),
  117. 'cache' =>
  118. array (
  119. 'frontend' =>
  120. array (
  121. 'default' =>
  122. array (
  123. 'id_prefix' => 'm2_',
  124. 'backend' => 'Cm_Cache_Backend_Redis',
  125. 'backend_options' =>
  126. array (
  127. 'server' => '127.0.0.1',
  128. 'port' => '6379',
  129. 'persistent' => '',
  130. 'database' => 1,
  131. 'force_standalone' => 0,
  132. 'connect_retries' => 2,
  133. 'read_timeout' => 10,
  134. 'automatic_cleaning_factor' => 0,
  135. 'compress_tags' => 1,
  136. 'compress_data' => 1,
  137. 'compress_threshold' => 20480,
  138. 'compression_lib' => 'gzip',
  139. ),
  140. ),
  141. 'page_cache' =>
  142. array (
  143. 'id_prefix' => 'm2_',
  144. 'backend' => 'Cm_Cache_Backend_Redis',
  145. 'backend_options' =>
  146. array (
  147. 'server' => '127.0.0.1',
  148. 'port' => '6379',
  149. 'persistent' => '',
  150. 'database' => 2,
  151. 'force_standalone' => 0,
  152. 'connect_retries' => 2,
  153. 'read_timeout' => 10,
  154. 'automatic_cleaning_factor' => 0,
  155. 'compress_tags' => 1,
  156. 'compress_data' => 1,
  157. 'compress_threshold' => 20480,
  158. 'compression_lib' => 'gzip',
  159. ),
  160. ),
  161. ),
  162. ),
  163. );

4.测试

如果有问题的话,
先清除redis缓存:
1,命令行里执行:

  1. redis-cli

就进入了 redis 控制台,
2,然后执行:

  1. FLUSHALL

就是清空全部缓存,如果显示 OK 就是成功了,
3,再执行:

  1. exit

来退出。
再更新下文件:

  1. php bin/magento maintenance:enable
  2. rm -rf var/di/* && rm -rf var/generation/* && rm -rf var/cache/* && rm -rf var/page_cache/* && rm -rf var/view_preprocessed/* && rm -rf pub/static/* && rm -rf generated/* && mkdir var/di
  3. php bin/magento setup:upgrade && php bin/magento setup:di:compile
  4. php bin/magento setup:static-content:deploy -f && php bin/magento indexer:reindex && php bin/magento maintenance:disable && php bin/magento cache:clean && php bin/magento cache:flush

浏览器打开M2网站,
然后在命令行里输入

  1. redis-cli monitor

会看到有终端有即时更新消息,说明配置成功了。
Tony's blog image