• Nginx安装与虚拟主机配置shell脚本


    今天继续给大家介绍Linux运维相关知识,本文主要内容是Nginx安装与虚拟主机配置shell脚本。

    一、NGINX安装

    在今天的脚本上,我们开始尝试使用函数和参数,以增强脚本的灵活性和实现脚本的模块化。
    我们定义一个NGINX_INSTALL()的函数,主要用于安装NGINX,该函数需要下载NGINX的安装包,将该安装包解压,并进行预编译、编译和安装操作,相关脚本如下所示:

    NGINX_INSTALL(){
            wget -c http://nginx.org/download/nginx-1.12.0.tar.gz
            echo "Nginx download OK!"
            yum install -y gcc gcc-c++ openssl-devel pcre-devel pcre
            echo "Nginx Rliance OK!"
            tar xf nginx-1.12.0.tar.gz
            mv nginx-1.12.0 /opt/nginx
            useradd nginx
            echo "Nginx User create OK!"
            cd /opt/nginx
            ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
            make && make install
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二、NGINX配置

    接下来,我们还需要配置NGINX,以实现虚拟注意。定义一个NGINX_CONFIG()的函数,并且在该函数中传输两个参数,第一个参数是NGINX虚拟主机所监听的端口,第二个参数是NGINX虚拟主机的域名。相关脚本如下所示:

    NGINX_CONFIG(){
            cd /usr/local/nginx/
            cp -a conf/nginx.conf conf/nginx.conf.bak
            cat >conf/nginx.conf<<EOF
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
        server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
        server {
             listen       $1;
             server_name  $2;     
             location / {
                 root   /data/www/$2/;
                 index  index.html index.htm;
             }
        }
    }
    EOF
    mkdir -p /data/www/$2/
    touch /data/www/$2/index.html
    echo "Nginx VHOSTS" >> /data/www/$2/index.html
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    在上述脚本中,我们直接把NGINX的原有配置文件进行了备份,并且把新的配置文件直接写入到了脚本中。

    三、函数调用与启动

    最后,我们还要写一段脚本,调用上述两个函数,并且启动NGINX,相关脚本如下所示:

    NGINX_INSTALL
    NGINX_CONFIG $1 $2
    /usr/local/nginx/sbin/nginx
    
    • 1
    • 2
    • 3

    四、脚本展示

    我们把上述所有脚本整合到一起,形成完整的脚本,如下所示:

    #!/bin/bash
    NGINX_INSTALL(){
            wget -c http://nginx.org/download/nginx-1.12.0.tar.gz
            echo "Nginx download OK!"
            yum install -y gcc gcc-c++ openssl-devel pcre-devel pcre
            echo "Nginx Rliance OK!"
            tar xf nginx-1.12.0.tar.gz
            mv nginx-1.12.0 /opt/nginx
            useradd nginx
            echo "Nginx User create OK!"
            cd /opt/nginx
            ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
            make && make install
    
    }
    NGINX_CONFIG(){
            cd /usr/local/nginx/
            cp -a conf/nginx.conf conf/nginx.conf.bak
            cat >conf/nginx.conf<<EOF
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
        server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
        server {
             listen       $1;
             server_name  $2;     
             location / {
                 root   /data/www/$2/;
                 index  index.html index.htm;
             }
        }
    }
    EOF
    mkdir -p /data/www/$2/
    touch /data/www/$2/index.html
    echo "Nginx VHOSTS" >> /data/www/$2/index.html
    }
    NGINX_INSTALL
    NGINX_CONFIG $1 $2
    /usr/local/nginx/sbin/nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    在这里插入图片描述
    在这里插入图片描述

    五、效果检验

    在完成上述脚本后,我们直接执行该脚本,执行命令:

    bash nginx_install.sh 81 www.pzz.com
    
    • 1

    该脚本运行结果如下所示:
    在这里插入图片描述
    从上图可以看出,该脚本运行后,已经开始监听80和81端口。我们在本地hosts文件中,将www.pzz.com重定向到该虚拟机上,然后访问该域名的80和81端口,结果如下所示:
    在这里插入图片描述
    在这里插入图片描述
    从上图可以看出,该脚本可以成功安装Nginx并且配置虚拟主机!
    原创不易,转载请说明出处:https://blog.csdn.net/weixin_40228200

  • 相关阅读:
    ssm+vue+elementUI 基于微信小程序的游戏美术外包管理信息系统-#毕业设计
    [Spring] Spring5——事务简介
    Vue3学习(二十四)- 文档页面功能开发
    [网鼎杯 2020 朱雀组]phpweb-1|反序列化
    使用Django JWT实现身份验证
    windows flask服务卡死的问题
    如何在.Net Framework应用中请求HTTP2站点
    SEO的5大关键指标:排名+流量+会话+停留时长+跳出率
    MySQL的index merge(索引合并)导致数据库死锁分析与解决方案 | 京东云技术团队
    实战PyQt5:157-QChart图表之动态样条曲线
  • 原文地址:https://blog.csdn.net/weixin_40228200/article/details/125593045