keepalived+nginx+lnmp 网站架构

《网站架构演变技术研究》

项目实施手册

 

 

 

 

 

 

 

 

 

2019年8月2日

 

第一章:  实验环境确认 4

1.1-1.系统版本 4

1.1-2.内核参数 4

1.1-3.主机网络参数设置 4

1-1-4 .项目拓扑图 5

第二章:  部署后端web服务 6

2-1 .安装Nginx服务端 6

2-1-1 .安装nginx 依赖包 6

2-1-3.修改Nginx配置文件 7

2-1-4.创建nginx启动文件软链接 8

2-1-5.启动nginx,开机自启 8

2-1-6. 查询端口80状况 8

2-1-7.安装其他web服务器 8

2-2 .部署PHP环境 9

2-2-1 .安装PHP 软件 9

2-2-3. 查询端口9000状况 9

2-2-3.安装其他web服务器 9

第三章:  部署NFS服务 9

3-1 .安装NFS 服务器端 10

3.1-1 .nfs软件安装 10

3-1-2 .创建共享目录 10

3-1-3. 修改/etc/exports配置文件 10

3-1-4. 启动服务,开机自启 10

3-1-5. 本地挂载测试 10

3-2. 部署web客户端挂载nfs存储 11

3-2-1.配置web服务器 11

3-2-2.手动挂载-临时挂载 11

3-2-3. 配置开机自动挂载-永久挂载 11

3-2-4.安装其他web服务器 12

3-3. 部署rsync备份服务器 12

3-3-1.修改配置文件,添加nfsbackup新模块 12

3-3-2.启动rsync服务,开机自启 12

3-4在NFS服务器端配置inotify事件监控工具 13

3-4-1.安装inotify 监控工具 13

3-4-2.编写inotify + rsync 实时监控同步脚本 13

3-4-3添加脚本到开机脚本中: 13

第四章:  部署mariadb数据库 13

4-1.安装并配置mariadb数据库 14

4-1-1. 安装mariadb数据库 14

4-1-2.启动mariadb服务,开机自启 14

4-1-3.数据库创建密码 14

4-1-4.创建数据库及用户授权 14

4-1-5.备份WordPress数据库 14

4-1-6.定时备份WordPress数据库 15

五章 :  部署Nginx反向代理软件 15

5-1.在proxy1上安装ningx反向代理软件 16

5-1-1 使用源码包安装Nginx软件 16

5-1-2配置Nginx服务器,实现反向代理功能 16

5-1-3.创建nginx启动文件软链接 17

5-1-4.启动nginx,开机自启 17

5-1-5. 查询端口80状况 17

5-1-6.安装proxy2 代理服务器 18

六章 :  部署Keepalived高可用 19

6-1.在proxy1上安装keepalived主服务器 19

6-1-2. 安装keepalived软件 19

6-1-2.修改keepalived配置文件 19

6-1-3.重启keepalived服务 20

6-2.在proxy2上安装keepalived备用服务器 20

6-2-1. 安装keepalived软件 21

6-2-2.修改keepalived配置文件 21

6-2-3.重启keepalived服务 22

七章 :  部署wordpress网站后台 22

7-1 部署wordpress网站 23

7-1-1 上传源代码到web1上 23

7-1-2 初始化网站配置(使用客户端访问VIP地址) 23

7-1-3 修改wordpress配置文件 25

7-1-4 登录wordpress 后台 25

 

第一章:  实验环境确认

1.1-1.系统版本

[root@nfs ~]# cat /etc/redhat-release

CentOS Linux release 7.5.1804 (Core)

1.1-2.内核参数

[root@nfs ~]# uname -r

3.10.0-862.el7.x86_64

1.1-3.机网络参数设置

主机名

ip地址

用途

web1

192.168.2.11/24

nginx web服务器

web2

192.168.2.12/24

nginx web服务器

web3

192.168.2.13/24

nginx web服务器

nfs

192.168.2.31/24

NFS存储服务器

rsync

192.168.2.22/24

rsync备份服务器

mariadb

192.168.2.21/24

mariadb数据库

proxy1

192.168.2.5/24

