首页 > 代码库 > 十五.编写自动脚本部署web,反向代理,nfs服务

十五.编写自动脚本部署web,反向代理,nfs服务

一.自动部署web,反向代理,加权轮询:

1.编写脚本自动部署反向代理,web,nfs.

#!/bin/bash

#author/dengsiyuan

function nginx_check()  #配置nginx   

{

ngx_status= ‘ps aux|grep -v grep|grep -c nginx‘ #取运行nginx进程的进程数

if [ -f /etc/nginx/nginx.conf ];then   #判断是否存在正规文件nginx.conf以判断nginx是否安装

    echo ‘nginx has been installed‘

 

else

    yum clean all;

    yum install epel-release -y && yum install nginx -y;  # 安装epelnginx

    echo ‘nginx successfully installed‘

fi

systemctl stop firewalld;   #关闭防火墙

if [ $ngx_status -lt 2 ];then  #nginx运行时有一个子进程一个父进程,进程数为2

   echo ‘nginx is not running‘

   systemctl start nginx.service;

else

   echo ‘nginx is running‘

fi

systemctl enable nginx.service; #开机启动nginx

 

 

}

function rewrite_nginx_conf()   #配置nginx.conf文件

{

    code1=‘upstream yuan { \n\t\t serve 192.168.58.131:80 weight=3; \n\t\t serve 192.168.58.132:80; \n\t\t serve 192.168.58.134:80; }‘

    sed -ri "/^http/a\ \t $code1" /etc/nginx/nginx.conf;

    sed -ri "/^ *location \/ \{$/a\ \t proxy_pass http://yuan\;" /etc/nginx/nginx.conf;

    systemctl reload nginx.service;#重新载入配置文件

 

 

}

function nfs_web()  #配置nfs服务端

{

if [ -d /var/lib/nfs ];then    #判断nfs是否已安装

    echo ‘rpcbind nfs-untils has been installed‘

    exit

else

    yum clean all;

    yum install rpcbind.service -y;    # 安装nfs,rpcbind 软件包

    yum install nfs-untils -y;

    echo ‘rpcbind nfs successfully installed‘

fi

if [ ! -d /share ];then   #判断共享目录是否存在

    mkdir -p /share;      #建共享目录

    chmod -R o+x /share;  # 给客户端的执行权限,可以在客户端进行读写

    echo ‘/share 192.168.58.0/24(rw,sync,fisd=0)‘ > /etc/exports; #配置exports文件

fi

systemctl start rpcbind.service; && systemctl start nfs-utils;  #依次启动rpcbind nfs

systemctl enable rpcbind.service; && systemctl enable nfs-utils;

gshare= ` showmount -e `; #查看共享是否成功

                                                                                                       back=`echo $?`

if [ $back -eq 0 ];then

    echo ‘nfs_web is successful,the share dirctory is ok now‘

    exit

else

    echo ‘share failed‘

fi

 

}

function nfs_client()   #客户端安装rpcbind ,nfs

{

if [ -d /var/lib/nfs ];then

    echo ‘rpcbind nfs-untils has been installed‘

    exit

else

    yum clean all;

    yum install rpcbind.service -y;

    yum install nfs-untils -y;

    echo ‘rpcbind nfs successfully installed‘

fi

systemctl start rpcbind.service; && systemctl start nfs-utils;

systemctl enable rpcbind.service; && systemctl enable nfs-utils;

showmount -e 192.168.58.134; #查看服务器是否共享目录成功

return=`echo $?`

if [ $return -eq 0 ];then

    mount 192.168.58.134:/share /usr/share/nginx/html;#将共享目录挂载到客户端上

else

    ‘mount failed‘

    exit

fi

 

}

十五.编写自动脚本部署web,反向代理,nfs服务