SHELL编程开发Nginx虚拟主机管理脚本精华版

#!/bin/bash

#2020年3月8日15:28:16

#auto config nginx vhosts

#by author lijingtao

#########################

NGX_VHOST="$*"

NGX_VER="1.16.0"

NGX_CNF="nginx.conf"

NGX_YUM="yum install -y"

NGX_DIR="/usr/local/nginx"

NGX_SOFT="nginx-${NGX_VER}.tar.gz"

NGX_URL="http://nginx.org/download"

NGX_SRC=$(echo $NGX_SOFT|sed 's/\.tar.*//g')

NGX_ARGS="--user=www --group=www --with-http_stub_status_module"

if [ $# -eq 0 ];then

      echo -e "\033[32m--------------------\033[0m" 

      echo -e "\033[32mUsage:{/bin/sh $0 v1.jf.com|v2.jf.com v3.jf.com|help}\033[0m"  

      exit 1

fi

if [ ! -d $NGX_DIR ];then

      #Install nginx web

      $NGX_YUM wget gzip make tar gcc

      $NGX_YUM pcre pcre-devel zlib-devel

      wget -c  $NGX_URL/$NGX_SOFT

      tar -xzf $NGX_SOFT

      cd $NGX_SRC

      useradd -s /sbin/nologin www -M

      ./configure --prefix=$NGX_DIR $NGX_ARGS

      make

      make install

      $NGX_DIR/sbin/nginx

      ps -ef|grep nginx

      netstat -tnlp|grep -w 80

      setenforce 0

      systemctl stop firewalld.service

fi

 

#Config nginx vhosts

cd $NGX_DIR/conf/

grep -ai "include vhosts" ${NGX_CNF} >>/dev/null

if [ $? -ne 0 ];then

      \cp ${NGX_CNF} ${NGX_CNF}.bak

      grep -vE "#|^$" ${NGX_CNF} >${NGX_CNF}.swp

      sed -i '/server/,$d' ${NGX_CNF}.swp

      echo -e "    include vhosts/*;\n}" >>${NGX_CNF}.swp

      \cp ${NGX_CNF}.swp ${NGX_CNF}

fi

 

mkdir -p vhosts

cd vhosts

 

for NGX_VHOST in $(echo $NGX_VHOST)

do

cat>$NGX_VHOST<<EOF

server {

        listen       80;

        server_name  $NGX_VHOST;

        location / {

            root   html/$NGX_VHOST;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

}

EOF

mkdir -p $NGX_DIR/html/$NGX_VHOST

cat>$NGX_DIR/html/$NGX_VHOST/index.html<<EOF

<html>

<h1>$NGX_VHOST Test Pages.</h1>

<hr color=red>

</html>

EOF

cat $NGX_VHOST

$NGX_DIR/sbin/nginx -t >>/dev/null 2>&1

if [ $? -eq 0 ];then

      echo -e "\033[32mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok\033[0m"

      echo -e "\033[32mnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful\033[0m"

      $NGX_DIR/sbin/nginx -s reload

fi

done

发布了14 篇原创文章 · 获赞 0 · 访问量 414

猜你喜欢

转载自blog.csdn.net/falnet/article/details/104736447