192.168.4.5/24

高可用+代理服务器

proxy2

192.168.2.5/24

192.168.4.5/24

高可用+代理服务器

1-1-4 .项目拓扑图

 

 

第二章:  部署后端web服务

2-1 .安装Nginx服务端

  Nginx是C语言开发,安装Nginx需要将官网下载的源码包进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc.

  pcre(PerlCompatible Regular Expressions)是一个Perl库,包括perl兼容的正则表达式库.nginx的http模块使用pcre来解析正则表达式,所以需要在liunx上安装pcre库.

  OpenSSL是一个强大的安全套接字层密码库.囊括主要的密码算、常用的秘钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其他目的使用.Nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库.

 

2-1-1 .安装nginx 依赖包

[root@web1 ~]# yum  -y install gcc pcre-devel openssl-devel

2-1-2.编译安装nginx

nginx-1.12.2.tar.gz分别拷贝至web1,web2,web3上

在安装nginx 之前,先要创建一个nginx用户

[root@web1 ~]#useradd nginx

解压tar包:

[root@web1 ~]#tar -xf /root/nginx-1.12.2.tar.gz

[root@web1 ~]#cd /root/nginx-1.12.2

指定安装nignx 功能模块和安装路径

 [root@web1 ~]# ./configure \

   --prefix=/usr/local/nginx  \      #指定安装路径

   --user=nginx  \                 #指定用户

   --group=nginx  \                #指定组

   --with-http_ssl_module    \      #开启SSL加密功能

   --with-stream  #开启4层反向代理功能

编译安装

  [root@web1 ~]#make && make install

2-1-3.修改Nginx配置文件

  [root@web1 ~]#vim /usr/local/nginx/conf/nginx.conf

     server {

        listen       80;

        server_name  localhost;

        charset utf-8;       #需要中文显示时去掉该注释

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.php index.html index.htm;

error_page  404     /404/404.html;    #指定404报错页面路径

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;    #将请求转发给本机9000端口

            fastcgi_index  index.php;

            include        fastcgi.conf;       #加载其他配置文件

        }

.......

 保存并退出

2-1-4.创建nginx启动文件软链接

[root@web1 ~]#ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

2-1-5.启动nginx,开机自启

[root@web1 ~]#echo “nginx” > /etc/rc.d/rc.local

#加入到开机脚本中

[root@web1 ~]#nginx    

#启动nginx

2-1-6. 查询端口80状况

[root@web1 ~]# netstat -ntlup | grep 80

tcp        0     0 0.0.0.0:80   0.0.0.0:*          LISTEN      835/nginx: master p

tcp        0      0 0.0.0.0:22    0.0.0.0:*             LISTEN      800/sshd            

#nginx 服务已经正常启动

2-1-7.安装其他web服务器

需要在web2,web3 上执行web1相同的操作

2-2 .部署PHP环境

2-2-1 .安装PHP 软件

 [root@web1 ~]#yum -y install php php-fpm php-mysql

 

2-2-2 . 启动PHP-FPM服务

 [root@web1 ~]#systemctl start php-fpm

 [root@web1 ~]#systemctl enable php-fpm

2-2-3. 查询端口9000状况

[root@web1 ~]# netstat -ntlup | grep 9000

tcp     0   0 127.0.0.1:9000   0.0.0.0:*   LISTEN   1110/php-fpm: maste             

#php-fpm 服务已经正常启动

2-2-3.安装其他web服务器

需要在web2,web3 上执行web1相同的操作

第三章:  部署NFS服务

3-1 .安装NFS 服务器端

3.1-1 .nfs软件安装

[root@nfs ~]#yum -y install nfs-utils rpcbind

 

3-1-2 .创建共享目录

[root@nfs ~]#mkdir /www

 

3-1-3. 修改/etc/exports配置文件

[root@nfs ~]# vim /etc/exports

/www 192.168.2.0/24(rw,sync,no_all_squash,no_root_squash)

###

rw: 读写权限

sync: 资料同步到写入存储器中

no_all_squash:不以匿名nobody)身份访问

no_root_squash:登录到主机的用户如果是root,该用户即拥有root权限.

