手机版
你好,游客 登录 注册
背景:
阅读新闻

CentOS 7下搭建百万PV网站架构详述

[日期:2018-11-05] 来源:Linux社区  作者:Linux [字体: ]

日均百万PV的网站站,费用也不低,并且CDN节点有时会出问题,还 需要每次的更改后刷新CDN,不太方便。

改造后期望:

配置独立服务器,升级带宽,更改环境以支持PHP的程序,实现本地化的广告投放功能,以扩展自有化。

PV(Page View,页面浏览量)即点击量,通常意义上说PV的多少是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标。pv的解释是这样的:一个访问者在24小时(0点-23点)内到底看了网站的几个页面。需要注意的是:同一个人浏览网站的同一个页面,不重复计算pv量,点击100次页只算1次。

案例概述:本案例设计采用四层模型实现,主要分为前端反向代理层,web层,数据库缓存层和数据库层。前端反向代理词采用主备模式,web层采用群集模式,数据库缓存层采用主备模式,数据层采用主从模式

具体实现:

下面将记录实现过程的点滴,期间得益于互联网上许多资料的帮助,在此一并表示感谢。此文章力争做到细致,清晰,希望对后来者起到一定帮助作用。

这里我为了节省资源,将前端代理层,数据库缓存层,数据库层部署在两台虚拟机上,将web层分别部署在两台虚拟机上。

拓扑图如下:
CentOS 7下搭建百万PV网站架构详述

实验环境如下:
CentOS 7下搭建百万PV网站架构详述

------------------------------------------分割线------------------------------------------

本文的源码包可从以下信息得到下载:

点击这个http://www.linuxidc.com/Linux/2013-12/93755.htm 链接去关注 Linux公社官方微信,关注后回复数字155206。即可得到网友的分享密码。

如果取消关注Linux公社公众号,即使再次关注,也将无法提供本服务!

链接: https://pan.baidu.com/s/1SK5Yv0bEPkzDjATqC67RZg  密码:获得见上面的方法,地址失效请在下面留言。

------------------------------------------分割线------------------------------------------

具体部署如下:
一 在前面两台主从服务器上安装nginx和keepalived
rpm -ivh http://nginx.org/packages/CentOS/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm #

装带有nginx rpm软件包的源,主从都要做

yum install nginx keepalived -y            #使用centos默认的仓库完成的安装
vim /etc/keepalived/keepalived.conf      #配置keepalived的主配置文件
! Configuration File for keepalived
vrrp_script nginx {                      #定义函数库脚本
        script "/opt/shell/nginx.sh"      #添加脚本路径
        interval 2                    #定义每次执行该脚本的间隔为2s
}

global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id NGINX_HA          #定义该服务器的名称,从服务器要定义不同的名称好加以区分
}

vrrp_instance VI_1 {
    state MASTER              #将主服务器的角色定为MASTER,从服务器为BACKUP
    interface ens33            #将网卡接口改为ens33
    virtual_router_id 51       
    priority 100                #定义优先级,主服务器要高于从服务器
    advert_int 1
    authentication {
        auth_type PASS        #这里表示主从服务器的同步密码,保持默认即可
        auth_pass 1111
    } 
    track_script {
        Nginx              #在这里调用上面定义好的脚本,注意脚本名称一定要相同
    } 
    virtual_ipaddress {
        192.168.199.188        #定义虚拟IP,虚拟IP一定要和服务器的真实IP在同一网段
    } 

在指定的目录下创建脚本

mkdir /opt/shell
vim /opt/shell/nginx.sh       
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $k -gt 0 ];then
        /bin/systemctl start nginx.service
else
/bin/systemctl stop nginx.service
fi                              #该脚本作用是在启动keepalived服务时就可以直接启动nginx服务
chmod +x /opt/shell/nginx.sh      #赋予脚本执行权限

配置nginx反向代理:

vim /etc/nginx/nginx.conf        #配置nginx的配置文件
upstream tomcat_pool {
                server 192.168.199.131:8080; 
                server 192.168.199.132:8080;      #定义后端的两台Tomcat地址
                ip_hash;            #会话稳固功能,否则无法通过vip地址登录
        }     
        server {
                listen 80;
                server_name 192.168.199.188;    #虚拟IP
                location / {
                        proxy_pass http://tomcat_pool;
                        proxy_set_header X-Real-IP $remote_addr;
                }
        }


nginx -t -c /etc/nginx/nginx.conf #测试配置文件语法

从服务器的配置和主服务器的配置基本相同,唯一不同地方在于keepalived的配置文件,在上方都有说明,这里就不再演示

systemctl start keepalived.service      #开启主服务器的keepalived服务
netstat -ntap | grep nginx      #查看nginx是否开启,要想关闭nginx,需要先关闭keepalived

CentOS 7下搭建百万PV网站架构详述

systemctl start keepalived.service      #开启从服务器的keepalived服务

netstat -ntap | grep nginx      #查看nginx是否开启,要想关闭nginx,需要先关闭keepalived,注意第二台的nginx启动可能会有些慢,如果nginx端口一致启动不了就去检查keepalived的配置文件和自己定义的脚本,大部分错误都是这两个地方

CentOS 7下搭建百万PV网站架构详述

二 部署两台Tomcat节点服务器
解压所需要的两个软件包

tar zxvf jdk-8u144-linux-x64.tar.gz -C /opt        #jdk是一个java运行环境,要想安装tomcat必须先安装jdk
tar zxvf apache-tomcat-8.5.23.tar.gz -C /opt
mv jdk1.8.0_144/ /usr/local/java
mv apache-tomcat-8.5.23/ /usr/local/tomcat8    #为了方便使用jdk与Tomcat,我将它们重命名到系统目录下vim /etc/profile                #添加环境变量
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib

source /etc/profile #重新加载环境变量

java -version #使用该命令查看jdk是否安装成功,如果显示版本号表示安装成功

CentOS 7下搭建百万PV网站架构详述

ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown      #对Tomcat的开启与关闭命令建立软链接

tomcatup #开启tomcat

CentOS 7下搭建百万PV网站架构详述

cd /usr/local/tomcat8/webapps/ROOT/
mv index.jsp index.jsp.bk
vim index.jsp                        #为了方便之后的测试我修改一下tomcat的首页
<h1>server 131!</h1>

再次对主节点的Tomcat进行访问
CentOS 7下搭建百万PV网站架构详述

从节点的配置与主节点完全一样,为了便于区分我也修改了从节点的首页
CentOS 7下搭建百万PV网站架构详述

接着我用虚拟IP进行访问,测试反向代理是否成功
CentOS 7下搭建百万PV网站架构详述

cd /usr/local/tomcat8/conf      #修改两台节点服务器的主配置文件
vim server.xml    #跳到行尾,在Host name下新增 148
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>    #跳到行尾,在Host name下新增 日志调试信息debug为0表示信息越少,docBase指定访问目录

CentOS 7下搭建百万PV网站架构详述

三 部署MySQL数据库(在主从服务器上都要部署)
这里为了节省时间我采用yum安装mariadb数据库来代替MySQL数据库,这两个数据库的功能相同,而且mariadb的配置更加简单

yum install mariadb-server mariadb -y
systemctl enable mariadb.service
systemctl start mariadb.service        #开启数据库
netstat -ntap | grep 3306            #查看数据库端口

[root@localhost ~]# mysql_secure_installation        #对数据库进行常规安全设置

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):                #这里可以直接回车,这时我还没有密码
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y                            #这里问是否创建密码,选择是
New password:                                      #输入新的密码
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n      #这里问是否删除匿名用户,选择no
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n    #这里问是否拒绝管理员进行远程登录,选择no
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n      #这里问是否删除默认测试数据库,选择no
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y            #最后问是否现在加载权限表,选择yes
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

导入slsaledb数据库

mysql -u root -p < slsaledb-2014-4-10.sql
mysql -u root -p
show databases;

CentOS 7下搭建百万PV网站架构详述

MariaDB [(none)]> GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY '123456';    #给root用户授权,让其可以管理slsaledb
MariaDB [(none)]> flush privileges;      #重新加载数据库权限

在从数据库上进行同样的部署

以下操作在两台tomcat节点做

tar zxvf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/        #将会员商城的软件包解压至Tomcat的站点目录下
cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
vim jdbc.properties          #修改数据库IP地址为VRRP虚拟地址,以及授权用户root和密码123456

CentOS 7下搭建百万PV网站架构详述

tomcatdown
tomcatup      #重新启动Tomcat服务让配置文件生效

http://192.168.199.188 #通过虚拟IP访问商城
CentOS 7下搭建百万PV网站架构详述

四 部署Redis群集(在主服务器和从服务器上做)

yum install epel-release -y        #使用centos7.4 默认源安装
yum install redis -y
vim /etc/redis.conf          #修改Redis主配置文件
bind 0.0.0.0

