系统相关
首页 > 系统相关> > sh nginx 虚拟主机安装卸载

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 $

;;

标签:虚拟主机,VHOSTS,html,nginx,sh,033,NGX,DIR
来源: https://blog.51cto.com/1585654/2658340