Nginx虚拟主机,自动添加,根据$1参数创建多个虚拟主机
[code]
#!/bin/bash
#2017年8月29日14:46:53
#By ying
#auto install nginx and create vhost
#———————————–#
NGINX_DIR=/usr/local/nginx
CONF=/usr/local/nginx/conf
V_DIR=/usr/local/nginx/conf/vhosts
V_HOSTS=$1
GET=”http://nginx.org/download/nginx-1.12.0.tar.gz”
MAKE=”./configure –prefix=/usr/local/nginx –with-http_realip_module –with-http_sub_module –with-http_gzip_static_module –with-http_stub_status_module –with-pcre”
#安装编译Nginx
function NGINX_INSTALL()
{
if [ ! -e $CONF ];then
cd /usr/local/src/
yum install -y pcre-devel pcre wget zlib-devel
wget $GET
tar zxf nginx-1.12.0.tar.gz
cd nginx-1.12.0
make clean
$MAKE
make
make install
fi
#创建虚拟主机
NUM=`grep “include vhosts\/\*\.conf” /usr/local/nginx/conf/nginx.conf|wc -l`
if [ $NUM == 0 ];then
sed -i ‘$i\include vhosts/*;’ $CONF
fi
if [ ! -d $V_DIR ];then
mkdir -p $V_DIR
fi
cat>$CONF/vhosts/$V_HOSTS<<EOF
server {
listen 80;
server_name $V_HOSTS;
location / {
root /www/$V_HOSTS/;
index index.html;
}
}
EOF
mkdir -p /www/$V_HOSTS/
cat>/www/$V_HOSTS/index.html<<EOF
<html>
<h1><center>The First Test Nginx page.</center></h1>
<hr color=”red”>
<h2><center>$V_HOSTS</center></h2>
</html>
EOF
#判断配置文件是否成功
$NGINX_DIR/sbin/nginx -t
if [ $? -eq 0 ];then
pkill nginx
$NGINX_DIR/sbin/nginx
else
echo “配置文件出错”
echo `$NGINX_DIR/sbin/nginx -t`
exit
fi
}
if [ -z $1 ];then
echo -e “\033[32m——————–\033[0m”
echo -e “\033[32mPlease enter sh $0 www.ying.com.\033[0m”
exit 0
fi
NGINX_INSTALL
[/code]