一键编译安装nginx-1.18.0脚本(适用centos7,ubuntu1804,debian10)

#!/bin/sh
centos_page="
gcc
pcre-devel
openssl-devel
zlib-devel
make
"
ubuntu_page="
libpcre3
libpcre3-dev
openssl
libssl-dev
gcc
make
zlib1g-dev
"

COLOR="echo -e \E[1;32m"
COLOR1="echo -e \E[1;31m"
END="\E[0m"

centos_install(){
    . /etc/init.d/functions
    #判断编译安装的依赖包是否存在
    for PK in $centos_page;do
        rpm -q $PK &> /dev/null || yum -y -q install $PK    
    done
    #创建用户
    id nginx &> /dev/null || useradd -s /sbin/nologin nginx
    #下载源码包
    ${COLOR}"正在下载源码包,请稍等!"${END}
    cd /usr/local/src/
    [ -e nginx-1.18.0.tar.gz ] || wget http://nginx.org/download/nginx-1.18.0.tar.gz &> /dev/null
    [ -e nginx-1.18.0.tar.gz ] && tar xf nginx-1.18.0.tar.gz
    #创建安装目并修改权限
    [ -d /apps/nginx/run ] || mkdir -p /apps/nginx/run
    chown -R nginx.nginx /apps/nginx
    #编译
    ${COLOR}"正在编译安装,请稍等!"${END}
    cd  nginx-1.18.0/
    ./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 -j `lscpu | awk '/^CPU(s)/{print $2}'` && make install
    #修改配置文件
    [ -e /apps/nginx/conf/nginx.conf ] && sed -i 's@^#pid.*@pid /apps/nginx/run/nginx.pid;@' /apps/nginx/conf/nginx.conf
    cat > /usr/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

    [Install]
    WantedBy=multi-user.target
EOF
    #命令创建
    ln -s /apps/nginx/sbin/nginx /usr/bin/nginx
    #启动服务
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null && action "nginx安装启动完成!" || action "nginx启动失败,请检查配置文件!" false 
}

ubuntu_install(){
    #判断编译安装的依赖包是否存在
    for PK1 in $ubuntu_page;do
        dpkg -s $PK1 &> /dev/null || apt -y install $PK1
    done 
    #创建用户
    id nginx &> /dev/null || useradd -s /sbin/nologin nginx
    #下载源码包
    ${COLOR}"正在下载源码包,请稍等!"${END}
    cd /usr/local/src/
    [ -e nginx-1.18.0.tar.gz ] || wget http://nginx.org/download/nginx-1.18.0.tar.gz &> /dev/null
    [ -e nginx-1.18.0.tar.gz ] && tar xf nginx-1.18.0.tar.gz
    #创建安装目并修改权限
    [ -d /apps/nginx/run ] || mkdir -p /apps/nginx/run
    chown -R nginx.nginx /apps/nginx
    #编译
    ${COLOR}"正在编译安装,请稍等!"${END}
    cd  nginx-1.18.0/
    ./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 -j `lscpu | awk '/^CPU(s)/{print $2}'` && make install
    #修改配置文件
    [ -e /apps/nginx/conf/nginx.conf ] && sed -i 's@^#pid.*@pid /apps/nginx/run/nginx.pid;@' /apps/nginx/conf/nginx.conf
    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

[Install]
WantedBy=multi-user.target
EOF
    #命令创建
    ln -s /apps/nginx/sbin/nginx /usr/bin/nginx
    #启动服务
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null && ${COLOR}"nginx启动成功!"${END} || ${COLOR1}"nginx启动失败,请检查配置文件!"${END}
}

#分系统类型进行安装
#ostype1=`awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release` 
ostype2=`awk -F'"' '/^NAME/{print $2}' /etc/os-release`
if [[ $ostype2 == "CentOS Linux" ]];then
    centos_install
elif [[ $ostype2 == "Ubuntu" ]];then
    ubuntu_install
elif [[ $ostype2 == "Debian GNU/Linux" ]];then
    ubuntu_install
fi
原文地址:https://www.cnblogs.com/nj-duzi/p/14028643.html