自动部署Nginx和nfs并架设Nginx集群脚本

本人经过多次尝试,简单完成了自动部署Nginx和nfs脚本,并且能够自动部署web反向代理集群,下面详细的阐述一下本人的思路。(以下脚本本人处于初学阶段,写的并不是很完善,所以需要后期进行整理和修正,请高手能够多多指教。)

本脚本需要注意的是:

1、这是针对centOS6.8,32位操作系统写的脚本文件,如果想在cenOS7中运行,就需要有些改动

2、这个脚本需要先安装代理服务器部分,再安装反向代理服务器,因为涉及到共享文件夹挂载的问题,所以需要有先后顺序;

3、今后本人会对此脚本进行更新和完善,希望朋友们来相互交流。

#!/bin/bash 
login=0
server_leader='192.168.1.104'               #设置代理服务器IP地址;
server_web1='192.168.1.105:80 weight=3'     #设置第一台反向代理服务器地址并设置权重参数;
server_web2='192.168.1.106:80'              #设置第二台反向代理服务器地址;
server_web3='192.168.1.107:80'              #设置第三台反向代理服务器地址;
server_nfs='192.168.1.0'                    #设置nfs共享配置文件的网络地址;

function install_nginx(){                                                                         #命名安装nginx的函数;
    yum -y install epel-release;                                                                  #安装epel扩展源(因为安装Nginx服务需要epel扩展源);
    yum -y install nginx ;                                                                        #安装Nginx服务;
    result=`echo $?`;                                                                             #将安装是否成功的结果返回给result变量;
        if (( $result == 0 ));                                                                    #判断返回结果是不是成功
            then
                echo 'Congratulations!!';
                echo 'Now will be starting the service,please wait...';
chkconfig nginx on; #将nginx设置为开机启动 service nginx start; #安装完成后启动Nginx服务; start_result=`echo $?`; #将启动结果存入变量start_result; if (( $start_result == 0 )); #判断启动结果是否成功; then cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak #将nginx.conf配置文件备份; echo 'The service is started successful'; echo 'Install nginx successful,the system will be exit,ByeBye!!'; else echo 'Please find some error to reinstall.ByeBye!!'; break; fi else echo 'Please find some error to reinstall.ByeBye!!'; fi } function install_nfs(){ #创建安装nfs服务的函数; yum -y install rpcbind nfs-utils #安装rpcbind和nfs-utils服务; result=`echo $?` #将安装结果放入result变量; if (( $result == 0 )); #判断安装结果是否成功; then echo '******************Congratulations!!************************'; echo '******Now will be starting the service,please wait...******';
chkconfig rpcbind on; #将rpcbind设置为开机启动
chkconfig nfs on; #将nfs设置为开机启动 service rpcbind restart; #启动rpcbind服务;
service nfs restart; #启动nfs服务; start_result=`echo $?`; #将启动结果存入start_result; if (( $start_result == 0 )); #判断启动结果是否成功; then echo '*************The service is started successful*************'; echo '**Install nfs successful,the system will be exit,ByeBye!!**'; else echo '*******Please find some error to reinstall.ByeBye!!*******'; break; fi else echo '************Please find some error to reinstall.**********'; fi } function set_nginx(){ #创建设置nginx服务函数; sed -ri "s/(http {)/http { upstream Mysite{ server $server_web1; server $server_web2; server $server_web3; } server { server_name $server_leader; listen 80; location /{ proxy_pass http://Mysite; } }/" /etc/nginx/nginx.conf #修改配置文件/etc/nginx/nginx.conf配置文件 } function set_nfs(){ #创建设置nfs的函数; mkdir /share #创建共享目录share; echo 'test web server!!' > /share/index.html #往share目录中写入文件index.html; echo "/share $server_nfs/24(rw,sync,fsid=0)" > /etc/exports #创建nfs共享配置文件并将配置写入文件; chmod o=rwx /share #给共享配置文件添加读写执行权限; /etc/init.d/rpcbind restart #重启rpcbind服务; /etc/init.d/nfs restart #重启nfs服务,使配置文件生效; } function set_web(){ #创建设置反向代理服务器函数; mount -t nfs $server_leader:/share/ /usr/share/nginx/html/ #将共享目录挂载到反向代理服务器的主目录中; /etc/init.d/nginx restart #重启nginx服务使配置生效; } while (( $login == 0 )) do echo '+===================================================+' echo '| Welcome to my Nginx auto install system!!! |' echo '| 1.install ordinary web server |' echo '| 2.install proxy web server |' echo '| 3.exit |' echo '+===================================================+' read -p 'Please chose your number:' chose #可以选择1:安装反向代理服务器(也就是普通的web服务器);2:安装代理服务器;3:退出系统 if [ ! $chose ] then continue fi if (( $chose == 1 )) then echo 'Your choice is number 1,the ordinary web server will be install,Are you sure?' read -p 'y/n:' pd if [ "$pd" == 'y' ]; then echo $pd install_nginx; set_web; fi if [ "$pd" == 'n' ]; then continue; fi elif (( $chose == 2 )) then echo 'Your choice is number 2,the proxy web server will be install,Are you sure?' read -p 'y/n:' pd2 if [ "$sp2" = 'y' ]; echo $sp2 then echo $pd2 install_nginx; install_nfs; set_nginx; set_nfs; fi if [ "$pd2" = 'n' ] then continue fi elif (( $chose == 3 )) then echo 'Thank you use my system,ByeBye.' break else continue fi done exit

  

原文地址:https://www.cnblogs.com/mojiexiaolong/p/6623291.html