每一年都奔走在自己de热爱里

愿你天黑有灯,下雨有伞

终极一键编译安装Nginx

附:该脚本实现一键编译安装Nginx,实现Nginx开机自启!实现systemctl与nginx命令同时管理nginx!

脚本如下:

 

终极一键编译安装Nginx
#!/bin/bash
#
#********************************************************************
#Author:            zenglf
#QQ:                7654321
#Date:              2022-06-08
#FileName           install_nginx.sh
#URL:               http://www.programmer.com
#Description        The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************

#NGINX_FILE=nginx-1.20.2      #可以自由选配nginx版本
NGINX_FILE=nginx-1.18.0
SRC_DIR=/usr/local/src
NGINX_URL=http://nginx.org/download/
TAR=.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu | awk '/^CPU/{ print $2 }'|head -n 2 | tail -n +2`
. /etc/os-release

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

os_type () {
   awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release
}

os_version () {
   awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release
}

check () {
    [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
    cd  ${SRC_DIR}
    if [  -e ${NGINX_FILE}${TAR} ];then
        color "相关文件已准备好" 0
    else
        color '开始下载 nginx 源码包' 0
        wget ${NGINX_URL}${NGINX_FILE}${TAR} 
        [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; } 
    fi
} 

install () {
    color "开始安装 nginx" 0
    if id nginx  &> /dev/null;then
        color "nginx 用户已存在" 1 
    else
        useradd -s /sbin/nologin -r  nginx
        color "创建 nginx 用户" 0 
    fi
    color "开始安装 nginx 依赖包" 0
    if [ `os_type` == "Rocky" -a `os_version` == '8.5' ] ;then
        yum -y -q install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed 
    elif [ `os_type` == "CentOS" -a `os_version` == '7' ];then
        yum -y -q  install make gcc pcre-devel openssl-devel zlib-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
    cd $SRC_DIR
    tar xf ${NGINX_FILE}${TAR}
    NGINX_DIR=`echo ${NGINX_FILE}${TAR}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${NGINX_DIR}
    ./configure --prefix=${NGINX_INSTALL_DIR} --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 $CPUS && make install 
    [ $? -eq 0 ] && color "nginx 编译安装成功" 0 ||  { color "nginx 编译安装失败,退出!" 1 ;exit; }
    echo "export PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
	ln -s /apps/nginx/sbin/nginx /usr/sbin/

    cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null 
    systemctl is-active nginx &> /dev/null ||  { color "nginx 启动失败,退出!" 1 ; exit; }
    color "nginx 安装完成" 0
}

check
install

部分安装过程:

[00:29:39 root@rocky8 ~]#bash install_nginx_v0.sh
开始下载 nginx 源码包                                      [  OK  ]
--2022-06-09 00:29:57--  http://nginx.org/download/nginx-1.18.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
正在连接 nginx.org (nginx.org)|52.58.199.22|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1039530 (1015K) [application/octet-stream]
正在保存至: “nginx-1.18.0.tar.gz”

nginx-1.18.0.tar.gz             100%[=======================================================>]   1015K   426KB/s  用时 2.4s

2022-06-09 00:30:00 (426 KB/s) - 已保存 “nginx-1.18.0.tar.gz” [1039530/1039530])

开始安装 nginx                                             [  OK  ]
创建 nginx 用户                                            [  OK  ]
开始安装 nginx 依赖包                                      [  OK  ]

...

make[1]: 离开目录“/usr/local/src/nginx-1.18.0”
nginx 编译安装成功                                         [  OK  ]
nginx 安装完成                                             [  OK  ]

#启动nginx:
[21:07:37 root@Centos7 ~]#nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[21:07:59 root@Centos7 ~]#ss -ntl
State      Recv-Q Send-Q                Local Address:Port                               Peer Address:Port
LISTEN     0      128                               *:80                    #nginx默认开启80端口                        *:*
LISTEN     0      128                               *:22                                            *:*
LISTEN     0      100                       127.0.0.1:25                                            *:*
LISTEN     0      128                               *:111                                           *:*
LISTEN     0      128                            [::]:22                                         [::]:*
LISTEN     0      100                           [::1]:25                                         [::]:*
LISTEN     0      128                            [::]:111                                        [::]:*
[21:08:02 root@Centos7 ~]#

测试状态:

[02:19:31 root@rocky8 ~]#nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[02:19:37 root@rocky8 ~]#
[02:19:37 root@rocky8 ~]#
[02:19:38 root@rocky8 ~]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[02:19:47 root@rocky8 ~]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-06-09 02:19:22 CST; 38s ago
  Process: 10830 ExecStart=/apps/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 10829 ExecStartPre=/apps/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 10827 ExecStartPre=/bin/rm -f /apps/nginx/logs/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 10832 (nginx)
    Tasks: 2 (limit: 12253)
   Memory: 1.8M
   CGroup: /system.slice/nginx.service
           ├─10832 nginx: master process /apps/nginx/sbin/nginx
           └─10833 nginx: worker process

6月 09 02:19:22 rocky8.magedu.org systemd[1]: Starting The nginx HTTP and reverse proxy server...
6月 09 02:19:22 rocky8.magedu.org nginx[10829]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
6月 09 02:19:22 rocky8.magedu.org nginx[10829]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
6月 09 02:19:22 rocky8.magedu.org systemd[1]: Started The nginx HTTP and reverse proxy server.

查看端口监听:

[02:22:07 root@rocky8 ~]#ss -ntlp
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port  Process
LISTEN  0       128            0.0.0.0:80          0.0.0.0:*      users:(("nginx",pid=10833,fd=8),("nginx",pid=10832,fd=8))
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*      users:(("sshd",pid=818,fd=4))
LISTEN  0       128               [::]:22             [::]:*      users:(("sshd",pid=818,fd=6))

 

posted @ 2022-06-16 22:21  一念6  阅读(143)  评论(0编辑  收藏  举报