Nginx的快速部署(Centos Shell脚本)

Posted spark

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx的快速部署(Centos Shell脚本)相关的知识,希望对你有一定的参考价值。

当然nginx最快的部署还是Docker,或者虚拟机镜像部署,但是虚拟机和docker还是得新建镜像嘛。分享一个小脚本,测试正常,部分参数需要自己修改下。


##### Created by bluehunter #####

#####      2017-09-29       #####

#####    有盐APP TEAM       #####

#####  NginxInstall V1.0    #####



#模块主要分为版本管理,参数自定义,一些常用方法定义,具体安装部分。

#初始化


yum install -y make  perl-devel perl perl-ExtUtils-Embed cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel patch wget crontabs libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel unzip tar bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap diffutils ca-certificates net-tools libc-client-devel psmisc libXpm-devel git-core c-ares-devel libicu-devel libxslt libxslt-devel xz


#下载源,自己修改成自己的源。

Download_Mirror='https://soft.vpser.net'

YouyanApp_Download_Mirror='https://xxxx.xxxxx.com'


#是否支持ssl

Enable_Nginx_Openssl=y


#自定义编译参数

Nginx_Modules_Options='--with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_random_index_module --with-http_secure_link_module --with-mail --with-mail_ssl_module --with-http_gunzip_module --with-http_image_filter_module  --with-http_perl_module --add-module=../lua-nginx-module-0.10.6 --add-module=../echo-nginx-module-0.60 --add-module=../ngx_devel_kit-0.3.0 --add-module=../redis2-nginx-module-0.14 --add-module=../set-misc-nginx-module-0.31' 


#数据目录

Default_Website_Dir='/xxx/wwwroot/default'

Log_Dir='/xxx/wwwlogs'


#版本定义

Autoconf_Ver='autoconf-2.69'

Libiconv_Ver='libiconv-1.14'

LibMcrypt_Ver='libmcrypt-2.5.8'

Mcypt_Ver='mcrypt-2.6.8'

Mhash_Ver='mhash-0.9.9.9'

Freetype_Ver='freetype-2.7'

Curl_Ver='curl-7.51.0'

Pcre_Ver='pcre-8.39'

Jemalloc_Ver='jemalloc-4.5.0'

TCMalloc_Ver='gperftools-2.5'

Libunwind_Ver='libunwind-1.1'

Libicu4c_Ver='icu4c-58_1'

Boost_Ver='boost_1_59_0'

Openssl_Ver='openssl-1.0.2l'

Nginx_Ver='nginx-1.10.0'

Pureftpd_Ver='pure-ftpd-1.0.45'


XCache_Ver='xcache-3.2.0'

ImageMagick_Ver='ImageMagick-7.0.5-7'

Imagick_Ver='imagick-3.4.3'

ZendOpcache_Ver='zendopcache-7.0.5'

Redis_Stable_Ver='redis-3.2.8'

phpRedis_Ver='redis-3.1.2'

Memcached_Ver='memcached-1.4.36'

Libmemcached_Ver='libmemcached-1.0.18'

PHPMemcached_Ver='memcached-2.2.0'

PHP7Memcached_Ver='memcached-3.0.3'

PHPMemcache_Ver='memcache-3.0.8'

PHPOldApcu_Ver='apcu-4.0.11'

PHPNewApcu_Ver='apcu-5.1.8'



#当前路径

cur_dir=$PWD

mkdir ${cur_dir}/src

mkdir ${cur_dir}/conf

mkdir ${cur_dir}/init.d

cd ${cur_dir}/src


#定义解压缩方法

Tar_Cd()

{

    local FileName=$1

    local DirName=$2

    cd ${cur_dir}/src

    [[ -d "${DirName}" ]] && rm -rf ${DirName}

    echo "Uncompress ${FileName}..."

    tar zxf ${FileName}

    echo "cd ${DirName}..."

    cd ${DirName}

}


#解压bz2方法

Tarj_Cd()

{

    local FileName=$1

    local DirName=$2

    cd ${cur_dir}/src

    [[ -d "${DirName}" ]] && rm -rf ${DirName}

    echo "Uncompress ${FileName}..."

    tar jxf ${FileName}

    echo "cd ${DirName}..."

    cd ${DirName}

}



