您的位置:首页 > 运维架构 > Nginx

sh nginx虚拟主机批量添加

2021-03-05 17:05 836 查看

#!/bin/bash
#2021年3月3日21:59:07
#auto manager nginx virtual hosts
#by author www.jfedu.net
########################
NGX_VHOST="$*"
NGX_VER="1.12.2"
NGX_CNF="nginx.conf"
NGX_YUM="yum install -y"
NGX_DIR="/usr/local/nginx"
NGX_SRC="nginx-${NGX_VER}"
NGX_SOFT="nginx-${NGX_VER}.tar.gz"
NGX_URL="http://nginx.org/download"
NGX_ARGS="--user=www --group=www --with-http_stub_status_module"
if [ $# -eq 0 ];then
echo -e "\033[32m---------------\033[0m"
echo -e "\033[32mUsage:{/bin/sh $0 v1.jf.com|v2.jf.com|help}\033[0m"
exit 0
fi
if [ ! -f $NGX_DIR/sbin/nginx ];then
#Install Nginx
$NGX_YUM wget tar gzip make gcc
$NGX_YUM pcre pcre-devel zlib-devel
wget -c $NGX_URL/$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 nginx
netstat -nltp|grep 80
setenforce 0
systemctl stop firewalld.service
fi

#Config virtual hosts
cd $NGX_DIR/conf/
\cp ${NGX_CNF} ${NGX_CNF}.bak
cat>${NGX_CNF}<<EOF
worker_processes 1;
events {
systemctl stop firewalld.service
fi

#Config virtual hosts
cd $NGX_DIR/conf/
\cp ${NGX_CNF} ${NGX_CNF}.bak
cat>${NGX_CNF}<<EOF
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include vhosts/*;
}
EOF
mkdir -p vhosts
cd vhosts/
for NGX_VHOST in $NGX_VHOST
do
touch $NGX_VHOST
cat>$NGX_VHOST<<-EOF
server {
listen 80;
server_name $NGX_VHOST;
location / {
root html/$NGX_VHOST;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
EOF
mkdir -p $NGX_DIR/html/$NGX_VHOST/
cat>$NGX_DIR/html/$NGX_VHOST/index.html<<-EOF
<h1>$NGX_VHOST Test Pages.</h1>
<hr color=red>
EOF
$NGX_DIR/sbin/nginx -t
$NGX_DIR/sbin/nginx -s reload
done

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: