1. 程式人生 > >nginx通過shell腳本平滑升級版本

nginx通過shell腳本平滑升級版本

ima term tar func () follow ech check 是否

1、簡介
  有時候nginx發布了新BUG或者添加了新的功能時,想要更新的時候服務又不能中斷,這時候就要用到nginx的平滑升級了。

  該腳本同樣適用於添加新擴展,添加新擴展的時候只需要把更新的版本修改為當前版本,更新的時候把需要添加的擴展加上去即可。
我這裏nginx安裝目錄為/usr/local/nginx
當前系統,阿裏雲ECS CentOS 7 64位

2、查看nginx版本與編譯信息
/usr/local/nginx/sbin/nginx -V
技術分享圖片

註:這裏的擴展要記錄下來(重要),等下升級的時候用到,如果有需要添加新信息可以一起編譯。
技術分享圖片

3、使用shell腳本平滑升級nginx版本
shell 腳本:---------------------------------------------------------------------------------------------------------------------------------

#!/bin/bash
source ./cnl_function.sh
source ./cnl_install_lnmp_init.sh
#function of install nginx
updatenginx(){
cd /usr/local/src
[ -f nginx-1.15.9.tar.gz ] || wget http://nginx.org/download/nginx-1.15.9.tar.gz
tar -zxf nginx-1.15.9.tar.gz
cd nginx-1.15.9
myum pcre-devel
[ -d /usr/local/nginx ] && cp -R /usr/local/nginx /usr/local/nginx

date +%s
check_ok
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_v2_module \
--with-poll_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_dav_module \
--with-http_flv_module
#只編譯不安裝
make
check_ok
if [ -f /usr/local/nginx/sbin/nginx ]
then
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
check_ok
fi

cp /usr/local/src/nginx-1.15.9/objs/nginx /usr/local/nginx/sbin/
check_ok

kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
check_ok

}

read -p "Initialization completion, Enter (Y) to start update nginx1.15.6 :" n
if [ $n == ‘Y‘ ]
then
echo "Start update==============================================================================================================================>"
update_nginx
echo "The update_nginx make done"
else
echo "Cancel the update."
fi

shell 腳本:---------------------------------------------------------------------------------------------------------------------------------

4、腳本授權,運行腳本,並查看nginx版本是否升級成功
chmod o+x
./updata.sh
技術分享圖片

/usr/local/nginx/sbin/nginx -V
技術分享圖片

nginx通過shell腳本平滑升級版本