负载均衡 +LNMP+NFS+rsync+lsync部署流程
文章目录 负载均衡 +LNMP+NFS+rsync+lsync部署流程服务器准备 需求 配置过程 1.nfs服务器配置动态资源公共存储磁盘/data/wordpress 2.db01服务器配置存放静态资源的数据库服务 3.web两台服务器部署nginx+PHP服务 4.web两台服务器编写业务配置文件,创建代码目录并修改其属主组,上传代码 5.浏览器访问,并进行“数据库的打通”配置(两台web都做) 6.将nfs的/data/wordpress挂载到业务存储动态资源的位置 7.lb01配置负载均衡 8.backup服务器配置rsync备份服务 9.nfs服务器部署lsync服务监控动态文件目录,实时同步至backup 10.设置定时任务、脚本,实现定期备份代码 11.在backup服务器部署nfs服务器,编写脚本,防止单点故障
服务器准备
主机名 公网IP 私网IP 功能 web01 10.0.0.7 172.16.1.7 Nginx+PHP web02 10.0.0.8 172.16.1.8 Nginx+PHP db01 10.0.0.51 172.16.1.51 mariadb nfs 10.0.0.31 172.16.1.31 nfs,lsync lb01 10.0.0.5 172.16.1.5 nginx负载均衡 backup 10.0.0.41 172.16.1.41 rsync
需求
部署WordPress业务;
1 .所有服务统一虚拟用户:dezyan ,uid和gid均为666
2 .动态资源存储到nfs服务器中,而且为了防止nfs单点故障,要设置备用nfs服务器,并设置脚本
3 .静态资源存放在db01服务器中
4 .需要多台web服务器,并且要实现负载均衡
5 .用户的动态文件需要实时同步至backup服务器中的/imag目录
6 .要每天24点备份代码目录至backup服务器中的/code目录,要压缩为以当天主机名+日期命名的压缩包再备份
7 .除代理服务器外,所有服务器都不得暴露在公网中
配置过程
1.nfs服务器配置动态资源公共存储磁盘/data/wordpress
1 .创建虚拟用户dezyan
[ root@nfs ~]
[ root@nfs ~]
[ root@nfs ~]
uid = 666 ( dezyan) gid = 666 ( dezyan) groups = 666 ( dezyan)
2 .安装nfs服务,并进行配置
[ root@nfs ~]
[ root@nfs ~]
/data/wordpress 172.16 .1.0/24( rw,sync,all_squash,anonuid= 666 ,anongid= 666 )
3 .创建必要文件,并启动
[ root@nfs ~]
[ root@nfs ~]
[ root@nfs ~]
4 .检查
[ root@nfs ~]
Export list for 172.16 .1.31:
/data/wordpress 172.16 .1.0/24
5 .更改属主属组
[ root@nfs ~]
2.db01服务器配置存放静态资源的数据库服务
1 .安装mariadb服务,并启动
[ root@db01 ~]
[ root@db01 ~]
[ root@db01 ~]
2 .设置数据库密码
[ root@db01 ~]
3 .设置远程登录账户
[ root@db01 ~]
MariaDB [ ( none) ] > grant all on *.* to dzy@'%' identified by 'dzy123.com' ;
4 .创建WordPress静态资源存放的数据库,名为wordpress
MariaDB [ ( none) ] > create database wordpress;
5 .使用web服务器远程连接测试
注意!web01和web02服务器也要下载mariadb-server服务,但不需要启动,因为需要MySQL命令
[ root@web01 ~]
[ root@web02 ~]
[ root@web02 ~]
Welcome to the MariaDB monitor. Commands end with ; or \ g.
3.web两台服务器部署nginx+PHP服务
1 .创建虚拟用户dezyan
[ root@web01 ~]
[ root@web01 ~]
[ root@web01 ~]
uid = 666 ( dezyan) gid = 666 ( dezyan) groups = 666 ( dezyan) 2 .安装nginx服务,修改配置,启动服务
[ root@web01 ~]
[ nginx-stable]
name = nginx stable repo
baseurl = http://nginx.org/packages/centos/7/$basearch /
gpgcheck = 0
enabled = 1
gpgkey = https://nginx.org/keys/nginx_signing.key
module_hotfixes = true
[ root@web01 ~]
[ root@web01 ~]
[ root@web01 nginx]
user dezyan;
[ root@web01 nginx]
[ root@web01 nginx] 3 .安装PHP,修改配置,启动服务
[ root@web01 ~]
[ root@web01 ~] 24 user = dezyan26 group = dezyan38 listen = 127.0 .0.1:9000
[ root@web01 ~]
[ root@web01 ~]
4.web两台服务器编写业务配置文件,创建代码目录并修改其属主组,上传代码
[ root@web01 ~]
server { listen 80 ; server_name wp.dezyan.com; root /code/wordpress; location / { index index.php index.html; } location ~ \ .php$ { fastcgi_pass 127.0 .0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ; include fastcgi_params; }
}
[ root@web01 ~]
[ root@web01 ~]
[ root@web01 ~]
[ root@web01 ~]
[ root@web01 wordpress]
[ root@web01 wordpress]
[ root@web01 wordpress]
[ root@web01 wordpress]
[ root@web01 ~]
5.浏览器访问,并进行“数据库的打通”配置(两台web都做)
数据库名 wordpress 用户名 dzy 密码 dzy123.com 数据库主机 172.16.1.51 表前缀 wp_
6.将nfs的/data/wordpress挂载到业务存储动态资源的位置
mkdir -p /code/wordpress/wp-content/uploads/
mount -t nfs 172.16 .1.31:/data/wordpress /code/wordpress/wp-content/uploads/
[ root@web01 ~]
172.16 .1.31:/data/wordpress /code/wordpress/wp-content/uploads/ nfs defaults 0 0
1 .创建虚拟用户dezyan
[ root@web01 ~]
[ root@web01 ~]
[ root@web01 ~]
uid = 666 ( dezyan) gid = 666 ( dezyan) groups = 666 ( dezyan) 2 .安装nginx服务,修改配置,启动服务
[ root@web01 ~]
[ nginx-stable]
name = nginx stable repo
baseurl = http://nginx.org/packages/centos/7/$basearch /
gpgcheck = 0
enabled = 1
gpgkey = https://nginx.org/keys/nginx_signing.key
module_hotfixes = true
[ root@web01 ~]
[ root@web01 ~]
[ root@web01 nginx]
user dezyan;
vim /etc/nginx/proxy_paramsproxy_set_header Host $http_host ; proxy_http_version 1.1 ; proxy_connect_timeout 30 ; proxy_send_timeout 60 ; proxy_read_timeout 60 ; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k;
[ root@lb01 ~]
[ root@lb01 conf.d]
upstream webs { server 172.16 .1.7; server 172.16 .1.8;
}
server { listen 80 ; server_name wp.dezyan.com; location / { proxy_pass http://webs; include proxy_params; }
}
[ root@web01 nginx]
[ root@web01 nginx]
8.backup服务器配置rsync备份服务
1 .下载rsync服务
[ root@backup ~]
2 .修改配置文件
uid = dezyan
gid = dezyan
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log[ code]
path = /rsync/code[ imag]
path = /rsync/imag
3 .创建必要文件
[ root@backup ~]
[ root@backup ~]
[ root@backup ~]
uid = 666 ( dezyan) gid = 666 ( dezyan) groups = 666 ( dezyan)
[ root@backup ~]
[ root@backup ~]
[ root@backup ~]
[ root@backup ~]
4 .启动服务
[ root@backup ~]
[ root@backup ~]
5 .随便找一台机器测试
[ root@lb01 ~]
9.nfs服务器部署lsync服务监控动态文件目录,实时同步至backup
1 .安装
[ root@nfs ~]
2 .配置
[ root@nfs ~]
settings { logfile = "/var/log/lsyncd/lsyncd.log" ,statusFile = "/var/log/lsyncd/lsyncd.status" ,maxProcesses = 2 ,nodaemon = false,
}
sync { default.rsync,source = "/data/wordpress/" ,target = "rsync_backup@172.16.1.41::imag" ,delete = true,delay = 1 ,rsync = { binary = "/usr/bin/rsync" ,password_file = "/etc/rsyncd.pwd" ,archive = true,compress = true,}
}
3 .创建必要文件
[ root@nfs ~]
[ root@nfs ~]
4 .启动服务
[ root@nfs ~]
[ root@nfs ~]
5 .在backup查看是否已同步
[ root@backup ~]
drwxr-xr-x 3 dezyan dezyan 16 Dec 11 17 :52 /rsync/imag/2024
10.设置定时任务、脚本,实现定期备份代码
[ root@web01 ~]
[ root@web01 ~]
date = ` date +%F`
name = ` hostname `
dir = { $date } _{ $name }
tar zcf /tmp/$dir .tar.gz /code/*
export RSYNC_PASSWORD = 123
rsync -avz /tmp/$dir .tar.gz rsync_backup@172.16.1.41::code[ root@web01 ~]
00 00 * * * root sh /shellscripts/code.sh
11.在backup服务器部署nfs服务器,编写脚本,防止单点故障
1 .下载
[ root@backup ~]
2 .配置
[ root@backup ~]
/rsync/imag 172.16 .1.0/24( rw,sync,all_squash,anonuid= 666 ,anongid= 666 )
3 .启动
[ root@backup ~]
[ root@backup ~]
4 .查看
[ root@backup ~]
Export list for 172.16 .1.41:
/rsync/imag 172.16 .1.0/24
5 .编写脚本( web端都写)
[ root@web01 ~]
ping -c1 -W1 172.16 .1.31 &> /dev/null
if [ $? -ne 0 ] ; then
umount -lf /code/wordpress/wp-content/uploads &> /dev/null &
sleep 2
umount -lf /code/wordpress/wp-content/uploads &> /dev/null
mount -t nfs 172.16 .1.41:/rsync/imag /code/wordpress/wp-content/uploads
fi