Magento 2 通过配置Redis加速网站

季宏言 |

Redis是一种缓存技术,允许WEB应用程序快速存储数据并从内存中回收。Redis是分布式缓存(很像MEMCACHED),但提供了更多的底层功能。安装Redis让你的网站大幅度提升性能。
废话不多说,直接安装Redis如下,安装环境Centos 8

1. 安装

yum install redis

2. 编辑配置文件,如果同一台服务器,可保持默认

vim /etc/redis.conf

#requirepass那行并打开注释,在后面写自己的密码,如下 requirepass yourpassword 将bind 后 127.0.0.1 改为 内网IP # 为安全起见,请勿使用 0.0.0.0 protected-mode yes 改为 protected-mode no # 关闭保护模式 daemonize no 改为 daemonize yes # 开启守护进程 3. 启动

systemctl start redis

//重启

systemctl stop redis

4. 开启自启动

systemctl enable redis

5. 开放防火墙端口(如有需要)

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=6379/tcp --permanent

//重启防火墙

systemctl restart firewalld

**这里也提供下Ubuntu的安装Redis方式**

1. 安装

sudo apt-get install redis-server

2. 查看redis状态

ps -aux|grep redis

输出结果如下:
redis     7207  0.0  0.1  40112  1712 ?        Ssl  09:56   0:00 /usr/bin/redis-server 127.0.0.1:6379
root      7221  0.0  0.0  14376   988 pts/0    S+   09:57   0:00 grep –color=auto redis
3. 根据启动命令检查服务状态

netstat -nlt|grep 6379

4. 检查Redis的状态

sudo /etc/init.d/redis-server status

5. 修改配置(如需)

sudo vi /etc/redis/redis.conf

 

******配置redis******

cd /www/wwwroot/magento-root/app/etc/

cp env.php noredis.env.php   #默认的配置文件备份

cd /www/wwwroot/magento #退到文件根目录

*配置redis,在网站根目录下执行以下命令

**配置默认缓存

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0

 

**配置页面缓存

bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1

 

查看是否配置成功

redis-cli monitor

 

如果配置成功,浏览器访问任意页面SSH会输出类似下面的结果

 


 

  1476826133.810090 [0 127.0.0.1:52366] "select" "1" 1476826133.816293 [0 127.0.0.1:52367] "select" "0" 1476826133.817461 [0 127.0.0.1:52367] "hget" "zc:k:ea6_GLOBAL__DICONFIG" "d" 1476826133.829666 [0 127.0.0.1:52367] "hget" "zc:k:ea6_DICONFIG049005964B465901F774DB9751971818" "d" 1476826133.837854 [0 127.0.0.1:52367] "hget" "zc:k:ea6_INTERCEPTION" "d" 1476826133.868374 [0 127.0.0.1:52368] "select" "1" 1476826133.869011 [0 127.0.0.1:52369] "select" "0" 1476826133.869601 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_DEFAULT__10__235__32__1080MAGENTO2" "d" 1476826133.872317 [0 127.0.0.1:52369] "hget" "zc:k:ea6_INITIAL_CONFIG" "d" 1476826133.879267 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL_PRIMARY_PLUGIN_LIST" "d" 1476826133.883312 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL__EVENT_CONFIG_CACHE" "d" 1476826133.898431 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DB_PDO_MYSQL_DDL_STAGING_UPDATE_1" "d" 1476826133.898794 [0 127.0.0.1:52369] "hget" "zc:k:ea6_RESOLVED_STORES_D1BEFA03C79CA0B84ECC488DEA96BC68" "d" 1476826133.905738 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_STORE_DEFAULT_10__235__32__1080MAGENTO2" "d" ... more ... 1476826210.634998 [0 127.0.0.1:52439] "hmset" "zc:k:ea6_MVIEW_CONFIG" "d" "a:18:{s:19:\"design_config_dummy\";a:4:{s:7:\"view_id\";s:19:\"design_config_dummy\";s:12:\"action_class\";s:39:\"Magento\\Theme\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:14:\"customer_dummy\";a:4:{s:7:\"view_id\";s:14:\"customer_dummy\";s:12:\"action_class\";s:42:\"Magento\\Customer\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:13:\"cms_page_grid\";a:4:{s:7:\"view_id\";s:13:\"cms_page_grid\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:1:{s:8:\"cms_page\";a:3:{s:4:\"name\";s:8:\"cms_page\";s:6:\"column\";s:7:\"page_id\";s:18:\"subscription_model\";N;}}}s:21:\"catalog_category_flat\";a:4:{s:7:\"view_id\";s:21:\"catalog_category_flat\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:6:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";N;}s:31:\"catalog_category_entity_decimal\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_decimal\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:27:\"catalog_category_entity_int\";a:3:{s:4:\"name\";s:27:\"catalog_category_entity_int\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:28:\"catalog_category_entity_text\";a:3:{s:4:\"name\";s:28:\"catalog_category_entity_text\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:31:\"catalog_category_entity_varchar\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_varchar\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:32:\"catalog_category_entity_datetime\";a:3:{s:4:\"name\";s:32:\"catalog_category_entity_datetime\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}}}s:24:\"catalog_category_product\";a:4:{s:7:\"view_id\";s:24:\"catalog_category_product\";s:12:\"action_class\";s:46:\"Magento\\Catalog\\Model\\Indexer\\Category\\Product\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:2:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\" ... more ...------------------------------------


清除缓存:

redis-cli flushall
bin/magento cache:flush
bin/magento cache:clean

 

网站如需迁移,可执行下面的操作方便迁移

cp env.php redis.env.php  #网站迁移前将配置过redis的配置文件备份
cp noredis.env.php env.php #把之前默认的配置文件恢复,然后清除缓存

 

本文参考连接:https://devdocs.magento.com/guides/v2.4/config-guide/redis/redis-pg-cache.html

添加新评论

此字段内容将保密,不会被其他人看见。