这篇文章主要为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
微信扫一扫,加我!
微信号已复制
微信公众号
微信扫一扫,关注我!
公众号已复制
广东省深圳市南山区 电信 1F
亲测,写的不错,感谢博主