#下载方法

Download_Files()

{

    local URL=$1

    local FileName=$2

    if [ -s "${FileName}" ]; then

        echo "${FileName} [found]"

    else

        echo "Notice: ${FileName} not found!!!download now..."

        wget  ${URL} -O  ${FileName}

    fi

}


#安装openssl,方便指定版本

Install_Nginx_Openssl()

{

    if [ "${Enable_Nginx_Openssl}" = 'y' ]; then

        Download_Files ${Download_Mirror}/lib/openssl/${Openssl_Ver}.tar.gz ${Openssl_Ver}.tar.gz

        [[ -d "${Openssl_Ver}" ]] && rm -rf ${Openssl_Ver}

        tar zxf ${Openssl_Ver}.tar.gz

        Nginx_With_Openssl="--with-openssl=${cur_dir}/src/${Openssl_Ver}"

    fi

}


#下载相关模块,yum安装了依赖,所以有些没用上。有的是php的依赖模块,暂时还没用

Install_OtherMoudel()

{

Download_Files http://luajit.org/download/LuaJIT-2.0.3.tar.gz LuaJIT-2.0.3.tar.gz

Download_Files https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz ngx_devel_kit-0.3.0.tar.gz

Download_Files https://github.com/openresty/lua-nginx-module/archive/v0.10.6.tar.gz lua-nginx-module-0.10.6.tar.gz 

Download_Files https://github.com/openresty/redis2-nginx-module/archive/v0.14.tar.gz redis2-nginx-module-0.14.tar.gz

Download_Files https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz echo-nginx-module-0.60.tar.gz

Download_Files https://github.com/openresty/set-misc-nginx-module/archive/v0.31.tar.gz set-misc-nginx-module-0.31.tar.gz

Download_Files ${Download_Mirror}/web/nginx/nginx-1.10.0.tar.gz nginx-1.10.0.tar.gz 

Download_Files ${Download_Mirror}/lib/autoconf/${Autoconf_Ver}.tar.gz ${Autoconf_Ver}.tar.gz

Download_Files ${Download_Mirror}/web/libiconv/${Libiconv_Ver}.tar.gz ${Libiconv_Ver}.tar.gz

Download_Files ${Download_Mirror}/web/libmcrypt/${LibMcrypt_Ver}.tar.gz ${LibMcrypt_Ver}.tar.gz

Download_Files ${Download_Mirror}/web/mcrypt/${Mcypt_Ver}.tar.gz ${Mcypt_Ver}.tar.gz

Download_Files ${Download_Mirror}/web/mhash/${Mhash_Ver}.tar.bz2 ${Mhash_Ver}.tar.bz2

Download_Files ${Download_Mirror}/lib/freetype/${Freetype_Ver}.tar.bz2 ${Freetype_Ver}.tar.bz2

Download_Files ${Download_Mirror}/lib/curl/${Curl_Ver}.tar.bz2 ${Curl_Ver}.tar.bz2

Download_Files ${Download_Mirror}/web/pcre/${Pcre_Ver}.tar.bz2 ${Pcre_Ver}.tar.bz2


#删除旧目录

[ -d "${cur_dir}/src/LuaJIT-2.0.3" ] && rm -rf ${cur_dir}/src/LuaJIT-2.0.3

[ -d "${cur_dir}/src/ngx_devel_kit-0.2.19" ] && rm -rf  ${cur_dir}/src/ngx_devel_kit-0.2.19

[ -d "${cur_dir}/src/lua-nginx-module-0.10.6" ] && rm -rf  ${cur_dir}/src/lua-nginx-module-0.10.6

[ -d "${cur_dir}/src/redis2-nginx-module-0.14" ] && rm -rf ${cur_dir}/src/redis2-nginx-module-0.14

[ -d "${cur_dir}/src/echo-nginx-module-0.60" ] && rm -rf ${cur_dir}/src/echo-nginx-module-0.60

[ -d "${cur_dir}/src/set-misc-nginx-module-0.31" ] && rm -rf ${cur_dir}/src/set-misc-nginx-module-0.31

#解压安装包

Tar_Cd ${cur_dir}/src/LuaJIT-2.0.3.tar.gz  LuaJIT-2.0.3

Tar_Cd ${cur_dir}/src/ngx_devel_kit-0.3.0.tar.gz ngx_devel_kit-0.3.0

Tar_Cd ${cur_dir}/src/lua-nginx-module-0.10.6.tar.gz  lua-nginx-module-0.10.6

Tar_Cd ${cur_dir}/src/redis2-nginx-module-0.14.tar.gz  redis2-nginx-module-0.14

Tar_Cd ${cur_dir}/src/echo-nginx-module-0.60.tar.gz echo-nginx-module-0.60

Tar_Cd ${cur_dir}/src/set-misc-nginx-module-0.31.tar.gz set-misc-nginx-module-0.31


cd ${cur_dir}/src/LuaJIT-2.0.3

make

make install

export LUAJIT_LIB=/usr/local/lib

export LUAJIT_INC=/usr/local/include/luajit-2.0/


}


