负载均衡+LNMP+rsync+NFS+lsync部署流程

负载均衡+LNMP+NFS+rsync+lsync部署流程

文章目录

服务器准备

主机名 公网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业务;

sh 复制代码
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

sh 复制代码
1.创建虚拟用户dezyan
[root@nfs ~]# groupadd -g666 dezyan
[root@nfs ~]# useradd -g666 -u666 -M -s /sbin/nologin dezyan
[root@nfs ~]# id dezyan 
uid=666(dezyan) gid=666(dezyan) groups=666(dezyan)
2.安装nfs服务,并进行配置
[root@nfs ~]# yum install -y nfs-utils
[root@nfs ~]# cat /etc/exports
/data/wordpress 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3.创建必要文件,并启动
[root@nfs ~]# mkdir -p /data/wordpress
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl enable  nfs
4.检查
[root@nfs ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data/wordpress 172.16.1.0/24
5.更改属主属组
[root@nfs ~]# chown -R dezyan.dezyan /data/wordpress/

2.db01服务器配置存放静态资源的数据库服务

sh 复制代码
1.安装mariadb服务,并启动
[root@db01 ~]# yum install -y mariadb-server
[root@db01 ~]# systemctl start mariadb
[root@db01 ~]# systemctl enable  mariadb
2.设置数据库密码
[root@db01 ~]# mysqladmin password 'dzy123.com'
3.设置远程登录账户
[root@db01 ~]# mysql -uroot -pdzy123.com
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 ~]# yum install -y mariadb-server
[root@web02 ~]# yum install -y mariadb-server
[root@web02 ~]# mysql -h172.16.1.51  -udzy  -pdzy123.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.

3.web两台服务器部署nginx+PHP服务

sh 复制代码
#两台机器步骤完全一样,此处只以web01演示配置过程
1.创建虚拟用户dezyan
[root@web01 ~]# groupadd -g 666 dezyan
[root@web01 ~]# useradd -g666 -u666 -M -s /sbin/nologin dezyan
[root@web01 ~]# id dezyan
uid=666(dezyan) gid=666(dezyan) groups=666(dezyan)

2.安装nginx服务,修改配置,启动服务
#安装
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[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 ~]# yum install -y nginx
#改配置
[root@web01 ~]# cd /etc/nginx/
[root@web01 nginx]# vim nginx.conf
user  dezyan;
#启动
[root@web01 nginx]# systemctl start nginx
[root@web01 nginx]# systemctl enable  nginx

3.安装PHP,修改配置,启动服务
#安装
[root@web01 ~]# yum -y install php php-bcmath php-cli php-common php-devel php-embedded php-fpm php-gd php-intl php-mbstring php-mysqlnd php-opcache php-pdo   php-process php-xml php-json
#改配置
[root@web01 ~]# vim /etc/php-fpm.d/www.conf
 24 user = dezyan
 26 group = dezyan
 38 listen = 127.0.0.1:9000
#启动
[root@web01 ~]# systemctl  start php-fpm
[root@web01 ~]# systemctl enable  php-fpm
#检查端口有本地9000端口即可

4.web两台服务器编写业务配置文件,创建代码目录并修改其属主组,上传代码

sh 复制代码
#两台机器步骤完全一样,此处只以web01演示配置过程
#编写业务配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/wp.conf
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 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx
#创建代码目录,上传代码
[root@web01 ~]# mkdir /code/wordpress -p
[root@web01 ~]# cd /code/wordpress/
[root@web01 wordpress]# wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz
[root@web01 wordpress]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@web01 wordpress]# rm -rf wordpress-5.0.3-zh_CN.tar.gz
[root@web01 wordpress]# mv wordpress/* .
#修改代码目录属主属组
[root@web01 ~]# chown -R dezyan.dezyan /code/wordpress/

5.浏览器访问,并进行"数据库的打通"配置(两台web都做)

  • 按照下表填写
数据库名 wordpress
用户名 dzy
密码 dzy123.com
数据库主机 172.16.1.51
表前缀 wp_

6.将nfs的/data/wordpress挂载到业务存储动态资源的位置

sh 复制代码
#两台机器步骤完全一样,此处只以web01演示配置过程
#创建动态文件存放目录
mkdir -p /code/wordpress/wp-content/uploads/
mount -t nfs 172.16.1.31:/data/wordpress /code/wordpress/wp-content/uploads/
#设置开机自动挂载
[root@web01 ~]# vim /etc/fstab
172.16.1.31:/data/wordpress     /code/wordpress/wp-content/uploads/     nfs defaults 0 0

