如何在Magento1&2中配置Redis

redmaomail 2024-07-24 18:16 阅读数 58 #Magento

红帽云邮外贸主机

首先,Magento 1和2中的Redis缓存配置有几个先决条件:

  • Redis服务
  • PHP Redis扩展

注意:建议使用最新版本。

步骤1:更新并安装Redis-server

wget http://download.redis.io/releases/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make
make test
sudo make install
cd utils
sudo ./install_server.sh
sudo service redis_6379 start
sudo update-rc.d redis_6379 defaults

第2步:在Magento中配置Redis

A.配置Magento 2以将Redis用作缓存存储

将以下代码添加到您的应用程序app/etc/env.php:

<?php
// app/etc/env.php
return [
    // Other directives
    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '127.0.0.1',
                    'database' => '0',
                    'port' => '6379',
                    'password' => ''
                ]
            ],
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '127.0.0.1',
                    'database' => '1',
                    'port' => '6379',
                    'compress_data' => '0',
                    'password' => ''
                ]
            ]
        ]
    ]
];

B.配置Magento 2以将Redis用作会话存储

首先,您需要找到以下代码段:

'session' => [
    'save' => 'files'
],
And replace it with snippet as follows:
'session' => [
    'save' => 'redis',
    'redis' => [
        'host' => '127.0.0.1',
        'port' => '6379',
        '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'
    ] ],

其次,通过运行以下命令清除所有缓存会话:

bin/magento c:f

C.配置Magento 1使用Redis

首先,转到您的app/etc/local.xml,然后改变:

<?xml version="1.0"?>
<config>
    <global>
        <install>
            <date><![CDATA[Tue, 04 Oct 2016 09:53:37 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[e972f3c4e8436052de805bdf1f40de0f]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[magento]]></username>
                    <password><![CDATA[magento]]></password>
                    <dbname><![CDATA[magento]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

为:


<?xml version="1.0"?>
<config>
    <global>
        // Other directives
        <!-- This is a child node of config/global -->
        <cache>
            <backend>Cm_Cache_Backend_Redis</backend>
            <backend_options>
                <server>127.0.0.1</server> <!-- or absolute path to unix socket -->
                <port>6379</port>                
                <database>0</database> <!-- Redis database number; protection against accidental data loss is improved by not sharing databases -->
                <password></password> <!-- Specify if your server requires authentication -->
            </backend_options>
        </cache>
        <!--session_save><![CDATA[files]]></session_save-->
        <session_save>db</session_save>
        <redis_session>                       <!-- All options seen here are the defaults -->
            <host>127.0.0.1</host>            <!-- Specify an absolute path if using a unix socket -->
            <port>6379</port>
            <password></password>             <!-- Specify if your server requires authentication -->
            <timeout>2.5</timeout>            <!-- This is the connection timeout, not the locking timeout -->
            <db>0</db>                        <!-- Redis database number; protection from accidental loss is improved by using a unique DB number for sessions -->
            <compression_threshold>2048 </compression_threshold>  <!-- Set to 0 to disable compression (recommended when suhosin.session.encrypt=on); known bug with strings over 64k: https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/issues/18 -->
            <compression_lib>gzip</compression_lib>              <!-- gzip, lzf, lz4 or snappy -->
            <log_level>1</log_level>               <!-- 0 (emergency: system is unusable), 4 (warning; additional information, recommended), 5 (notice: normal but significant condition), 6 (info: informational messages), 7 (debug: the most information for development/testing) -->
            <max_concurrency>6</max_concurrency>                 <!-- 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 -->
            <break_after_frontend>5</break_after_frontend>       <!-- seconds to wait for a session lock in the frontend; not as critical as admin -->
            <fail_after>10</fail_after>                       <!-- seconds after which we bail from attempting to obtain lock (in addition to break after time) -->
            <break_after_adminhtml>30</break_after_adminhtml>
            <first_lifetime>600</first_lifetime>                 <!-- Lifetime of session for non-bots on the first write. 0 to disable -->
            <bot_first_lifetime>60</bot_first_lifetime>          <!-- Lifetime of session for bots on the first write. 0 to disable -->
            <bot_lifetime>7200</bot_lifetime>                    <!-- Lifetime of session for bots on subsequent writes. 0 to disable -->
            <disable_locking>0</disable_locking>                 <!-- Disable session locking entirely. -->
            <min_lifetime>60</min_lifetime>                      <!-- Set the minimum session lifetime -->
            <max_lifetime>2592000</max_lifetime>                 <!-- Set the maximum session lifetime -->
        </redis_session>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

其次,通过运行以下命令清除会话的所有缓存:

rm -rf /var/www/html/magento/var/session/*
rm -rf /var/www/html/magento/var/cache/*

 

第3步:重新启动并检查结果

接下来,您应该重新启动:

service redis-server restart

要检查Redis服务器是否正常工作,请输入:

redis-cli ping

如果结果是:

PONG

然后你的服务器正在响应。
此外,您可以通过以下方式监控所有流量:

redis-cli monitor

然后刷新页面。如果您可以在终端上看到生成的日志,有随机的字母数字字符,这意味着缓存正在运行。
您还可以检查 Redis-server是否能够设置密钥:

redis-cli
127.0.0.1:6379> set mykey KEY
OK
127.0.0.1:6379> get mykey
"KEY"
127.0.0.1:6379> exit

此外,您可以使用“info”命令获取服务器信息和统计信息:

redis-cli info

我们已经向您展示了在Magento 1和2中配置Redis的一些简单步骤。如果您有任何需要请联系我们。


红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机