3-1-4. 启动服务,开机自启

[root@nfs ~]# systemctl restart rpcbind

[root@nfs ~]# systemctl enable rpcbind

[root@nfs ~]# systemctl restart nfs

[root@nfs ~]# systemctl enable nfs

NFS使用的是随机端口,每次启动NFS都需要将自己的随机端口注册到rpcbind服务,这样客户端访问NFS时先到rpcbind查询端口信息,得到端口信息后再访问NFS服务。

3-1-5. 本地挂载测试

[root@nfs~]# mount 192.168.2.31:/www    /mnt

[root@nfs ~]# ll -d /mnt drwxr-xr-x. 2 root root 4096 3月 14 00:14 /mnt

[root@nfs ~]# df

文件系统          1K-块    已用     可用 已用% 挂载点

/dev/vda1      31445996 2651320 28794676    9% /

devtmpfs         712928       0   712928    0% /dev

tmpfs            723768       0   723768    0% /dev/shm

tmpfs            723768    8620   715148    2% /run

tmpfs            723768       0   723768    0% /sys/fs/cgroup

192.168.2.31:/www            144696       0   144696    /mnt

[root@nfs ~]# umount   /mnt

 

3-2. 部署web客户端挂载nfs存储

3-2-1.配置web服务器

[root@web1 ~]#yum install nfs-utils

3-2-2.手动挂载-临时挂载

[root@web1 ~]#mount 192.168.2.31:/www  /usr/loca/nginx/html

[root@web1 ~]# df

文件系统             1K-块    已用     可用 已用% 挂载点

/dev/vda1         31445996 2614140 28831856    9% /

devtmpfs            712928       0   712928    0% /dev

tmpfs               723768       0   723768    0% /dev/shm

tmpfs               723768    8648   715120    2% /run

tmpfs               723768       0   723768    0% /sys/fs/cgroup

tmpfs               144696       0   144696    0% /run/user/0

192.168.2.31:/www 31446016 2651136 28794880    9% /usr/local/nginx/html

3-2-3. 配置开机自动挂载-永久挂载

[root@web1 ~]# tail -1 /etc/rc.d/rc.local

mount -t nfs 192.168.2.31:/www /usr/local/nginx/html/ -o \ nolock,nfsvers=3,vers=3

3-2-4.安装其他web服务器

需要在web2,web3 上执行web1相同的操作

3-3. 部署rsync备份服务器

3-3-1.修改配置文件,添加nfsbackup新模块

在配置文件/etc/rsyncd.conf里添加nfsbackup新模块

[nfsbackup]

#使用目录

path = /www-bak

#备份路径

ignore errors    

#有错误时忽略

read only = false  

#阻止远程列表(不让通过远程方式看服务端共享内容

list = false

hosts allow = 192.168.2.0/24   

#允许IP

hosts deny = 0.0.0.0/32     

#禁止IP

3-3-2.启动rsync服务,开机自启

[root@rsync ~]# systemctl restart rsyncd

[root@rsync ~]# systemctl enable rsyncd

[root@rsync ~]# netstat -ntlup | grep rsync

tcp      0      0 0.0.0.0:873     0.0.0.0:*    LISTEN          8259/rsync          

tcp6     0      0 :::873              :::*      LISTEN          8259/rsync          

3-4在NFS服务器端配置inotify事件监控工具

 

3-4-1.安装inotify 监控工具

[root@nfs ~]# yum -y install inotify-tools

3-4-2.编写inotify + rsync 实时监控同步脚本

[root@nfs ~]#vim /root/.inotify-www.sh

#!/bin/bash

while inotifywait -rqq /www/

do

rsync -az --delete /www/ [email protected]:/www-bak/

done &

[root@nfs ~]#chmod +x /root/.inotify-www.sh

3-4-3添加脚本到开机脚本中:

[root@nfs ~]#echo “/root/.inotify-www.sh” >> /etc/rc.d/rc.local   

第四章:  部署mariadb数据库

4-1.安装并配置mariadb数据库

4-1-1. 安装mariadb数据库 

