天天看点

wordperss上线项目架构优化--代理服务,多台web主机的数据分离以及存储和数据库的分离wordperss上线项目架构优化–代理服务,多台web主机的数据分离以及存储和数据库的分离

目录

  • wordperss上线项目架构优化--代理服务,多台web主机的数据分离以及存储和数据库的分离
    • 拓扑及主机配置
    • 配置额外的web服务器
    • 配置NFS服务器
    • 配置代理服务器
    • 配置名称解析

wordperss上线项目架构优化–代理服务,多台web主机的数据分离以及存储和数据库的分离

  • 本文接上文单机安装基于LNMP结构的WordPress网站

拓扑及主机配置

主机角色 主机名 IP地址
client client eth0: 192.168.88.10/24
代理服务器 proxy

eth0: 192.168.88.5/24

eth1: 192.168.99.5/24

web服务器 web1 eth1: 192.168.99.11/24
web服务器 web2 eth1: 192.168.99.12/24
web服务器 web3 eth1: 192.168.99.13/24
数据库服务器 database eth1: 192.168.99.21/24
NFS服务器 nfs eth1: 192.168.99.31/24

配置额外的web服务器

  • 创建两台虚拟机,并配置防火墙、SELINUX、主机名、IP地址、yum
  • 配置web服务器
# 把web1的nginx打包拷贝到web2和web3上
[[email protected] ~]# cd /usr/local/
[[email protected] local]# tar czf /root/nginx.tar.gz nginx
[[email protected] local]# cd
[[email protected] ~]# scp nginx.tar.gz 192.168.99.12:/root/
[[email protected] ~]# ^12^13   # 将上一条命令的12换成13执行

# 在web2和web3上解压,实现nginx部署
[[email protected] ~]# tar xf nginx.tar.gz -C /usr/local/
[[email protected] ~]# tar xf nginx.tar.gz -C /usr/local/

# 把web1上的service文件拷贝到web2和web3上
[[email protected] ~]# scp /usr/lib/systemd/system/nginx.service 192.168.99.12:/usr/lib/systemd/system/
[[email protected] ~]# ^12^13

# 在web2和web3上启服务
[[email protected] ~]# systemctl daemon-reload 
[[email protected] ~]# systemctl enable nginx.service --now
[[email protected] ~]# ss -tlnp | grep :80
LISTEN     0      128          *:80
[[email protected] ~]# systemctl daemon-reload 
[[email protected] ~]# systemctl enable nginx.service --now
[[email protected] ~]# ss -tlnp | grep :80
LISTEN     0      128          *:80
           
  • 配置web2和web3支持php
[[email protected] ~]# yum install -y php php-fpm php-mysql
[[email protected] ~]# systemctl enable php-fpm --now

[[email protected] ~]# yum install -y php php-fpm php-mysql
[[email protected] ~]# systemctl enable php-fpm --now
           
  • 测试访问web2和web3
wordperss上线项目架构优化--代理服务,多台web主机的数据分离以及存储和数据库的分离wordperss上线项目架构优化–代理服务,多台web主机的数据分离以及存储和数据库的分离
  • 在web1、web2、web3上任意的一个页面上新建文章,另外的两台主机,也可以看到更新。因为3台web服务器新建文章时,都是把数据存入到数据库服务器了。

配置NFS服务器

  • 准备环境
  • 创建虚拟机,并配置防火墙、SELINUX、主机名、IP地址、yum
  • 配置NFS服务
# 安装nfs
[[email protected] ~]# yum install -y nfs-utils.x86_64 

# 配置共享
[[email protected] ~]# mkdir /web_share
[[email protected] ~]# vim /etc/exports
/web_share      192.168.99.0/24(rw,no_root_squash)
# rw表示读写权限
# no_root_squash,表示远程主机root创建的文件,属主属组就是root。默认会变成nfsnobody

# 启动服务。注意,NFS服务依赖rpcbind服务
[[email protected] ~]# systemctl enable rpcbind --now
[[email protected] ~]# ss -tlnp | grep :111
LISTEN     0      128          *:111
[[email protected] ~]# systemctl enable nfs --now
[[email protected] ~]# ss -tlnp | grep :2049
LISTEN     0      64           *:2049
# 验证
[[email protected] ~]# showmount -e
Export list for nfs:
/web_share 192.168.99.0/24
           
  • 迁移文件至nfs共享
