这是一个用于安装Nginx的shell脚本,适用于UbuntuCentOS,已在Ubuntu22.04CentOS7.9测试通过。

## 脚本需求:

写一个自动部署nginx服务的脚本。手动部署步骤如下,请将下面的步骤写成shell脚本。

1)下载源码
cd  /usr/local/src
sudo curl -O http://nginx.org/download/nginx-1.24.0.tar.gz

2)解压
sudo tar zxf nginx-1.24.0.tar.gz
cd nginx-1.24.0

3)安装依赖
## RHEL/Rocky
sudo yum install -y gcc  make  pcre-devel zlib-devel  openssl-devel

4)配置
sudo ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf  --with-http_ssl_module --with-http_stub_status_module --with-http_v2_module --with-http_gzip_static_module --with-poll_module --with-http_realip_module --with-stream --with-stream_ssl_module

5)编译和安装
sudo make  && sudo  make install

6)编辑system服务管理脚本
sudo vi /lib/systemd/system/nginx.service  #写入如下内容
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStartSec=120
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000

[Install]
WantedBy=multi-user.target

7)加载服务
sudo  systemctl daemon-reload

8)启动服务
sudo systemctl start nginx

------- 分割线,以下为脚本正文 -------

#!/bin/bash
# Modified By: fan**  (微信: ***yzy)
# version: v2
# date: 2023-11-6

# 检查Nginx是否已经安装
if command -v nginx >/dev/null 2>&1; then
    echo "Nginx已经安装,脚本将退出。"
    exit 1
fi

# 检查Docker是否安装
if command -v docker >/dev/null 2>&1; then
    echo "Docker已安装,检查是否运行Nginx容器。"
    # 检查Nginx容器是否存在
    if [ "$(sudo docker ps -q -f ancestor=nginx)" ]; then
        echo "Nginx容器已经运行,脚本将退出。"
        exit 1
    fi
    echo "Nginx未安装,脚本将继续执行。"
fi
echo "Nginx未安装,脚本将继续执行。"

ck_ok()
{
        if [ $? -ne 0 ]
        then
                echo "$1 error."
                exit 1
        fi
}

download_ng()
{
    cd  /usr/local/src
    if [ -f nginx-1.24.0.tar.gz ]
    then
        echo "当前目录已经存在nginx-1.24.0.tar.gz"
        echo "检测md5"
        ng_md5=`md5sum nginx-1.24.0.tar.gz|awk '{print $1}'`
        if [ ${ng_md5} == 'f95835b55b3cbf05a4368e7bccbb8a46' ]
        then
            return 0
        else
            sudo /bin/mv nginx-1.24.0.tar.gz nginx-1.24.0.tar.gz.old
        fi
    fi

    sudo curl -O http://nginx.org/download/nginx-1.24.0.tar.gz
    ck_ok "下载Nginx"

}

download_ng-conf()
{
    cd  /usr/local/src
    if [ -f nginx.conf ]
    then
        echo "当前目录已经存在nginx.conf"
            return 0
    else
        echo "下载配置文件"
        sudo curl -o nginx.conf http://oss.nextoyou.cn/config/nginx/v1/nginx.conf.txt
        ck_ok "下载配置文件"
    fi

}

install_ng()
{
    cd /usr/local/src
    echo "解压Nginx"
    sudo tar zxf nginx-1.24.0.tar.gz
    ck_ok "解压Nginx"
    cd nginx-1.24.0


    echo "安装依赖"
    if which yum >/dev/null 2>&1
    then
        ## RHEL/Rocky
        for pkg in gcc make pcre-devel zlib-devel openssl-devel
        do
            if ! rpm -q $pkg >/dev/null 2>&1
            then
                sudo yum install -y $pkg
                ck_ok "yum 安装$pkg"
            else
                echo "$pkg已经安装"
            fi
        done
    fi


    if which apt >/dev/null 2>&1
    then
        ##ubuntu
        for pkg in make libpcre++-dev  libssl-dev  zlib1g-dev
        do
            if ! dpkg -l $pkg >/dev/null 2>&1
            then
                sudo apt install -y $pkg
                ck_ok "apt 安装$pkg"
            else
                echo "$pkg已经安装"
            fi
        done
    fi

    echo "configure Nginx"
    sudo ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf  --with-http_ssl_module --with-http_stub_status_module --with-http_v2_module --with-http_gzip_static_module --with-poll_module --with-http_realip_module --with-stream --with-stream_ssl_module
    ck_ok "Configure Nginx"


    echo "编译和安装"
    sudo make && sudo make install
    ck_ok "编译和安装"


    if [ -f /usr/sbin/nginx ]
    then
        echo "已经存在nginx软连接"
    else
        echo "配置软连接"
        sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
        ck_ok "配置软连接"
    fi
    
    echo "修改配置文件"
    sudo cp -rf /usr/local/src/nginx.conf /etc/nginx/nginx.conf
    mkdir -p /data/wwwlogs/
    ck_ok "修改配置文件"

    echo "编辑systemd服务管理脚本"


    cat > /tmp/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPost=/bin/sleep 0.1
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
TimeoutStartSec=120
LimitNOFILE=1000000
LimitNPROC=1000000
LimitCORE=1000000

[Install]
WantedBy=multi-user.target
EOF

    sudo /bin/mv /tmp/nginx.service /lib/systemd/system/nginx.service
    ck_ok "编辑nginx.service"

    echo "加载服务"
    sudo systemctl unmask nginx.service
    sudo  systemctl daemon-reload
    sudo systemctl enable nginx
    echo "启动Nginx"
    sudo systemctl start nginx
    ck_ok "启动Nginx"
}

download_ng
download_ng-conf
install_ng