7.lb01配置负载均衡

sh 复制代码
1.创建虚拟用户dezyan
[root@web01 ~]# groupadd -g 666 dezyan
[root@web01 ~]# useradd -g666 -u666 -M -s /sbin/nologin dezyan
[root@web01 ~]# id dezyan
uid=666(dezyan) gid=666(dezyan) groups=666(dezyan)

2.安装nginx服务,修改配置,启动服务
#安装
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[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 ~]# yum install -y nginx
#改配置
[root@web01 ~]# cd /etc/nginx/
[root@web01 nginx]# vim nginx.conf
user  dezyan;
#配置代理
#反向代理优化文件
vim /etc/nginx/proxy_params
	    proxy_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 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# vim lb.conf
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;		#使用proxy_pass模块将请求转发给地址池
        include proxy_params;
        }
}

#启动
[root@web01 nginx]# systemctl start nginx
[root@web01 nginx]# systemctl enable  nginx

8.backup服务器配置rsync备份服务

sh 复制代码
1.下载rsync服务
[root@backup ~]# yum install -y rsync
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 ~]# groupadd -g 666 dezyan
[root@backup ~]# useradd -g 666 -u 666 -M -s /sbin/nologin dezyan
[root@backup ~]# id dezyan
uid=666(dezyan) gid=666(dezyan) groups=666(dezyan)
[root@backup ~]# echo rsync_backup:123 > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd 
[root@backup ~]# mkdir -p /rsync/code  /rsync/imag
[root@backup ~]# chown -R dezyan.dezyan /rsync/
4.启动服务
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable  rsyncd
5.随便找一台机器测试
[root@lb01 ~]# rsync -avz /etc/hosts rsync_backup@10.0.0.41::code

9.nfs服务器部署lsync服务监控动态文件目录,实时同步至backup

sh 复制代码
1.安装
[root@nfs ~]# yum install -y lsyncd
2.配置
[root@nfs ~]# vim /etc/lsyncd.conf
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 ~]# echo 123 > /etc/rsyncd.pwd
[root@nfs ~]# chmod 600 /etc/rsyncd.pwd
4.启动服务
[root@nfs ~]# systemctl start lsyncd
[root@nfs ~]# systemctl enable  lsyncd
5.在backup查看是否已同步
[root@backup ~]# ll -d /rsync/imag/*
drwxr-xr-x 3 dezyan dezyan 16 Dec 11 17:52 /rsync/imag/2024

10.设置定时任务、脚本,实现定期备份代码

sh 复制代码
#两台web机器步骤完全一样,此处只以web01演示配置过程
[root@web01 ~]# mkdir /shellscripts
[root@web01 ~]# vim /shellscripts/code.sh
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 ~]# vim /etc/crontab
00 00 * * * root sh /shellscripts/code.sh

11.在backup服务器部署nfs服务器,编写脚本,防止单点故障

sh 复制代码
1.下载
[root@backup ~]# yum install -y nfs-utils
2.配置
[root@backup ~]# vim /etc/exports
/rsync/imag 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3.启动
[root@backup ~]# systemctl start nfs
[root@backup ~]# systemctl enable nfs
4.查看
[root@backup ~]# showmount -e 172.16.1.41
Export list for 172.16.1.41:
/rsync/imag 172.16.1.0/24
5.编写脚本(web端都写)
[root@web01 ~]# vim  /shellscripts/shellneterror.sh
#!/bin/bash
ping -c1 -W1 172.16.1.31 &>/dev/null #或者使用
#showmount -e 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
相关推荐
dessler18 分钟前
RabbitMQ-镜像队列(Mirrored Queues)
linux·运维·rabbitmq
瑾曦22 分钟前
Docker相关命令
linux
发抖吧小喵喵25 分钟前
rpm包直接安装新系统缺少依赖问题处理
linux·运维·服务器
Asuicao1 小时前
最新docker国内镜像源地址大全
运维·docker·容器
xhdll1 小时前
embodied复现所需docker环境配置粗略流程
运维·docker·容器
码农101号1 小时前
Linux中Docker Swarm介绍和使用
linux·spring cloud·docker
Nazi61 小时前
dockerfile基础
linux·运维·docker·容器·云计算
所念皆为东辞1 小时前
elk部署加日志收集
linux·elk·elasticsearch·centos
TLucas2 小时前
Centos 7部署.NET 8网站项目
linux·nginx·postgresql·centos·.net