nginx批量分发脚本编写
[root@x112 vhost]# vim vhost.sh
#!/bin/bash
#describtion for install nginx virtualhost
ListenPort=81
Num=1
ServerName=abc
Count=$1
Path=/usr/local/nginx/
DocumentRoot=${Path}html/abc
if [ ! -d "${Path}conf/vhost" ];then
mkdir $Path/conf/vhost
fi
grep 'include vhost/*.conf' /usr/local/nginx/conf/nginx.conf
if [ $? -ne 0 ];then
sed -i_bak '/include mime.types/ainclude vhost/*.conf' $Path/conf/nginx.conf
fi
Dir_conf=`ls ${Path}conf/vhost/`
if [[ "$Dir_conf" =~ $ServerName[0-9]+.conf ]];then
rm -rf ${Path}conf/vhost/$ServerName*.conf
fi
for i in `seq $Count`
do
cat <<-eof >$Path/conf/vhost/$ServerName$Num.conf
server {
listen $ListenPort;
server_name www.$ServerName$Num.com;
charset utf-8;
location / {
root $DocumentRoot$Num;
index index.html index.htm;
# proxy_pass http://webserver;
}
}
eof
ListenPort=$[ListenPort+1]
Num=$[Num+1]
done
Num=1
Dir_html=`ls ${Path}html`
if [[ "$Dir_html" =~ ${ServerName}[0-9]+ ]];then
rm -rf ${DocumentRoot}*
fi
for i in `seq $Count`
do
if [ ! -d "$DocumentRoot$Num" ];then
mkdir $DocumentRoot$Num
fi
cat <<-eof >$DocumentRoot$Num/index.html
<h1>Welcome to mysite $ServerName$Num</h1>
<hr>
eof
Num=$[Num+1]
done
${Path}sbin/nginx -t
${Path}sbin/nginx -s reload
运行测试:
[root@x112 vhost]# sh -vx vhost.sh 5