# 1. 将网页目录保留权限,打压缩包
[[email protected] ~]# cd /usr/local/nginx/
[[email protected] nginx]# tar cpzf /root/html.tar.gz html

# 2. 拷贝文件至nfs服务器
[[email protected] ~]# scp html.tar.gz 192.168.99.31:/root/

# 3. 在nfs服务器上解压
[[email protected] ~]# tar xf html.tar.gz -C /web_share/

# 4. 删除web服务器html目录中的内容
[[email protected] ~]# rm -rf /usr/local/nginx/html/*
[[email protected] ~]# rm -rf /usr/local/nginx/html/*
[[email protected] ~]# rm -rf /usr/local/nginx/html/*

# 5. 此时,通过浏览器访问各web服务器,将会报403错误
# 6. 在各web服务器上挂载共享目录
[[email protected] ~]# yum install -y nfs-utils
[[email protected] ~]# echo '192.168.99.31:/web_share/html /usr/local/nginx/html nfs defaults 0 0' >> /etc/fstab 
[[email protected] ~]# mount -a
[[email protected] ~]# df -h /usr/local/nginx/html/
文件系统                      容量  已用  可用 已用% 挂载点
192.168.99.31:/web_share/html   30G  1.4G   29G    5% /usr/local/nginx/html

[[email protected] ~]# yum install -y nfs-utils
[[email protected] ~]# echo '192.168.99.31:/web_share/html /usr/local/nginx/html nfs defaults 0 0' >> /etc/fstab 
[[email protected] ~]# mount -a
[[email protected] ~]# df -h /usr/local/nginx/html/
文件系统                      容量  已用  可用 已用% 挂载点
192.168.99.31:/web_share/html   30G  1.4G   29G    5% /usr/local/nginx/html

[[email protected] ~]# yum install -y nfs-utils
[[email protected] ~]# echo '192.168.99.31:/web_share/html /usr/local/nginx/html nfs defaults 0 0' >> /etc/fstab 
[[email protected] ~]# mount -a
[[email protected] ~]# df -h /usr/local/nginx/html/
文件系统                      容量  已用  可用 已用% 挂载点
192.168.99.31:/web_share/html   30G  1.4G   29G    5% /usr/local/nginx/html

# 7. 此时,通过浏览器访问各web服务器,又将恢复正常
# 8. 在任意一台web服务器上新建文章,查看web是否同步
           

配置代理服务器

  • 准备环境
  • 创建虚拟机,并配置防火墙、SELINUX、主机名、IP地址、yum
  • 配置HAProxy服务器
[[email protected] ~]# yum install -y haproxy
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
# 把63行到最后一行删除,然后追加以下内容
listen wordpress *:80
    balance roundrobin
    server web1 192.168.99.11:80 check inter 2000 rise 2 fall 3
    server web2 192.168.99.12:80 check inter 2000 rise 2 fall 3
    server web3 192.168.99.13:80 check inter 2000 rise 2 fall 3
[[email protected] ~]# systemctl enable haproxy.service --now
[[email protected] ~]# ss -tlnp | grep :80
LISTEN     0      128          *:80
           
  • 客户端访问http://192.168.88.5或http://192.168.99.5仍然可以正常访问
  • 为HAProxy配置监控页面
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
# 在结尾追加以下内容
listen mon *:1080
    stats refresh 30s
    stats uri /mon
    stats auth admin:admin
[[email protected] ~]# systemctl restart haproxy
# 访问http://192.168.88.5:1080/mon。不断访问http://192.168.88.5,在监控页可以看到不同的服务器有连接数。
           
wordperss上线项目架构优化--代理服务,多台web主机的数据分离以及存储和数据库的分离wordperss上线项目架构优化–代理服务,多台web主机的数据分离以及存储和数据库的分离

配置名称解析

  • 通过本机hosts文件实现名称解析
附:如果客户端是windows主机,则使用记事本程序打开

C:\windows\System32\drivers\etc\hosts

添加名称解析
  • 访问http://www.lab.com
  • 当点击http://www.lab.com页面中任意链接时,地址栏上的地址,都会变成

    192.168.99.11

    。通过以下方式修复它:
# 在nfs服务器上修改配置文件
[[email protected] ~]# vim /web_share/html/wp-config.php 
# define('DB_NAME', 'wordpress')它的上方添加以下两行:
define('WP_SITEURL', 'http://www.lab.com');
define('WP_HOME', 'http://www.lab.com');