编译安装nginx脚本(在线离线都适用)

#!/bin/bash
#********************************************************************
#Author:      HE-handsome
#QQ:         2700565402
#Date:       2022-09-30
#FileName:   install_nginx.sh
#email:      hpneed977@outlook.com
#Description:路漫漫其修远兮,吾将上下而求索
#********************************************************************
. /etc/os-release

version=1.20.2

install () {
    if [ -e nginx-${version}.tar.gz ];then
    echo "文件存在,安装进行中。。。"
    else
        echo "开始下载源码文件。。。"
    wget http://nginx.org/download/nginx-${version}.tar.gz
    [ $? -eq 0 ] || { echo "下载失败!立即退出!";exit; }
    fi
    
    if id nginx &> /dev/null;then
        echo "用户存在"
    else
        groupadd -g 990 -r nginx && useradd -g nginx -s /sbin/nologin -r -u 990 nginx
        echo "创建nginx用户"
    fi
    
    echo "安装依赖包"
    if [ $ID == "rocky" -o $ID == "centos" ];then
        yum -y install make  gcc gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
    else
        apt update &> /dev/null
    apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi
    tar xf nginx-${version}.tar.gz -C /usr/local/src
    cd /usr/local/src/nginx-${version}
    ./configure --prefix=/apps/nginx \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-pcre \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_realip_module
    make && make install
    [ $? -eq 0 ] && echo "nginx-configure success" ||  { echo "nginx-configure faile";exit; }
    chown -R nginx.nginx /apps/nginx
    ln -s /apps/nginx/sbin/nginx /usr/sbin/

    cat > /lib/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target


[Service]
Type=forking
#PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
LimitNOFILE=100000


[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null
    [ $? -eq 0 ] && echo "nginx-start success" || { echo "nginx-start faile";exit; }
}
install

 

posted @ 2022-09-03 13:12  GlassHeart  阅读(199)  评论(0)    收藏  举报