Shell脚本编程实战之Nginx虚拟主机脚本一

Nginx Web服务器的最大特点在于Nginx常被用于负载均衡、反向代理,单台Nginx服务器配置多个虚拟主机,百台服务器配置N多虚拟主机,基于Shell脚本可以更加高效的配置虚拟主机及添加、管理。本篇知识点主要介绍用Shell脚本实现Nginx自动安装以及虚拟主机的维护,编写思路如下:

  1. 判断Nginx WEB软件服务是否部署或者运行;
  2. 支持单个虚拟主机的添加;
  3. 支持多个虚拟主机添加;
  4. 支持删除单个虚拟主机或者多个虚拟主机

具体实现脚本:

#!/bin/bash

#2020年3月6日21:28:16

#auto config nginx vhosts

#by author lee

#########################

#Install nginx web

yum install -y wget gzip make tar gcc

yum install -y pcre pcre-devel zlib-devel

wget -c http://nginx.org/download/nginx-1.16.0.tar.gz

tar -xzf nginx-1.16.0.tar.gz

cd nginx-1.16.0

useradd -s /sbin/nologin www -M

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module

make

make install

/usr/local/nginx/sbin/nginx

ps -ef|grep nginx

netstat -tnlp|grep -w 80

setenforce 0

systemctl stop firewalld.service

#Config nginx vhosts

cd /usr/local/nginx/conf/

\cp nginx.conf nginx.conf.bak

grep -vE "#|^$" nginx.conf >nginx.conf.swp

sed -i '/server/,$d' nginx.conf.swp

echo -e "    include vhosts/*;\n}" >>nginx.conf.swp

\cp nginx.conf.swp nginx.conf

mkdir -p vhosts

cd vhosts

cat>v1.jfedu.net<<EOF

server {

        listen       80;

        server_name  v1.jfedu.net;

        location / {

            root   html/v1.jfedu.net;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

}

EOF

mkdir -p /usr/local/nginx/html/v1.jfedu.net

cat>/usr/local/nginx/html/v1.jfedu.net/index.html<<EOF

<html>

<head>

         <title><h1>v1.jfedu.net Test Pages.</h1></title>

</head>

<body>

         <hr color=red>

</body>

</html>

EOF

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

发布了14 篇原创文章 · 获赞 0 · 访问量 414

猜你喜欢

转载自blog.csdn.net/falnet/article/details/104706878