阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Shell编写Nginx自动部署脚本

176次阅读
没有评论

共计 2602 个字符,预计需要花费 7 分钟才能阅读完成。

1、功能描述

1. 安装支持包,从软件源下载自定义的 NGINX 包,创建 NGINX 用户和用户组。

2. 安装并初始化 NGINX 配置。

3. 运行 NGINX 并检测运行状态。

2、实现

源码如下:

#!/bin/bash
# eastmoney public tools
# version: v1.0.1
# create by XuHoo, 2016-9-28
#

function environment() {if [["$USER" != "root" ]]; then
        echo "Current user is not root"
        return 1
    fi
    yum -y install wget curl pcre pcre-devel zlib zlib-devel gcc gcc-c++ &> /tmp/nginx_install.log
    # getUrl: Input download source address
    # getUrl='http://172.16.1.1\nginx-1.8.1.tar.gz'
    wget -P /tmp/ $getUrl/nginx.tar.gz
    grep "nginx" /etc/passwd > /dev/null
    if [[$? -ne 0]]; then  # check user and group
        groupadd nginx
        useradd -M -g nginx -s /sbin/nologin nginx
    fi
    cd /tmp; tar -zxf nginx.tar.gz; cd nginx
    return 0
}; environment; [$? -ne 0 ] && exit 1

function install() {# Compile before installation configuration
    ./configure --prefix=/usr/local/nginx \
                --user=nginx --group=nginx \
                --with-http_stub_status_module \
                &> /tmp/nginx_install.log
    if [[$? -ne 0 ]]; then
        return 1
    else
        # make && make install
        make &> /tmp/nginx_install.log
        make install &> /tmp/nginx_install.log
        if [[$? -ne 0 ]]; then
            return 1
        fi
        return 0
    fi
}; install; [$? -ne 0 ] && exit 1

function optimize() {ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ > /dev/null
    cp -f /tmp/nginx_control.sh /etc/init.d/nginx
    cp -f /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf
    # The number of CPU cores current server,
    # Amend the "worker_processes" field to the value of the processor
    processor=`cat /proc/cpuinfo | grep "processor" | wc -l`
    sed -i "s/^w.*;$/worker_processes  ${processor};/g" /usr/local/nginx/conf/nginx.conf
    chmod +x /etc/init.d/nginx
    chkconfig --add nginx
    retval=`chkconfig --level 3 nginx on`  # Configure nginx open start service
    return $retval
}; optimize; [$? -ne 0 ] && exit 1

function run() {# Test nginx.conf file syntax is correct
    /etc/init.d/nginx test &> /tmp/nginx_run.log
    if [[$? -ne 0 ]]; then
        retval=$?
    else  # Start nginx server
        /etc/init.d/nginx start &> /tmp/nginx_run.log
        if [[$? -ne 0 ]]; then
            retval=$?
        fi
    fi
    return 0
}; run; [$? -ne 0 ] && exit 1

function check() {# Modified index.html page content
    content=$"deployment on $(date "+%Y-%m-%d %H:%M:%S")"
    echo $content > /usr/local/nginx/html/index.html
    # View the index.html, and the output of the modified index.html page
    /etc/init.d/nginx status
    echo -n "Index.html: "; curl http://localhost
}; check

更多 Nginx 负载均衡配置 相关教程见以下内容

Nginx 负载均衡配置说明 http://www.linuxidc.com/Linux/2016-03/129424.htm

Linux 下 Nginx+Tomcat 负载均衡和动静分离配置要点  http://www.linuxidc.com/Linux/2016-01/127255.htm

Docker+Nginx+Tomcat7 配置简单的负载均衡  http://www.linuxidc.com/Linux/2015-12/125907.htm

Nginx 负载均衡(主备)+Keepalived  http://www.linuxidc.com/Linux/2015-12/126865.htm

使用 Nginx 作为负载均衡器 http://www.linuxidc.com/Linux/2015-12/125789.htm

使用 Nginx 简单实现负载均衡  http://www.linuxidc.com/Linux/2016-08/134443.htm

Nginx 负载均衡与高可用的实现 http://www.linuxidc.com/Linux/2016-04/130350.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135619.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2602字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中