自动化部署nginx

1、shell自动化部署nginx

#!/bin/bash

host_name=(
wyl01
wyl02
wyl03
)

host_ip=(
10.10.252.151 
10.10.252.112 
10.10.252.127
)

#nginx的部分信息
NGINX_VERSION='nginx-1.14.0'                             #nginx的版本
NGINX_INSTALL_PATH='/usr/local'                          #nginx的安装路径

function adduser(){  
  useradd -d /home/$1 $1
  expect -c"
  spawn passwd $1
  expect {
     \"*yes/no*\" {send \"yes\r\";exp_continue}
     \"*New password:*\" {send \"$2\r\";exp_continue}
     \"*Retype new password:*\" {send \"$2\r\";exp_continue}
}" 
}

function check_exist_user(){
echo_fun 5 检查$1用户是否存在
num_user=`cat /etc/passwd|grep -w $1|wc -l`
if [ ${num_user} -eq 1 ];then
   echo_fun 2 该机器已经创建了$1用户,无需再创建,继续下一步
   num_user=`echo 1`
else
   echo_fun 4 创建$1用户
   num_user=`echo 0`
fi
}


function check_catalog_exist(){
 test -d $1 && echo_fun 5 $1的安装目录已经存在,请检查 && exit
}


function check_package(){
package_num=`rpm -qa|grep $1|wc -l `

if [ $package_num -lt 1 ];then
  echo_fun 5 缺少$1的依赖包,安装依赖
  yum install -y $1
  check_ok
fi
}

#检查环境变量---单台
function check_etc_profile(){
echo_fun 4 配置环境变量
#检查环境变量中是否已经配置了
path_num=`cat /etc/profile |grep -w $1 |wc -l `
if [ ${path_num} -ge 1 ];then
   echo_fun 5 该机器环境变量中已经配置,请检查准确性
   #回滚,删除之前的软连接和安装包
   rm -rf $1
   rm -rf $2
   exit
fi
}


#解压ngxin的二进制包。
function extract_nginx(){
  cd ${SOFTWARE_PATH}
  echo_fun 4 解压nginx二进制包,并创建软连接
  tar -xf ${NGINX_VERSION}.tar.gz 
  cd ${NGINX_VERSION}
  ./configure --prefix=./configure --prefix=${NGINX_INSTALL_PATH}/nginx --user=nginx --group=nginx --error-log-path=/usr/local/nginx/error.log --pid-path=/usr/local/nginx/nginx.pid --http-log-path=/usr/local/nginx/access.log --with-debug
  check_ok
  make && make install
  check_ok
}


function start_nginx(){
echo_fun 4 启动nginx
cd ${NGINX_INSTALL_PATH}/nginx
nohup nginx >/home/${NGINX_USER}/nginx.log 2>&1
sleep 15s
nginx_pid_num=`ps -ef|grep  nginx|grep -v grep|wc -l`
if [ ${nginx_pid_num} -ge 1 ];then
 echo_fun 2 nginx has started....
fi
}



#nginx的部署
function step_fun_7(){
#检查nginx安装目录
check_catalog_exist ${NGINX_INSTALL_PATH}/nginx
#解压nginx
extract_nginx

echo_fun 5 请创建nginx的work进程启动用户[nginx]
read -p "nginx_user=" nginx_user

#检查是否存在nginx用户
check_exist_user ${nginx_user}

if [ ${num_user} -eq 0 ]; then
  echo_fun 5 给${nginx_user}用户输入密码
  read -p "nginx_passwd=" nginx_passwd
  adduser $nginx_user $nginx_passwd 
fi

check_package openssl-devel
check_package pcre-devel
check_package zlib-devel
check_package gcc

check_etc_profile ${NGINX_INSTALL_PATH}/nginx
if [ ${path_num} -lt 1 ];then
   echo -e '\nexport NGINX_HOME='${NGINX_INSTALL_PATH}'/nginx\nexport PATH=${NGINX_HOME}/sbin:$PATH'>> /etc/profile
   source /etc/profile
fi

#启动nginx
start_nginx
}

猜你喜欢

转载自blog.csdn.net/wyl9527/article/details/81019398