您当前的位置:首页 > 计算机 > 服务器 > Nginx

nginx一键部署shell脚本

时间:08-27来源:作者:点击数:

新建一个文本文件,将以下代码复制进去,保存后将文件后缀名改为.sh,然后将文件上传至服务器

给文件添加执行权限chmod 744 nginx-install.sh

执行该脚本

安装成功后在脚本所在目录会存在下载的安装包以及解压后的文件,可直接删除

注意:

保证服务器已是联网状态,若服务器无法连接网络,请手动将安装包下载并上传到服务器,并与脚本保持在同级目录

下载地址:

nginx:http://nginx.p2hp.com/download/nginx-1.22.0.tar.gz

openssl:https://www.openssl.org/source/openssl-1.1.1s.tar.gz

pcre:https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz/download

zlib:https://www.zlib.net/fossils/zlib-1.2.13.tar.gz

#!/bin/bash
yum install -y gcc gcc-c++ wget

if [ -f ./openssl-1.1.1s.tar.gz ]; then
    echo "openssl-1.1.1s.tar.gz already exists!"
else
    wget https://www.openssl.org/source/openssl-1.1.1s.tar.gz --no-check-certificate
    if [ $? -ne "0" ]; then
        echo "download openssl-1.1.1s.tar.gz failed!"
        exit 1
    fi
fi
if [ -f ./pcre-8.45.tar.gz ]; then
    echo "pcre-8.45.tar.gz already exists!"
else
    wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz/download --no-check-certificate
    if [ $? -ne "0" ]; then
        echo "download pcre-8.45.tar.gz failed!"
        exit 1
    fi
    mv download pcre-8.45.tar.gz
    if [ $? -ne "0" ]; then
        echo "change file name failed! please execute command `mv download pcre-8.45.tar.gz` manually"
        exit 1
    fi
fi
if [ -f ./zlib-1.2.13.tar.gz ]; then
    echo "zlib-1.2.13.tar.gz already exists!"
else
    wget https://www.zlib.net/fossils/zlib-1.2.13.tar.gz --no-check-certificate
    if [ $? -ne "0" ]; then
        echo "download zlib-1.2.13.tar.gz failed!"
        exit 1
    fi
fi
if [ -f ./nginx-1.22.0.tar.gz ]; then
    echo "nginx-1.22.0.tar.gz already exists!"
else
    wget http://nginx.p2hp.com/download/nginx-1.22.0.tar.gz --no-check-certificate
    if [ $? -ne "0" ]; then
        echo "download nginx-1.22.0.tar.gz failed!"
        exit 1
    fi
fi

CURRENT_PATH=`pwd`

OPENSSL= /usr/local/openssl
if [ -f "$OPENSSL" ]; then
    echo "$OPENSSL exist!"
else 
    mkdir $OPENSSL
    cd $CURRENT_PATH
    echo "creat $OPENSSL success!"
    echo "start install openssl ..."
    tar -xzvf openssl-1.1.1s.tar.gz
    if [ $? -ne "0" ]; then
        echo "extract openssl-1.1.1s.tar.gz failed!"
        exit 1
    fi
    cd ./openssl-1.1.1s
    ./config --prefix=/usr/local/openssl 
    ./config -t
     if [ $? -ne "0" ]; then
        echo "configure openssl failed!"
        cd ..
        exit 1
    fi
    make && make install
    if [ $? -ne "0" ]; then
        echo "install openssl failed!"
        cd ..
        exit 1
    fi
    echo "install openssl success!"
    cd ..
   
fi



PCRE= /usr/local/pcre
if [ -f "$PCRE" ]; then
    echo "$PCRE exist!"
else 
    mkdir $PCRE
    cd $CURRENT_PATH
    echo "creat $PCRE success!"
    echo "start install pcre ..."
    tar -xzvf pcre-8.45.tar.gz
    if [ $? -ne "0" ]; then
        echo "extract pcre-8.45.tar.gz failed!"
        exit 1
    fi
    cd pcre-8.45/
    ./configure --prefix=/usr/local/pcre
    if [ $? -ne "0" ]; then
        echo "configure pcre failed!"
        cd ..
        exit 1
    fi
    make && make install
    if [ $? -ne "0" ]; then
        echo "install pcre failed!"
        cd ..
        exit 1
    fi
    echo "install pcre success!"
    cd ..   
fi



ZLIB= /usr/local/zlib
if [ -f "$ZLIB" ]; then
    echo "$ZLIB exist!"
else 
    mkdir $ZLIB
    echo "creat $ZLIB success!"
    echo "start install zlib ..."
    tar -xzvf zlib-1.2.13.tar.gz
    if [ $? -ne "0" ]; then
        echo "extract zlib-1.2.13.tar.gz failed!"
        exit 1
    fi
    cd zlib-1.2.13/
    ./configure --prefix=/usr/local/zlib
    if [ $? -ne "0" ]; then
        echo "configure zlib failed!"
        cd ..
        exit 1
    fi
    make && make install
    if [ $? -ne "0" ]; then
        echo "install zlib failed!"
        cd ..
        exit 1
    fi
    echo "install zlib success!"
    cd ..   
fi

NGINX=/usr/local/nginx
SOURCE_OPENSSL=$CURRENT_PATH/openssl-1.1.1s
SOURCE_PCRE=$CURRENT_PATH/pcre-8.45
SOURCE_ZLIB=$CURRENT_PATH/zlib-1.2.13
if [ -f "$NGINX" ]; then
    echo "$NGINX exist!"
else 
    mkdir $NGINX
    echo "creat $NGINX success!"
    echo "start install nginx ..."
    tar -xzvf nginx-1.22.0.tar.gz
    if [ $? -ne "0" ]; then
        echo "extract zlib-1.2.13.tar.gz failed!"
        exit 1
    fi
    cd nginx-1.22.0/
    ./configure --prefix=/usr/local/nginx --with-openssl=$SOURCE_OPENSSL --with-pcre=$SOURCE_PCRE --with-zlib=$SOURCE_ZLIB
    if [ $? -ne "0" ]; then
        echo "configure nginx failed!"
        cd ..
        exit 1
    fi
    make && make install
    if [ $? -ne "0" ]; then
        echo "install nginx failed!"
        cd ..
        exit 1
    fi
    echo "install nginx success!"
    if [ -f $NGINX/sbin/nginx ]; then
        $NGINX/sbin/nginx
        if [ $? -eq "0" ]; then
            echo "ngix is running ... "
        fi
    fi
    cd ..  
fi
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门