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

小柒博客 1 77,6925字数 426阅读1分25秒阅读模式

这篇文章主要为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

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
微信号已复制
微信公众号
微信扫一扫,关注我!
weinxin
公众号已复制
Shell最后更新:2024-1-24
小柒博客
  • 本文由 小柒博客 发表于 2017年12月23日 09:05:51
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.com/920.html
    • 运维老司机
      运维老司机 6

      亲测,写的不错,感谢博主 :razz:

    匿名

    发表评论

    匿名网友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    拖动滑块以完成验证