1. 程式人生 > >一鍵部署nginx虛擬主機

一鍵部署nginx虛擬主機

oca #### c-c org sof efi /dev/null and null

#2018年1月13日12:16:14
#auto install Nginx and create virtual Hosts
#by author xiaofan
#########################
#Install Nginx Server
NGINX_DIR="/usr/local/nginx/"
NGINX_SOFT="nginx-1.12.0.tar.gz"
NGINX_SRC=$(echo $NGINX_SOFT|sed ‘s/.tar.*//g‘)
NGINX_CNF="nginx.conf"
NGINX_URL="http://nginx.org/download/"
NGINX_DOMAIN="$1"

if [ $# -eq 0 ];then
echo -e "\033[32m-------------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 v1.xiaofan.com|v2.xiaofan.com}\033[0m"
exit 0
fi

if [ ! -d $NGINX_DIR ];then
yum install pcre-devel pcre gcc gcc-c++ glibc glibc-devel zlib zlib-devel openssl-devel openssl -y

wget -c $NGINX_URL/$NGINX_SOFT
tar -xzf $NGINX_SOFT
cd nginx-1.12.0
sed -i -e ‘s/1.12.0//g‘ -e ‘s/nginx\//JWS/g‘ -e ‘s/"NGINX"/"JWS"/g‘ src/core/nginx.h
useradd www
./configure --user=www --group=www --prefix=$NGINX_DIR --with-http_stub_status_module --with-http_ssl_module
make
make install
fi

#Config Nginx Virtual Host

cd $NGINX_DIR/conf/
NUM=grep -c "include vhosts" ${NGINX_CNF}
if [ $NUM -eq 0 ];then
grep -vE "#|^$" ${NGINX_CNF} >${NGINX_CNF}.swp
\cp ${NGINX_CNF}.swp ${NGINX_CNF}
sed -i ‘$i\include vhosts/*;‘ ${NGINX_CNF}
fi

mkdir -p $NGINX_DIR/conf/vhosts/
cat>$NGINX_DIR/conf/vhosts/$NGINX_DOMAIN<<EOF
server {
listen 80;
server_name $NGINX_DOMAIN;
location / {
root /data/www/$NGINX_DOMAIN;
index index.html index.htm;
}

}
EOF
#Create Nginx HTML DIR
mkdir -p /data/www/$NGINX_DOMAIN/
cat>/data/www/$NGINX_DOMAIN/index.html<<EOF
<html>
<h1><center>The First Test Nginx page.</center></h1>
<hr color="red">
<h2><center>$NGINX_DOMAIN</center></h2>
</html>
EOF
NUM1=ps -ef |grep -v grep|grep -v auto|grep -c nginx
if [ $NUM1 -eq 0 ];then
$NGINX_DIR/sbin/nginx
else
$NGINX_DIR/sbin/nginx -t >>/dev/null 2>&1
if [ $? -eq 0 ];then
$NGINX_DIR/sbin/nginx -s reload
else
echo "Please Check Nginx Service info. Exit."
exit 0
fi
fi``

一鍵部署nginx虛擬主機