CentOS 7下搭建百万PV网站架构详述

systemctl start redis.service      #开启Redis服务
netstat -ntap | grep 6379        #查看监听端口
redis-cli -h 192.168.199.129 -p 6379    #在主服务器上测试连接
192.168.199.129:6379> set name test    #设置name,值是test
OK
192.168.199.129:6379> get name        #获取name值
"test"

接着在从服务器上也是进行同样的部署
CentOS 7下搭建百万PV网站架构详述
在从服务器上需要在配置文件中多加如下这句话,将地址指向主服务器
CentOS 7下搭建百万PV网站架构详述

systemctl start redis.service            #开启从服务器的Redis
redis-cli -h 192.168.199.130 -p 6379    #登录Redis
192.168.199.130:6379> get name      #查看name值
"test"                            #数据会从主服务器上同步

在两台Tomcat节点服务器配置商城项目中的连接redis的参数

vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
38        <!--redis 配置 开始-->
47                <constructor-arg value="192.168.199.188"/>
 48                <constructor-arg value="6379"/>

测试缓存效果

redis-cli -h 192.168.199.188 -p 6379
192.168.199.188:6379> info
keyspace_hits:1            #命中数
keyspace_misses:0          #未命中数

以下配置redis集群主从切换----只在主服务器上操作

redis-cli -h 192.168.199.129 info Replication
#Replication
role:master
connected_slaves:1
slave0:ip=192.168.199.130,port=6379,state=online,offset=1541,lag=1
master_repl_offset:1541
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1540

vim /etc/redis-sentinel.conf
17 protected-mode no

69 sentinel monitor mymaster 192.168.199.129 6379 1    #1表示有一台从服务器

98 sentinel down-after-milliseconds mymaster 3000

systemctl start redis-sentinel.service    #启动集群
netstat -ntap | grep 26379

systemctl start redis-sentinel.service    #在从服务器上启动集群
netstat -ntap | grep 26379

redis-cli -h 192.168.199.129 -p 26379 info Sentinel    #再次在主服务器上进行查看
#Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.199.129:6379,slaves=1,sentinels=2

验证主从切换
在主上

systemctl stop redis
redis-cli -h 192.168.199.129 -p 26379 info Sentinel
#Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.199.130:6379,slaves=1,sentinels=3    #主服务器进行了切换,如果再次开启主服务器,角色并不会切换,除非关掉从服务器

验证数据同步情况:

在主服务器上:
redis-cli -h 192.168.199.129 -p 6379
192.168.199.129:6379> set name2 test2
OK
192.168.199.129:6379> get name2
"test2"

在从服务器上:
redis-cli -h 192.168.199.130 -p 6379
192.168.199.130:6379> get name2
"test2"                                #如果无法连接或无法同步就重启两边的群集服务和redis服务

五 部署MySQL主从同步

在主服务器上:
vim /etc/my.cnf          #配置主MySQL的配置文件
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1
log_slave_updates=true
sync_binlog=1

systemctl restart mariadb.service    #重新启动mariadb数据库使配置文件生效

netstat -ntap | grep 3306

mysql -u root -p          #登录数据库

MariaDB [(none)]> show master status;      #查看日志文件名称和偏移量
MariaDB [(none)]> grant replication slave on *.* to 'rep'@'192.168.199.%' identified by '123456';    #给从数据库授权
MariaDB [(none)]> flush privileges;          #刷新权限

在从服务器上:
vim /etc/my.cnf
[mysqld]
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2            #id值不能与主服务器相同
log_slave_updates=true
sync_binlog=1

systemctl restart mariadb.service      #重新启动数据库,使配置文件生效

netstat -ntap | grep 3306

mysql -u root -p
MariaDB [(none)]> change master to master_host='192.168.199.129',master_user='rep',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=473;    #设置主服务器的日志文件名称和偏移量
MariaDB [(none)]> start slave;          #开启同步
MariaDB [(none)]> show slave status\G;    #查看同步状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes              #这两条信息都显示Yes表示同步成功

六 登录验证(登录用户名为admin)

http://192.168.199.188/


CentOS 7下搭建百万PV网站架构详述

在主服务器上:
redis-cli -h 192.168.199.188 -p 6379 
192.168.199.188:6379> info
keyspace_hits:4       

#对命中数进行查看

到此百万PV网站架构搭建完成

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2018-11/155206.htm

linux
相关资讯       CentOS 7搭建网站  百万PV网站架构 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款