sh nginx 虚拟主机安装卸载

#!/bin/bash
#2021年3月10日22:07:13
#auto config nginx virtual hosts
#by author www.jfedu.net
########################
NGX_VHOSTS=""
NGX_VER="1.18.0"
NGX_CNF="nginx.conf"
NGX_YUM="yum install -y"
NGX_DIR="/usr/local/nginx"
NGX_SOFT="nginx-${NGX_VER}.tar.gz"
NGX_URL="http://nginx.org/download"
NGX_SRC=$(echo $NGX_SOFT|sed 's/.tar.*//g')
NGX_ARGS="--user=www --group=www --with-http_stub_status_module"

function install_nginx(){
if [ ! -f $NGX_DIR/sbin/nginx ];then
#Install Nginx WEB
$NGX_YUM wget gzip tar make gcc
$NGX_YUM pcre pcre-devel zlib-devel
wget -c $NGX_URL/$NGX_SOFT -P /usr/src/
cd /usr/src/
ls -l $NGX_SOFT
tar -xzvf $NGX_SOFT
cd $NGX_SRC/
useradd -s /sbin/nologin www -M
./configure --prefix=$NGX_DIR/ $NGX_ARGS
make -j4
make -j4 install
ls -l $NGX_DIR/
$NGX_DIR/sbin/nginx
ps -ef|grep -aiwE nginx
netstat -tnlp|grep -aiwE 80
setenforce 0
systemctl stop firewalld.service
fi
}

function add_vhosts(){
#Config Nginx virtual hosts
cd $NGX_DIR/conf/
grep -aiE "include vhosts" $NGX_CNF
if [ $? -ne 0 ];then
\cp $NGX_CNF $NGX_CNF.bak
sed -i -e '/#/d' -e '/^$/d' $NGX_CNF
sed -i '/server/,$d' $NGX_CNF
echo -e " include vhosts/;\n}" >>$NGX_CNF
mkdir -p vhosts
fi
cd vhosts
for NGX_VHOSTS in $

do
cat>$NGX_VHOSTS<<-EOF
server {
listen 80;
server_name $NGX_VHOSTS;
location / {
root html/$NGX_VHOSTS;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
EOF
cat $NGX_VHOSTS
echo -e "\033[32m-------------------\033[0m"
echo -e "\033[32mThe $NGX_VHOSTS add success.\033[0m"
mkdir -p $NGX_DIR/html/$NGX_VHOSTS/
cat>$NGX_DIR/html/$NGX_VHOSTS/index.html<<-EOF
<h1>$NGX_VHOSTS Test Pages.</h1>
<hr color=red>
EOF
$NGX_DIR/sbin/nginx -t
$NGX_DIR/sbin/nginx -s reload
done
}

function remove_vhosts(){
#Delete Nginx virtual hosts
cd $NGX_DIR/conf/vhosts/
for NGX_VHOSTS in $*
do
ls -l $NGX_VHOSTS
cat $NGX_VHOSTS
rm -rf $NGX_VHOSTS
rm -rf $NGX_DIR/html/$NGX_VHOSTS/
echo -e "\033[32m-------------------\033[0m"
echo -e "\033[32mThe $NGX_VHOSTS remove success.\033[0m"
$NGX_DIR/sbin/nginx -t
$NGX_DIR/sbin/nginx -s reload
done
}

case $1 in
-i|-I)
install_nginx
;;
-a|-A)
shift 1
add_vhosts $
;;
-d|-D)
remove_vhosts $

;;

  • )
    echo -e "\033[32m-------------------\033[0m"
    echo -e "\033[32m1)-I 1.12.0|1.18.0,Install Nginx WEB.\033[0m"
    echo -e "\033[32m2)-A v1.jf.com|v2.jf.com,Add virtual hosts\033[0m"
    echo -e "\033[32m3)-D v1.jf.com|v2.jf.com,Delete virtual hosts\033[0m"
    exit 1
    esac
上一篇:Go进阶:如何开发多彩动感的终端UI应用


下一篇:利用python实现日间、星期客流高峰提示