Nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分(redis主从)
2.3 Redis 主从搭建
2.3.1 redis安装配置
[root@localhost redis-2.8.13]# yum install gcc gcc-c++ -y
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget -c http://download.redis.io/releases/redis-2.8.13.tar.gz
注:安装gcc gcc-c++,下载redis源码包
[root@localhost src]# tar -zxf redis-2.8.13.tar.gz
[root@localhost src]# cd redis-2.8.13
[root@localhost redis-2.8.13]# make
[root@localhost redis-2.8.13]# cd src
[root@localhost src]# make install PREFIX=/usr/local/redis
注:解压redis,并编译安装到指定目录
[root@localhost redis-2.8.13]# cp redis.conf /usr/local/redis/
注:将redis配置文件复制到安装目录下,由于redis安装不会自动生成配置文件,故此操作在源码解压目录中操作
[root@localhost redis-2.8.13]# echo 'export PATH=/usr/local/redis/bin:$PATH' >> /etc/profile
[root@localhost redis-2.8.13]# source /etc/profile
注:把redis下的bin目录加入环境变量profile末尾,然后执行命令使其生效。
[root@localhost redis]# sed -i 's/daemonize no/daemonize yes/g' redis.conf
注:由于默认redis启动不是后端启动,所以可以修改redis配置文件,把此处的no改为yes即可
[root@localhost redis]# ./bin/redis-server ./redis.conf
[root@localhost redis]# ps aux | grep redis
root 4490 0.1 0.4 137372 2324 pts/0 Sl 05:26 0:00 /usr/local/redis/bin/redis-server *:6379
root 4500 0.0 0.1 103320 884 pts/0 S+ 05:32 0:00 grep redis
[root@localhost redis]# netstat -anlp | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 4490/redis-server *
tcp 0 0 :::6379 :::* LISTEN 4490/redis-server *
[root@localhost redis]#
注:执行./bin/redis-server ./redis.conf 启动redis,虽然无任何提示,但可以使用ps命令查看redis进程以及使用netstat命令查看redis端口号
停止redis
redis-cli shutdown
关闭所有
redis-cli -p 6397 shutdown
关闭6397端口的redis
注:关闭以后缓存数据会自动转储到硬盘上,硬盘地址可以查看redis.conf配置文件中的dbfilename dump.rdb
2.3.2 Redis主从配置
Redis主从配置非常简单,而且redis支持多重slave,所以可以无限延伸下去
[root@localhost ~]# sed -i '/port 6379/aslaveof 192.168.8.135 6379' /usr/local/redis/redis.conf
注:只需要在slave上面的redis主配置文件中port 6379 行下面加上slaveof 192.168.8.135 6379即可
[root@localhost ~]# pkill redis
[root@localhost ~]# /usr/local/redis/bin/redis-server ./redis.conf
[root@localhost ~]# redis-cli
127.0.0.1:6379> info
注:重启redis,并执行info命令,如果显示如下图所示,表示slave配置完成
2.3.3 安装PHP-redis连接驱动
注:驱动需要和php安装在一起
所以一下配置在LAP上面进行
驱动作用:要确保PHP能够连接Redis缓存服务器,需添加PHP Redis扩展程序,即添加PHP扩展模块
[root@localhost src]# wget -c https://github.com/phpredis/phpredis/archive/3.1.2.tar.gz
[root@localhost src]# tar zxf 3.1.2.tar.gz
[root@localhost src]# cd phpredis-3.1.2/
[root@localhost phpredis-3.1.2]# /usr/local/php5/bin/phpize
[root@localhost phpredis-3.1.2]# ./configure --with-php-config=/usr/local/php5/bin/php-config --enable-redis
[root@localhost phpredis-3.1.2]# make -j8 && make install
注:下载驱动并编译安装,由于驱动本身解压后是没有configure执行文件的,所以使用phpize命令创建
如果出现以下报错请执行yum install m4 autoconf -y
记录下图中部分/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626/
[root@localhost phpredis-3.1.2]# cat >> /usr/local/php5/lib/php.ini << EOF
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626/"
extension=redis.so
EOF
注:在php.ini最后追加以上内容
[root@localhost phpredis-3.1.2]# /usr/local/apache2/bin/apachectl restart
注:重启apache,然后刷新网页出现如下redis信息,即集成完成!
2.4 发布Discuz论坛
[root@localhost src]# wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip
root@localhost src]# unzip Discuz_X3.2_SC_UTF8.zip
[root@localhost src]# cp -a readme/ upload/ utility/ /usr/local/apache2/htdocs/
[root@localhost htdocs]# cp -a ./upload/* .
[root@localhost htdocs]# ../bin/apachectl restart
注:下载discuz软件,并解压复制到apache发布目录,并拷贝upload里的文件到发布根目录
访问http://192.168.8.135/install/进行安装
提示不可写,直接在发布目录执行如下命令
[root@localhost htdocs]# chmod -R o+w .*
此处省略discuz的安装过程,出现以下界面表示discuz安装完成
[root@localhost config]# vim config_global.php
注:修改config_global.php ,此文件在发布目录config下,修改为如下图所示
第二台LAP无需安装discuz,直接从第一台LAP上把发布目录拷贝过来即可,或者更方便的做法是部署一台NFS服务器,两台LAP共享发布目录即可
[root@localhost htdocs]# scp -r .* root@192.168.8.136:/usr/local/apache2/htdocs/
2.5 测试redis缓存discuz以及redis主从
[root@localhost config]# redis-cli
127.0.0.1:6379> KEYS *
(empty list or set)
127.0.0.1:6379> KEYS *
1) "NgoypY_diytemplatenamehome"
2) "NgoypY_usergroups"
3) "NgoypY_onlinerecord"
4) "NgoypY_announcements"
5) "NgoypY_heats"
6) "NgoypY_pluginlanguage_system"
7) "NgoypY_userstats"
8) "NgoypY_usergroup_7"
9) "NgoypY_forum_index_page_7"
10) "NgoypY_pluginlanguage_script"
11) "NgoypY_style_default"
12) "NgoypY_userapp"
13) "NgoypY_historyposts"
14) "NgoypY_setting"
15) "NgoypY_plugin"
16) "NgoypY_cronnextrun"
17) "NgoypY_diytemplatenameforum"
18) "NgoypY_ipbanned"
19) "NgoypY_diytemplatename"
20) "NgoypY_magic"
21) "NgoypY_forumlinks"
22) "NgoypY_onlinelist"
127.0.0.1:6379>
注:在discuz论坛上面随便点击内容,然后在redis master上面执行以上命令,可以看到缓存的数据
[root@localhost ~]# redis-cli
127.0.0.1:6379> KEYS *
1) "NgoypY_magic"
2) "NgoypY_heats"
3) "NgoypY_diytemplatenamehome"
4) "NgoypY_historyposts"
5) "NgoypY_userapp"
6) "NgoypY_pluginlanguage_system"
7) "NgoypY_style_default"
8) "NgoypY_usergroup_7"
9) "NgoypY_forumlinks"
10) "NgoypY_plugin"
11) "NgoypY_usergroups"
12) "NgoypY_setting"
13) "NgoypY_onlinerecord"
14) "NgoypY_diytemplatenameforum"
15) "NgoypY_diytemplatename"
16) "NgoypY_userstats"
17) "NgoypY_cronnextrun"
18) "NgoypY_announcements"
19) "NgoypY_pluginlanguage_script"
20) "NgoypY_onlinelist"
21) "NgoypY_ipbanned"
127.0.0.1:6379>
注:以上是redis slave上面查看的内容,与master一致,表示redis缓存与redis主从配置无问题
至此基本环境搭建完毕!