基于Nginx负载均衡
nfs服务测试脚本
#!/bin/bash
#配置yum源
cd /
cp -rp /etc/yum.repos.d /
rm -rf /etc/yum.repos.d/*
cat >/etc/yum.repos.d/centos.repo<<EOF
[aaaaa]
name=aaaa
enabled=1
gpgcheck=0
baseurl=file:///mnt
EOF
#挂载镜像
mount /dev/sr0 /mnt
yum clean all
yum repolist
#安装nfs服务
yum install -y nfs-utils*
systemctl start rpcbind
systemctl start nfs
#解压软件包
mkdir /luntan
unzip Discuz_X3.2_SC_UTF8.zip -d /luntan
if [ $? -eq 0 ]
then
echo "已完成"
else
echo"压缩包未解压"
yum install -y unzip
unzip Discuz_X3.2_SC_UTF8.zip -d /luntan
fi
#创建共享目录,修改配置文件
cat >>/etc/exports <<EOF
/luntan *(rw,no_root_squash)
EOF
#重启nfs服务
systemctl restart nfs
#关闭防火墙及SELINUX
systemctl stop firewalld
systemctl disable firewalld
setenforce = 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
exit
apache端测试脚本
#!/bin/bash
#挂载镜像
cd /
mount /dev/sr0 /mnt
#配置本地yum源
cp -rp /etc/yum.repos.d/ .
rm -rf /etc/yum.repos.d/*
cat >/etc/yum.repos.d/centos.repo<<EOF
[aaaaa]
name=aaaa
enabled=1
gpgcheck=0
baseurl=file:///mnt
EOF
yum clean all
yum repolist
#安装httpd*,nfs*,php*
yum install -y httpd* nfs*
yum install -y php*
if [ $? -eq 0 ]
then
echo "php安装成功"
else
echo "php未成功安装,正在重新安装"
yum install -y php php-mysql
fi
#关闭防火墙及SELINUX
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#挂载共享目录NFS
mount -t nfs 192.168.112.136:/luntan /var/www/html/
#挂载后文件移动及赋权
cd /var/www/html/
mv /var/www/html/upload/* /var/www/html
chmod -R 757 data/ config/ uc*
#重启apache服务
systemctl restart httpd
exit
Nginx端测试脚本
配置nginx源(使用EOF写入时,baseurl部分出现缺失,需要单独配置)
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
Nginx主服务配置
#!/bin/bash
#关闭防火墙及SELINUX
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#获取阿里源
which wget
if [ $? -eq 0 ]
then
echo "wget已安装"
else
echo "wget未安装,开始安装wget"
yum install -y wget
fi
wget http://mirrors.aliyun.com/repo/Centos-7.repo
if [ $? -eq 0 ]
then
echo "已成功获取镜像"
else
echo "镜像获取失败"
read -ep "请输入主机IP:"a
read -ep "请输入网关:"b
sed -i '$i\ nameserver '$a' \n nameserver '$b' \n nameserver 114.114.114.114' /etc/resolv.conf
systemctl restart network
fi
#重新获取阿里源
yum clean all
yum repolist
#修改阿里源配置
sed -i 's/http/https/g' Centos-7.repo
sed -i 's/gpgcheck=1/gpgcheck=0/g' Centos-7.repo
#挂载镜像
mount /dev/sr0 /mnt
#安装并启动Nginx
yum install -y nginx
systemctl start nginx
#配置反向代理的apache主机
sed -i '15i\ upstream backend { \n server 主apacheIP:80;\n server 从apacheIP:80;\n }' /etc/nginx/nginx.conf
#配置Nginx开启反向代理访问
sed -i 's/index.html index.htm/forum.php/g' /etc/nginx/conf.d./default.conf
sed -i '10i\ proxy_pass http://backend;\n proxy_redirect default;\n proxy_set_header Host $host;\n proxy_set_header X-Real-IP $remote_addr;' /etc/nginx/conf.d/default.conf
#重启Nginx服务
systemctl restart nginx
#安装keepalived
yum install -y keepalived
#修改keepalived配置文件
cat /dev/null >/etc/keepalived/keepalived.conf
cat >/etc/keepalived/keepalived.conf<<EOF
global_defs {
router_id 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.112.100 #虚拟ip
}
}
EOF
#重启keepalived服务
systemctl restart keepalived
exit
Nginx从服务
#!/bin/bash
#关闭防火墙及SELINUX
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#获取阿里源
yum install -y wget
wget http://mirrors.aliyun.com/repo/Centos-7.repo
if [ $? -eq 0 ]
then
echo "已成功获取镜像"
else
echo "镜像获取失败"
sed -i '$i\ nameserver 从服务主机ip \n nameserver 192.168.112.2 \n nameserver 114.114.114.114' /etc/resolv.conf
systemctl restart network
fi
#重新获取阿里源
yum clean all
yum repolist
#修改阿里源配置
sed -i 's/http/https/g' Centos-7.repo
sed -i 's/gpgcheck=1/gpgcheck=0/g' Centos-7.repo
#挂载镜像
mount /dev/sr0 /mnt
#安装并启动Nginx
yum install -y nginx
systemctl start nginx
#配置反向代理的apache主机
sed -i '15i\ upstream backend { \n server 主apacheIP:80;\n server 从apacheIP:80;\n }' /etc/nginx/nginx.conf
#配置Nginx开启反向代理访问
sed -i 's/index.html index.htm/forum.php/g' /etc/nginx/conf.d./default.conf
sed -i '10i\ proxy_pass http://backend;\n proxy_redirect default;\n proxy_set_header Host $host;\n proxy_set_header X-Real-IP $remote_addr;' /etc/nginx/conf.d/default.conf
#重启Nginx服务
systemctl restart nginx
#安装keepalived
yum install -y keepalived
#修改keepalived配置文件
cat /dev/null >/etc/keepalived/keepalived.conf
cat >/etc/keepalived/keepalived.conf<<EOF
global_defs {
router_id 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.112.100 #虚拟ip
}
}
EOF
#重启keepalived服务
systemctl restart keepalived
exit
Mysql端测试脚本
需要配置raid5、主从复制
#主库配置
#!/bin/bash
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#配置raid5
which mdadm
if [ $? -eq 0 ]
then
echo "mdadm已安装"
else
echo "mdadm未安装"
yum install -y mdadm
fi
mdadm -C /dev/md5 -l 5 -n 3 -x 1 /dev/sd{b,c,d,e}
mkfs -t ext4 /dev/md5
#先创建myql文件,再挂载raid5,设置开机自动挂载
mkdir /var/lib/mysql
mount /dev/md5 /var/lib/mysql
sed -i '$i/dev/md5 /var/lib/mysql ext4 defaults 0 0' /etc/fstab
#下载mariadb,配置数据库文件
yum install -y mariadb*
sed -i '4i\server-id=1 \nlog-bin=mysql-bin' /etc/my.cnf#唯一id,开启二进制日志
#重启数据库服务,设置登入密码
systemctl restart mariadb
mysql_secure_installation<<EOF
123456
123456
EOF
#创建hz用户
mysql -p123456 -e "create user 'hz'@'%' identified by '123456';"
#授权用户
mysql -p123456 -e "grant replication slave on *.* to 'root'@'192.168.112.140' identified by '123456';"
#数据库授权,允许root远程登陆
mysql -p123456 -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"
#刷新权限
mysql -p123456 -e "flush privileges;"
#查看主库状态,从库开始配置
mysql -p123456 -e "show master status;"
echo "请手动到从库填写并开启复制"
mysql从库配置
#从库配置
#!/bin/bash
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#配置raid5
which mdadm
if [ $? -eq 0 ]
then
echo "mdadm已安装"
else
echo "mdadm未安装"
yum install -y mdadm
fi
mdadm -C /dev/md5 -l 5 -n 3 -x 1 /dev/sd{b,c,d,e}
mkfs -t ext4 /dev/md5
#先创建myql文件,再挂载raid5,设置开机自动挂载
mkdir /var/lib/mysql
mount /dev/md5 /var/lib/mysql
sed -i '$i/dev/md5 /var/lib/mysql ext4 defaults 0 0' /etc/fstab
#下载mariadb,配置数据库文件
yum install -y mariadb*
sed -i '4i\server-id=2 \n log-bin=mysql-bin \n relay_log=relaylogs' /etc/my.cnf
#唯一id,开启二进制日志,开启中继日志
#重启数据库服务,设置登入密码
systemctl restart mariadb
mysql_secure_installation<<EOF
123456
123456
EOF
登录MySQL从库,进行操作
mysql -uroot -p123456 -e "stop slave;"
mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='数据库主库IP',MASTER_USER='root',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE=' mysql-bin.000001', MASTER_LOG_POS=1914,MASTER_CONNECT_RETRY=10;"
mysql -uroot -p123456 -e "show slave status\G;"
CHANGE MASTER TO MASTER_HOST='数据库主库IP',MASTER_USER='hz',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE=' mysql-bin.000001', MASTER_LOG_POS=1914,MASTER_CONNECT_RETRY=10;
keepalived主库配置
#安装keepalived
yum install -y keepalived
#修改keepalived配置文件
cat /dev/null >/etc/keepalived/keepalived.conf
cat >/etc/keepalived/keepalived.conf<<EOF
global_defs {
router_id 3
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 2
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.112.200 #虚拟ip
}
}
EOF
#重启keepalived服务
systemctl restart keepalived
keepalived从库配置
#安装keepalived
yum install -y keepalived
#修改keepalived配置文件
cat /dev/null >/etc/keepalived/keepalived.conf
cat >/etc/keepalived/keepalived.conf<<EOF
global_defs {
router_id 4
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 2
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.112.200 #虚拟ip
}
}
EOF
#重启keepalived服务
systemctl restart keepalived