Izhuceul 发表于 2018-8-26 11:16:25

SHELL 自动部署Nginx并设置虚拟主机

#!/bin/bash  
#2017-8-29 16:07:07
  
#by fengchenglangzi
  
#auto install nginx and vhost
  
###################
  
#定义变量
  
SOFT_DIR="/usr/local/src"
  
NGINX_DIR="/usr/local/nginx"
  
NGINX_SOFT="nginx-1.12.0.tar.gz"
  
NGINX_SOFT_NAME=$(echo $NGINX_SOFT|awk -F".tar" '{print $1}')
  
YUM="yum install -y"
  
PCRE_GCC="gcc gcc-c++ pcre pcre-devel openssl-devel"
  
###################
  
#安装Nginx
  
function Install_Nginx () {
  
      if [ ! -d $NGINX_DIR ];then
  
                $YUM $PCRE_GCC
  
                cd $SOFT_DIR
  
                if [ ! -d $SOFT_DIR/$NGINX_SOFT_NAME ];then
  
                        wget -c http://nginx.org/download/$NGINX_SOFT
  
                        tar -zxf $NGINX_SOFT
  
                fi
  
                cd $NGINX_SOFT_NAME
  
                sed -i -e 's/1.12.0//g' -e 's/nginx\//JWS/g' -e 's/"NGINX"/"JWS"/g' src/core/nginx.h
  
                useradd nginx;usermod -s /sbin/nologin nginx
  
                ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module\
  
--with-http_ssl_module && make -j8 && make install -j8
  
                if [ $? -eq 0 ];then
  
                        echo "Nginx安装完毕!!!"
  
                     /usr/local/nginx/sbin/nginx
  
                fi
  
      else
  
                echo "Nginx已经安装!!!"
  

  
      fi
  
}
  
####################
  
#创建虚拟主机
  
function Create_Vhost () {
  
      if [ ! -d $NGINX_DIR ];then
  
                echo "当前系统未安装Nginx,请先安装Nginx!!!"
  
      else
  
                read -p "请输入新的虚拟主机域名:" input
  
                DOMAIN_NAME=($(echo $input))
  
                i=0
  
                while [[ $i < ${#DOMAIN_NAME[@]} ]]
  
                do
  
                if [ ! -d $NGINX_DIR/conf/vhost/${DOMAIN_NAME} ];then
  
                mkdir -p $NGINX_DIR/conf/vhost/${DOMAIN_NAME}/{logs,html}
  
                touch $NGINX_DIR/conf/vhost/${DOMAIN_NAME}/nginx.conf
  
                sed -i "/include * mime.types/a\include $NGINX_DIR\/conf\/vhost\/${DOMAIN_NAME}/nginx.conf;" $NGINX_DIR/conf/nginx.conf
  
cat >> $NGINX_DIR/conf/vhost/${DOMAIN_NAME}/nginx.conf > $NGINX_DIR/conf/vhost/${DOMAIN_NAME}/html/index.html
页: [1]
查看完整版本: SHELL 自动部署Nginx并设置虚拟主机