shell脚本 【ubuntu 24.04】 nginx 安装脚本

shell脚本 【ubuntu 24.04】 nginx 安装脚本

#! /bin/bash

#Author: wanglechunZone
#Time: 2024-09-18
#System: ubuntu wsl 20.04
#nginx install script

# variable
NGINX_VERSION=nginx-1.26.2
NGINX_INSTALL_PATH=/usr/local/nginx


#1.check user 
check_user(){
    if [ "root" != $USER ]; then
        echo "please switch the root user"
        exit 1
    fi  
}

#2.check tools install
check_tools_install(){
    # install depdency
    if ! (apt-get install -y gcc g++ libpcre3-dev zlib1g-dev wget elinks make 1>/dev/null);then
        echo "error: install error"
        exit 1
    fi

    # download nginx package
    if  wget https://nginx.org/download/$NGINX_VERSION.tar.gz 1>/dev/null; then
        tar -xzvf $NGINX_VERSION.tar.gz
        if [ ! -d $NGINX_VERSION ];then
            echo "error:not found $NGINX_VERSION"
            exit 1
        fi
    else
        echo "error: download $NGINX_VERSION fail"
        exit 1
    fi

}

#4.make install
install(){
    cd $NGINX_VERSION
    if ./configure --prefix=$NGINX_INSTALL_PATH 1>/dev/null;then
        echo "nginx making..."
        if make ;then
            echo "nginx install...."
            if make install ;then
                echo "nginx install success..."
            else
                echo "error: nginx install fail..."
                exit 1
            fi
        else
            echo "error: make fail..."
            exit 1
        fi
    else
        echo "error: nginx configure fail..."
        exit 1
    fi

}

#5.test 
nginx_test(){
    if $NGINX_INSTALL_PATH/sbin/nginx ;then
        echo "nginx start success..."
        elinks http://localhost -dump
    else    
        echo "nginx start fail..."
    fi
    exit 1
}

check_user
check_tools_install
install
nginx_test

你可能感兴趣的:(ubuntu,nginx,linux)