Nginx自动编译安装脚本

1.运行前准备

路径/usr/local/src下创建三个文件,或者见>>> Nginx编译安装  <<<

依赖库:nginx_dependent.txt

编译参数:nginx_parameter.txt

system服务文件:nginx.service

[root@Zabbix src]# cat nginx_dependent.txt
gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel libxml2-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed
geoip-devel wget net-tools
[root@Zabbix src]# cat nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@Zabbix src]# cat nginx_parameter.txt
--prefix=/usr/local/nginx-1.16.1
--user=nginx
--group=nginx
--with-file-aio
--with-threads
--with-http_ssl_module
--with-http_v2_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module=dynamic
--with-http_image_filter_module=dynamic
--with-http_geoip_module=dynamic
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_auth_request_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_degradation_module
--with-http_slice_module
--with-http_stub_status_module
--with-http_perl_module=dynamic
--with-mail=dynamic
--with-mail_ssl_module
--with-stream
--with-stream_ssl_module
--with-stream_realip_module
--with-stream_geoip_module
--with-stream_geoip_module=dynamic
--with-stream_ssl_preread_module

2.脚本

#!/bin/bash
. /etc/init.d/functions
VERSION="nginx-1.16.1"
PACKAGE_DIR="/usr/local/src"
BASE_DIR="/usr/local/${VERSION}"
WORK_DIR="/usr/local/nginx"
PARAMETER="${PACKAGE_DIR}/nginx_parameter.txt"
DEPENDENT_LIB="${PACKAGE_DIR}/nginx_dependent.txt"
SERVICE_FILE="${PACKAGE_DIR}/nginx.service"
NGINX_USER="nginx"
###
echo -e "33[34m######Starting install ${VERSION}######33[0m"
###Error Tip###
Error(){
    action "$1" /bin/false
    exit 1
}
###安装依赖###
InstallDep(){
    if [[ -f ${DEPENDENT_LIB} ]]
        then  
            for i in `cat ${DEPENDENT_LIB}`
            do
                echo "Starting install ${i}"
                yum install -y ${i} &>/dev/null || Error "Install ${i} Package Failed"
                action "${i} Package Installed Successfully" /bin/true
            done
        else
            Error "${DEPENDENT_LIB} Is Not Found"
    fi
}
###创建用户###
CreateUser(){
    User_Check=`cat /etc/passwd|grep ${NGINX_USER}`
    if [[ $? != 0 ]]
        then
            useradd -s /sbin/nologin -M ${NGINX_USER} || Error "Create ${NGINX_USER} User Failed"
            action "${NGINX_USER} User Created Successfully" /bin/true
        else
            action "User ${NGINX_USER} Exists" /bin/true
    fi
}
###下载安装包###
DownloadPackages(){
    Package_URL="http://nginx.org/download/nginx-1.16.1.tar.gz"
    Nginx_Package=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz`
    if [[ ! -f ${Nginx_Package} ]]
        then
            action "Starting Download Package" /bin/true
            wget -P ${PACKAGE_DIR} ${Package_URL} || Error "Download Package Failed"
            action "Package Downloaded Successfully" /bin/true
         else
            action "Package Already Exists" /bin/true
    fi
}                 
UnzipPackages(){                  
    Nginx_Folder=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz -exec basename {} .tar.gz ;`
    Nginx_Package=`find ${PACKAGE_DIR} -name ${VERSION}*.tar.gz`
    if [[ ! -d ${PACKAGE_DIR}/${Nginx_Folder} ]]
        then
            echo "Starting Unzip Nginx Package,Just Wait"
            tar xf ${Nginx_Package} -C ${PACKAGE_DIR}|| Error "Unzip Package Failed"
            action "Package Unzipped Successfully" /bin/true
        else
            action "Package Already Unzipped" /bin/true
    fi
}
###编译安装###
InstallNginx(){
    Nginx_Path="${PACKAGE_DIR}/${Nginx_Folder}"
    if [[ ! -d ${BASE_DIR} ]] 
        then
            if [[ -f  ${PACKAGE_DIR}/nginx_parameter.txt ]]
                then
                    cd ${Nginx_Path}
                    ${Nginx_Path}/configure `cat ${PARAMETER}` || Error "Configure Package Failed"
                    action "Configured Successfully" /bin/true
                    make -j2 && make install || Error "Make Package Failed"
                    action "Nginx Installed Successfully" /bin/true
                    ln -s ${BASE_DIR} ${WORK_DIR} || Error "Link ${Nginx_Path} ${WORK_DIR} Failed"
                    action "Link ${Nginx_Path} ${WORK_DIR} Successfully" /bin/true
                else
                    action "PARAMETER.TXT Is Not Found" /bin/false
            fi        
        else
            action "Nginx Already Installed" /bin/true
    fi
}
###设置环境变量###
InitEnv(){
    bashrc="/etc/profile"
    Check_Env=`cat ${bashrc}|grep ${WORK_DIR}`
    if [[ ${Check_Env} == "" ]]
        then
            echo "export PATH=${WORK_DIR}/sbin:$PATH" >> ${bashrc} || Error "Set Environment Failed"
            action "Environment Setted Successfully" /bin/true
        else
            action "${BASE_DIR} Is Already Exists In ${bashrc}" /bin/true
    fi
    source /etc/profile
}
###配置system服务###
NginxService(){
    File_Check="/lib/systemd/system/nginx.service"
    if [[ ! -f ${File_Check} ]]
        then
            cp ${SERVICE_FILE} /lib/systemd/system/ || Error "Copy Service File Failed"
            action "Copy Service File Successfully" /bin/true
        else
            action "Service File Already Exists" /bin/true
    fi
}
###启动服务###
StartNginx(){
    Stat_Check=`netstat -lntup|grep 80`
    if [[ $? != 0 ]]
        then
            systemctl start nginx || Error "Nginx Start Failed"
            systemctl enable nginx || Error "Nginx Enable Failed"
            action "Nginx Start Successfully" /bin/true
        else
            action "Nginx Service Was Already Running,Exit The Script" /bin/true
            exit 1
    fi
}
TaskMain(){
    echo -e "33[32mStep One:Starting Install Dependency Packages33[0m"
    InstallDep
    echo -e "33[32mStep Two:Starting Create Nginx User33[0m"
    CreateUser
    echo -e "33[32mStep Three:Starting Download Package33[0m"
    DownloadPackages
    echo -e "33[32mStep Four:Starting Unzip Package33[0m"
    UnzipPackages
    echo -e "33[32mStep Five:Starting Install Nginx33[0m"
    InstallNginx
    echo -e "33[32mStep Six:Starting Set Environment33[0m"
    InitEnv
    echo -e "33[32mStep Seven:Starting Copy Service File33[0m"
    NginxService
    echo -e "33[32mStep Eight:Starting Start Service33[0m"
    StartNginx
    echo -e "33[32mCongratulations, Nginx Installation Was Successfully33[0m"
}
TaskMain


「 文章如果对你有帮助,请点个赞哦^^ 」 

0

这篇文章有 2 条评论

评论关闭。