Color_Text()

{

  echo -e " \e[0;$2m$1\e[0m"

}


Echo_Blue()

{

  echo $(Color_Text "$1" "34")

}


#安装nginx部分,根据版本指定不同参数

Install_Nginx()

{

    Echo_Blue "[+] Installing ${Nginx_Ver}... "

    groupadd www

    useradd -s /sbin/nologin -g www www

    Install_OtherMoudel

    cd ${cur_dir}/src

    Install_Nginx_Openssl

    Tar_Cd ${Nginx_Ver}.tar.gz ${Nginx_Ver}

    if echo ${Nginx_Ver} | grep -Eqi 'nginx-[0-1].[5-8].[0-9]' || echo ${Nginx_Ver} | grep -Eqi 'nginx-1.9.[1-4]$'; then

        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${Nginx_With_Openssl} ${NginxMAOpt} ${Nginx_Modules_Options}

    else

        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${Nginx_With_Openssl} ${NginxMAOpt} ${Nginx_Modules_Options}

    fi

    make && make install

    cd ../


    ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx


    rm -f /usr/local/nginx/conf/nginx.conf

    cd ${cur_dir}

   

    #配置文件初始化,从我们自己的源站下载修改好的配置文件。

    wget http://$YouyanApp_Download_Mirror/conf/nginx.conf -O ${cur_dir}/conf/nginx.conf

    wget http://$YouyanApp_Download_Mirror/conf/enable-php.conf -O ${cur_dir}/conf/enable-php.conf

    wget http://$YouyanApp_Download_Mirror/init.d/init.d.nginx -O ${cur_dir}/init.d/nginx

    \cp conf/nginx.conf /usr/local/nginx/conf/nginx.conf

    \cp conf/enable-php.conf /usr/local/nginx/conf/enable-php.conf

    \cp init.d/nginx /etc/init.d/nginx

    ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib64/

    chmod +x /etc/init.d/nginx


    #创建默认根目录

    mkdir -p ${Default_Website_Dir}

    chmod +w ${Default_Website_Dir}

    mkdir -p $Log_Dir

    chmod 777 $Log_Dir


    chown -R www:www ${Default_Website_Dir}


    mkdir /usr/local/nginx/conf/vhost


    if [ "${Default_Website_Dir}" != "/xxx/wwwroot/default" ]; then

        sed -i "s#/xxx/wwwroot/default#${Default_Website_Dir}#g" /usr/local/nginx/conf/nginx.conf

    fi


    cat >${Default_Website_Dir}/.user.ini<<EOF

open_basedir=${Default_Website_Dir}:/tmp/:/proc/

EOF

        chmod 644 ${Default_Website_Dir}/.user.ini

        chattr +i ${Default_Website_Dir}/.user.ini

   

    cat >>/usr/local/nginx/conf/fastcgi.conf<<EOF

fastcgi_param PHP_ADMIN_VALUE "open_basedir=\$document_root/:/tmp/:/proc/";

EOF

   

   

}


#开始安装

Install_Nginx


#启动

service nginx start  


以上是关于Nginx的快速部署(Centos Shell脚本)的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本编写nginx部署脚本

shell 脚本之一键部署安装 Nginx

Shell脚本快速部署Kubernetes集群系统

shell脚本实战-部署nginx脚本

shell脚本实战-部署nginx脚本

CentOS6.8 x86_64bit shell脚本实现模拟nginx服务启动与关闭