目錄
- 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

- 在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,在監控頁可以看到不同的伺服器有連接配接數。
配置名稱解析
- 通過本機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');