自动部署nginx nfs 服务
服务端上传安装配置脚本:
######服务端安装脚本 ##!/usr/bin/bash nsinstall="yum install rpcbind nfs-utils -y" epelinstall="yum install epel-release -y" installnginx="yum install nginx -y" #stop nginx server function stopnx(){ systemctl stop nginx } #restart nginx server function restartnx(){ systemctl restart nginx } #restart nfs server function nfsrestart(){ systemctl start nfs } #reload nginx server function reloadnx(){ systemct reload nginx } function initalconfig(){ systemctl stop firewalld setenforce 0 } #nginx install epel install function epelcheck(){ rpm -qa | grep epel if [ $? != 0 ] then $epelinstall else echo "epel alread install" fi } function nginstall(){ rpm -qa | grep nginx if [ $? != 0 ] then $installnginx else echo "nginx already exist" fi sleep 3 } ####nfs-utlis rpcbind install function nfsinstall(){ rpm -qa | grep nfs-utils if [ $? != 0 ] then $nsinstall else echo "nfs rpcbind alread install" fi } #server restart block #服务启动模块 function ngstart(){ systemctl status nginx | grep running if [ $? != 0 ] then restartnx echo "nginx server restart server successfully" else echo "nginx server alread running" fi } function rpcbindrst(){ restart=`systemctl start rpcbind` systemctl status rpcbind | grep running if [ $? != 0 ] then $restart echo "restart rpcbind successfully!" else echo "rpcbind alread running" fi } function nfserverrst(){ nfsrestartserver=systemctl status nfs-utils |grep dead if [ $? != 0 ] then nfsrestartserver echo "restart nfs server successfully!!" else echo "server alread running" fi } #read -p "please input nginx master serverIP address: " server1 #read -p "please entry nginx second serverIP address: " server2 #read -p "please entry nginx third serverIP address: " server3 #Reverse proxy configure
####配置模块开始 function ngconfigure(){ sedconfig="sh /opt/sed.sh" #调用sed修改配置文件脚本 egrep "upstream" /etc/nginx/nginx.conf if [ $? != 0 ] then $sedconfig else echo "configure file already modify" fi } echo "#################nfs shared input################ " #调用mount挂载脚本 function nfsshared(){ nfssha="sh /opt/nfsservermountcfg.sh" #调用NFS挂载脚本 $nfssha } #check epel epelcheck #nginx install nginstall #nginx server restart ngstart #nfs install nfsinstall #rpcbind restart rpcbindrst #nfs server restart nfsrestart #nginx configure ngconfigure #nfs shared nfsshared ##############sed 修改脚本开始 ################################# #!/usr/bin/bash read -p "please input nginx master serverIP address: " server1 read -p "please entry nginx second serverIP address: " server2 read -p "please entry nginx third serverIP address: " server3 function restart(){ systemctl restart nginx echo "restart nginx successfully!" } sed -ri "/http \{/a\ \ \ \ upstream tonycloud\{\n\ \ \ \ \ \ \ \ server $server1 weight=3;\n\ \ \ \ \ \ \ \ server $server2;\n\ \ \ \ \ \ \ \ server $server3; \}" /etc/nginx/nginx.conf sed -ri '/^#/d' /etc/nginx/nginx.conf | sed -r '/^$/d' sed –i "/location \\/ {/a\ \ \ \ \ \ \ \ \ \ \ \ proxy_pass http://tonycloud;\n" /etc/nginx/nginx.conf restart ######修改脚本结束###### ######配置模块结束######
####nfs nginx 客户端安装脚本
#将脚本上传到客户端直接执行
#!/usr/bin/bash
firewalstop="systemctl stop firewalld"
selinxcl="setenforce 0"
cmd3="touch /opt/3.txt"
###########################################################inital########################
firewalstop="systemctl stop firewalld"
selinxcl="setenforce 0"
##########客户端安装nfs nginx rpc模块
######################client install nginx rpcbind ######################
remoteistepel="yum install epel-release -y"
remoteistnginx="yum install nginx -y"
remoterstnginx="systemctl start nginx"
remoteistrpc="yum install rpcbind -y "
remoterstrpc="systemctl start rpcbind.service"
#初始化函数(关闭防火墙 SELINUX)
function checkfw(){
firewd="systemctl stop firewalld"
echo "check firewall"
systemctl status firewalld | grep running
if [ $? = 0 ]
then
$firewd
echo "success close firewall"
else
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~firewall alread close~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
fi
}
#epel check
function epelcheck(){
rpm -qa | grep epel
if [ $? = 0 ]
then
echo "##############################install epel ##############################################################"
$remoteistepel
else
echo "epel alread install "
fi
}
#nginx install
function nginstall(){
rpm -qa | grep nginx
if [ $? != 0 ]
then
echo "##############install nginx ############################"
$remoteistnginx && $remoterstnginx
else
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~nginx already exist~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
fi
sleep 3
}
#RPC 安装模块
function rpcinstall(){
rpm -qa | grep rpcbind
if [ $? != 0 ]
then
echo "#######################install rpcbind ################################"
$remoteistrpc && $remoterstrpc
else
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~client rpcbind already exist~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
fi
sleep 3
}
####客户端挂载模块
str=$(printf "%-110s" "#")
function shreadstore(){
filecon=`cat /etc/exports`
sharedirectory="/etc/exports"
if [ -s $sharedirectory ]
then
echo "$sharedirectory not empty >>>>>>>>>>> contents is $filecon"
else
echo "${str// /#}"
echo "example: 192.168.100.0/24 "
read -p "please input nfs store shared internet IP segment:" sharedIP
echo "/share $sharedIP(rw,sync,fsid=0)" >> $sharedirectory
echo "successfully cofigure nfs store file ,please look /etc/exports files "
sleep 3
fi
}
function clientmount(){
echo "remote host mount nfs store:"
echo "default mount /opt"
echo "example: 192.168.100.134"
read -p "please input nfsServer IP address: " server1
mountcmd="mount -t nfs $server1:/share /opt"
# ssh $server2 ${mountcmd}
${mountcmd}
echo "$mountcmd mount successfully coming out "
sleep 3
exit
}
shreadstore
clientmount
checkfw
nginstall
rpcinstall
#nfs shared ip segment
shreadstore
#nfs mount
clientmount
exit
######################client install done##############################################
##################服务监控模块,只要监控服务 stop 则立刻start###
#!/usr/bin/bash nginxst="systemctl start nginx" date1=`date +%F\ \%H:%M:%S` nfsst="systemctl start nfs" #nginx server monitor #服务监控模块开始 function nginxmonitor(){ while : do systemctl status nginx | grep running >> /dev/null if [ $? -ne 0 ] then echo "${date1} ##########nginx server will start" >> /opt/nginxmonitor.log sleep 0.3 ${nginxst} else echo "${date1} ::::: nginx server alread start..... " >> /opt/nginxmonitor.log sleep 3 clear fi done } function nfsmonitor(){ while : do systemctl status nfs | grep dead >> /dev/null if [ $? -eq 0 ] then echo "${date1} ########## nfs server will start " >> /opt/nfsmonitor.log ${nfsst} sleep 0.3 else echo "${date1} ::::: nfs server alread start..... " >> /opt/nfsmonitor.log sleep 3 clear fi done } nginxmonitor nfsmonitor ##################服务监控模块结束