51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Nginx批量创建虚拟主机自动化脚本

这篇文章主要为Nginx批量创建虚拟主机脚本,需要的朋友可以参考下。

[root@localhost ~]# vim auto_config_nginx_virtual_host.sh

#!/bin/bash
#Date:2018-5-20 13:14:00
#Author Blog:
#	https://www.yangxingzhen.com
#	https://www.yangxingzhen.cn
#Author WeChat:
#	微信公众号:小柒博客
#Author mirrors site:
#	https://mirrors.yangxingzhen.com
#About the Author
#	BY:YangXingZhen
#	Mail:xingzhen.yang@yangxingzhen.com
#	QQ:675583110
#Auto config Nginx VirtualHost

#Define Nginx path variables
NGINX_URL=http://mirrors.yangxingzhen.com/nginx
NGINX_FILE=nginx-1.13.4.tar.gz
NGINX_FILE_DIR=nginx-1.13.4
NGINX_PREFIX=/usr/local/nginx


nginx_install () {
#Install Nginx Soft
if \[ ! -d $NGINX_PREFIX \];then
yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ wget
wget -c -P /src $NGINX_URL/$NGINX_FILE
tar zxf /src/$NGINX_FILE -C /src
cd /src/$NGINX_FILE_DIR
sed -i 's/1.13.4/ /;s/nginx//nginx/' /src/nginx-1.13.4/src/core/nginx.h
useradd -s /sbin/nologin www
./configure --prefix=$NGINX_PREFIX 

--user=www 

--group=www 

--with-http_ssl_module 

--with-http_stub_status_module
if \[ $? -eq 0 \];then
make \&\& make install
echo -e "\\033\[32m NGINX Install success \\033\[0m"
else
echo -e "\\033\[32m NGINX Install fail,please check \\033\[0m"
exit 0
fi
else
echo -e "\\033\[32m Nginx has been installed \\033\[0m"
exit 0
fi
}


nginx_Virtual_Host () {
read -p "Please Enter Server_name:" HOST
if \[ -z $HOST \];then
echo -e "\\033\[32m Please Enter xiaoqi.com\|xiaoqi.com admin.com \\033\[0m"
exit 0
fi


NUM=`grep -c "include vhosts/*" $NGINX_PREFIX/conf/nginx.conf`
if \[ $NUM -eq 0 \];then
sed -i '$i\\tinclude vhosts/\*;' $NGINX_PREFIX/conf/nginx.conf
if \[ ! -d $NGINX_PREFIX/conf/vhosts \];then
mkdir -p $NGINX_PREFIX/conf/vhosts
fi			
fi


for i in $HOST
do
cat \>$NGINX_PREFIX/conf/vhosts/$i \<\<EOF
server {
listen       80;
server_name  $i;
location / {
root   /data/www/$i;
index  index.html index.htm;
}
}
EOF
if \[ ! -d /data/www/$i \];then
mkdir -p /data/www/$i
fi
cat \>/data/www/$i/index.html \<\<EOF
this is a $i server
EOF
echo "127.0.0.1 $i" \>\>/etc/hosts
done
ln -sf $NGINX_PREFIX/sbin/nginx /usr/bin
nginx -t \>/dev/null 2\>\&1
if \[ $? -eq 0 \];then
nginx
fi
for k in $HOST
do
curl $k
done
}


PS3="Please Enter select Install Menu\[1-3\]:"
select i in Install_Nginx-1.13.4 Nginx_Virtual_Host quit
do
case $i in
Install_Nginx-1.13.4)
nginx_install
;;
Nginx_Virtual_Host)
nginx_Virtual_Host $HOST
;;
quit)
echo -e "\\033\[33m Exit select Menu. \\033\[0m"
exit 0
esac
done


继续阅读 Shell最后更新:2024-1-24

赞(1)
未经允许不得转载:工具盒子 » Nginx批量创建虚拟主机自动化脚本