[root@mariadb ~]#yum -y install mariadb mariadb-server  mariadb-devel  

4-1-2.启动mariadb服务,开机自启

 [root@mariadb ~]#Systemctl start mariadb.service    

 [root@mariadb ~]#Systemctl enable mariadb.service

4-1-3.数据库创建密码

[root@mariadb ~]#mysqladmin -u root password ‘wordpress’

 

4-1-4.创建数据库及用户授权

[root@mariadb ~]#mysql -uroot -pwordpress

MariaDB [(none)]> create database wordpress;                        

MariaDB [(none)]>grant all privileges on wordpress.* to ‘wordpress’@'%' identified by 'wordpress' ;                       

#创建WordPress用户对WordPress库拥有全部权限,WordPres可以从任意地址登录,WordPress密码为’WordPress’%:匹配所有主机

MariaDB [(none)]> flush privileges;

#重新加载数据库

MariaDB [(none)]>exit

 

4-1-5.备份WordPress数据库

[root@mariadb ~]# vim /root/.mysql-bak.sh    

#自定义一个备份脚本

#!/bin/bash

mysqldump -uroot -pwordpress wordpress > \

 /tmp/wp-`date  +%Y-%m-%d-%H:%M:%S`.sql \

scp  /tmp/wp-`date   +%Y-%m-%d-%H:%M:%S`.sql   \ [email protected]:/mariadb/ >/dev/null

#远程拷贝到rsync 服务器上

[root@mariadb ~]# ls /tmp

wp-2019-08-01-10:14:05.sql

wp-2019-08-01-10:23:16.sql

wp-2019-08-01-10:24:16.sql

wp-2019-08-01-10:25:16.sql

4-1-6.定时备份WordPress数据库

[root@mariadb ~]# crontab -e     

00 */6 * * *   /root/.mysql-bak.sh

#每隔6个小时备份一次,备份到本地,以及rsync服务器上

 

 

第五章:  部署Nginx反向代理软件

5-1.在proxy1上安装ningx反向代理软件

5-1-1 使用源码包安装Nginx软件

安装依赖包

[root@proxy1 ~]#yum -y install gcc pcre-devel openssl-devel

创建用户

[root@proxy1 ~]#useradd -s /sbin/nologin nginx

解压tar包,并编译安装

[root@proxy1 ~]#tar -xf /root/nginx-1.12.2.tar.gz

[root@proxy1 ~]#cd /root/nginx-1.12.2

[root@proxy1 ~]#./configure --user=nginx --group=nginx \

--wuth-http-ssl_module  

[root@proxy1 ~]#make && make install

5-1-2配置Nginx服务器,实现反向代理功能

修改nginx配置文件

[root@proxy1 ~]# vim /usr/local/nginx/conf/nginx.conf

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    upstream webserver {

                           server 192.168.2.11:80;

                           server 192.168.2.12:80;

                           server 192.168.2.13:80;

}

#使用upstream定义后端服务器集群,集群名称任意(如webserver)

#使用server定义集群中的具体服务器和端口

......

server {

listen     80;

server_name www.fxkj.com fxkj.com;

         location / {

         root   html;

#通过proxy_pass将用户的请求转发给webserver集群

     proxy_pass http://webserver;

     index  index.php index.html index.htm;

  }

}

5-1-3.创建nginx启动文件软链接

[root@web1 ~]#ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

5-1-4.启动nginx,开机自启

[root@web1 ~]#echo “nginx” > /etc/rc.d/rc.local

#加入到开机脚本中

[root@web1 ~]#nginx    

#启动nginx

5-1-5. 查询端口80状况

[root@web1 ~]# netstat -ntlup | grep 80

tcp        0     0 0.0.0.0:80   0.0.0.0:*          LISTEN      835/nginx: master p

tcp        0      0 0.0.0.0:22    0.0.0.0:*             LISTEN      800/sshd            

#nginx 服务已经正常启动

5-1-6.安装proxy2 代理服务器

需要在proxy2上执行proxy1相同的操作

第六章:  部署Keepalived高可用

6-1.在proxy1上安装keepalived主服务器

6-1-2. 安装keepalived软件

