1、自动安装nginx脚本,添加虚拟nginx配置文件

#!/bin/bash
	#2021-3-17 16:53:06
	#Automatically configure the NGINX virtual service
	#by author shinian
	#########################
	NGINX_VER="1.9.9"
	NGINX_CNF="nginx.conf"
	NGINX_SOFT="nginx-${NGINX_VER}.tar.gz"
	NGINX_DIR="/usr/local/nginx"
	NGINX_URL="http://nginx.org/download"
	NGINX_SRC=$(echo $NGINX_SOFT|sed 's/.tar.*//g')
	
	function nginx_help(){
echo -e "\033[33mNginx VIrtual Manager SHELL Scripts\033[0m" 
echo -e "\033[33m-------------------------------\033[0m" 
echo -e "\033[33m1)-A New Install Nginx WEB Server.\033[0m" 
echo -e "\033[33m2)-B v1.www.com|v2.www.com v3.www.com\033[0m" 
echo -e "\033[33m3)-C v1.www.com|v2.www.com v3.www.com\033[0m" 
echo -e "\033[35mUsage:{/bin/bash $0 -A(Install) -B(add) or -C(del) -H(help)\033[0m" 
exit 0	
}

function nginx_install(){
	#Nginx Install Config
	if [ ! -d ${NGINX_DIR} ];then
wget -c $NGINX_URL/$NGINX_SOFT
	tar -xzf $NGINX_SOFT
	yum install pcre-devel zlib-devel -y
	cd nginx-${NGINX_VER}
	./configure --prefix=${NGINX_DIR}/.
	make
	make install
	${NGINX_DIR}/sbin/nginx
	ps -ef|grep nginx
	netstat -tnlp|grep 80
	setenforce 0
	systemctl stop firewalld.service
	else
	
echo -e "\033[33m-----------------------\033[0m"
echo -e "\033[32mThe Nginx WEB Already Install,Please Exit.\033[0m" 

fi
}

function virtual_add(){
#Nginx Config Virtual Host
if [ $# -le 1 ];then
nginx_help
fi
cd ${NGINX_DIR}/conf/
grep "include domains" ${NGINX_CNF} >>/dev/null 2>&1
if [ $? -ne 0 ];then
	grep -vE "^$|#" ${NGINX_CNF} > ${NGINX_CNF}.swp
\cp ${NGINX_CNF}.swp ${NGINX_CNF}
sed -i '/server/,$d' ${NGINX_CNF}
echo -e -e "    include domains/*;\n}" >>${NGINX_CNF}
${NGINX_DIR}/sbin/nginx -t
mkdir domains -p
fi
shift 1
NUM=`ls domains/|grep -c $*`
if [ $NUM -eq 0 ];then
cat>domains/$*<<EOF
server {
    listen       80;
    server_name  $*;
    location / {
        root   html/$*;
        index  index.html index.htm;
    }
}
EOF
mkdir -p ${NGINX_DIR}/html/$*
cat>${NGINX_DIR}/html/$*/index.html<<EOF
$* Welcome to nginx!
<hr color=red>
EOF
echo -e "\033[32m-----------------------\033[0m" 
echo -e "\033[32mThe Nginx $* ADD Success.\033[0m" 
cat domains/$*
echo -e "\033[32m-----------------------\033[0m" 
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
echo
else
echo -e "\033[32m-----------------------\033[0m" 
echo -e "\033[32mThe Nginx $* Already Exist,Please Exit.\033[0m" 
cat domains/$*
exit 1
fi
}

function virtual_del(){
cd ${NGINX_DIR}/conf/domains/
shift 1
ls -l|grep "$*" 
if [ $? -eq 0 ];then 
 rm -rf $*
/usr/local/nginx/sbin/nginx -s reload
fi
}

case $1 in
-A)
nginx_install
;;
-B)
virtual_add $*
;;
-C)
virtual_del $*
;;
*)
nginx_help
esac