[root@web1 ~]# yum -y install keepalived

6-1-2.修改keepalived配置文件

[root@web1 ~]#vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

     vrrp_iptables

#不添加任何防火墙规则,非常重要!!!

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id proxy1    

#自定义设置路由ID号

   vrrp_skip_check_adv_addr

   vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

#添加检测Nginx代理软件的脚本

vrrp_script check_nginx {

script "/etc/keepalived/check.sh"

interval 2

}

vrrp_instance VI_1 {

state MASTER  

    #主服务器为MASTER (备服务器改为BACKUP)

interface eth0  

#定义网络监听接口

virtual_router_id 51

priority 100    

#服务器优先级,优先级高优于获取vip

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass feixiangkeji  

#主备服务器密码必须一致,建议不要使用默认密码

    }

virtual_ipaddress { /

        192.168.4.80

#指定VIP地址

}

tarck_script {      #跟踪check_nginx检测脚本,一头一尾

check_nginx

}

}

6-1-3.重启keepalived服务

[root@web1 ~]#systemctl  restart keepalived

6-2.在proxy2上安装keepalived备用服务器

6-2-1. 安装keepalived软件

[root@web2 ~]# yum -y install keepalived

6-2-2.修改keepalived配置文件

[root@web2 ~]#vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

     vrrp_iptables

#不添加任何防火墙规则,非常重要!!!

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id proxy2

#自定义设置路由ID号

   vrrp_skip_check_adv_addr

   vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

#添加检测Nginx代理软件的脚本

vrrp_script check_nginx {

script "/etc/keepalived/check.sh"

interval 2

}

vrrp_instance VI_1 {

state BACKUP

#备服务器设为BACKUP

interface eth0

#定义网络监听接口

virtual_router_id 51

priority 90

#优先级要低于proxy1优先级

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass feixiangkeji

#主备服务器密码必须一致,建议不要使用默认密码

    }

virtual_ipaddress {

192.168.4.80

#指定VIP地址

}

tarck_script {  #跟踪check_nginx检测脚本,一头一尾

check_nginx

}

}

6-2-3.重启keepalived服务

[root@web2 ~]#systemctl  restart keepalived

第七章:  部署wordpress网站后台

7-1 部署wordpress网站

7-1-1 上传源代码到web1上

[root@web1 ~]# unzip wordpress.zip

[root@web1 ~]# cd wordpress

[root@web1 wordpress]# tar -xf wordpress-5.0.3-zh_CN.tar.gz

[root@web1 wordpress]# cp -r  wordpress/*  /usr/local/nginx/html/

[root@web1 wordpress]# chown -R apache.apache  /usr/local/nginx/html/

#web1.web2,web3 的网站数据会自动同步

提示:动态网站运行过程中,php脚本需要对网站目录有读写权限,而php-fpm默认启动用户为apache。

7-1-2 初始化网站配置(使用客户端访问VIP地址

[root@client ~]# firefox http://192.168.4.80/

 

开发人员在写代码的时候并不知道未来数据库服务器的IP、端口、数据库名称、账户等信息,该配置页面主要的作用就是动态配置数据库信息,根据前面步骤配置的数据库信息填空即可

 

第一次使用Wordpress需要给你的网站设置基本信息,如网站标题、网站管理员账户与密码等信息,配置完成后点击安装wordpress即可.

 

 

7-1-3 修改wordpress配置文件

[root@web1~]#vim /usr/local/nginx/html/wp-config.php

define('DB_NAME', 'wordpress')这行前面添加如下两行内容:

[root@web1~]# vim /usr/local/nginx/html/wp-config.php

define('WP_SITEURL', 'http://192.168.4.80');

define('WP_HOME', 'http://192.168.4.80');

如果不添加这两行配置,浏览器访问网站某个子页面后,URL会固定到某一台后端服务器不轮询。

7-1-4 登录wordpress 后台

[root@client ~]# firefox  http://192.168.4.80/wp-login.php

输入管理员用户名和密码:

 

2019-08-25 00:49:25

猜你喜欢

转载自www.cnblogs.com/fxmmq/p/11406702.html
今日推荐