nginx之熱部署,以及版本回滾


熱部署的概念:當從老版本替換為新版本的nginx的時候,如果不熱部署的話,會需要取消nginx服務並重啟服務才能替換成功,這樣的話會使正在訪問的用戶在斷開連接,所以為了不影響用戶的體驗,且需要版本升級,就需要熱部署來升級版本
版本回滾的概念:當新版本上線之后出現問題,需要回到老版本,這時候就需要做版本回滾,其實就是在你做版本升級的時候,將老版本備份以下,然后替換新版本,之后殺死新版本的進程便可以
實驗步驟:1:先從官網上拷貝nginx-1.14.2版本的鏈接,且需要源碼部署
2:解壓在/opt/目錄下
3:創建nginx的進程 useradd -u 998 -s /sbin/nologin nginx
4:cd /opt/nginx-1.14.2進行預編譯和編譯安裝,編譯的話需要先安裝devlopment或者安裝gcc,gcc-c++,yum -y groupinstall devlopment或者yum -y gcc gcc-c++前面時間需要久一點,后面快一寫
5:預編譯的命令是$./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
$make
$make install
6:用echo $?檢查有沒有安裝成功,成功的話則在下面設置控制命令nginxctl
7:$vi /usr/bin/nginxctl
#!/usr/bin/env bash
case $1 in
stop)
/usr/local/nginx/sbin/nginx -s quit
;;
start)
/usr/local/nginx/sbin/nginx
;;
reload)
/usr/local/nginx/sbin/nginx -s reload
;; *)
echo "please input (start|stop|reload)"
;;
esac

~ 8:chmod a+x /usr/bin/nginxctl #設置文件的執行權限
9:nginxctl start #啟動nginx服務
10:這時候從瀏覽器上輸入ip地址,便會顯示nginx訪問首頁
11:現在開始進行熱部署,將版本升級到nginx-16.0.1,從官網上拷貝下載鏈接
12:跟之前安裝一樣,不過到make之后就不一樣了,不需要make install
13:[root@web3 nginx-1.16.1]# mv /usr/local/nginx/sbin/{nginx,nginx.old}#備份一下nginx-14.0.2版本,防止版本需要回退
[root@web3 nginx-1.16.1]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@web3 nginx-1.16.1]# cd objs #新版本的nginx命令在objs里
nginx ngx_auto_config.h ngx_modules.c
nginx.8 ngx_auto_headers.h ngx_modules.o
[root@web3 nginx-1.16.1]# cp objs/nginx /usr/local/nginx/sbin/nginx #需要將新版本的nginx命令替換掉原先版本的
[root@web3 nginx-1.16.1]# ps aux|grep nginx #此時查看nginx進程,看到有nginx進程在運行,還是老版本的進程
root 51891 0.0 0.0 20544 600 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 51893 0.0 0.1 20988 1560 ? S 21:49 0:00 nginx: worker process
root 66744 0.0 0.0 112708 984 pts/0 R+ 22:06 0:00 grep --color=auto nginx
[root@web3 nginx-1.16.1]# kill -USR2 51891 #優雅的關閉老版本進程,熱部署主要就是依靠這條命令,正在訪問的用戶依然訪問老版本,新用戶訪問則會訪問到新版本的nginx
[root@web3 nginx-1.16.1]# ps aux |grep nginx #這時候看進程發現有4個進程,新老版本都有
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 51893 0.0 0.1 20988 1560 ? S 21:49 0:00 nginx: worker process
root 67798 0.1 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1328 ? S 22:07 0:00 nginx: worker process
root 68006 0.0 0.0 112708 984 pts/0 R+ 22:07 0:00 grep --color=auto nginx
[root@web3 nginx-1.16.1]# kill -WINCH 51891 #這條命令的意思是讓老版本不要去檢測新用戶訪問的響應
[root@web3 nginx-1.16.1]# ps aux|grep nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1328 ? S 22:07 0:00 nginx: worker process
root 69592 0.0 0.0 112708 980 pts/0 R+ 22:09 0:00 grep --color=auto nginx
[root@web3 nginx-1.16.1]# /usr/local/nginx/sbin/nginx -v #這時候檢測版本就是1.16.1
nginx version: nginx/1.16.1
2版本回滾:
版本回滾和之前差不多,只是有一個問題需要注意,因為新版本不行,所以直接用kill命令就好,如果再需要升級版本,再進行熱部署一遍即可
[root@web3 nginx-1.16.1]# vi /usr/bin/nginxctl
[root@web3 nginx-1.16.1]# mv /usr/local/nginx/sbin/{nginx,nginx.old-1.16.1}
[root@web3 nginx-1.16.1]# ps aux |grep nginx
root 21161 0.0 0.0 112708 984 pts/0 R+ 23:37 0:00 grep --color=auto nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1572 ? S 22:07 0:00 nginx: worker process
[root@web3 nginx-1.16.1]# mv /usr/local/nginx/sbin/{nginx.old,nginx}
[root@web3 nginx-1.16.1]# ps aux|grep nginx
root 22307 0.0 0.0 112708 984 pts/0 R+ 23:39 0:00 grep --color=auto nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 67799 0.0 0.1 21004 1572 ? S 22:07 0:00 nginx: worker process
[root@web3 nginx-1.16.1]# kill -USR2 51891
[root@web3 nginx-1.16.1]# kill -9 67799
[root@web3 nginx-1.16.1]# ps aux|grep nginx
nginx 23560 1.1 0.1 21004 1328 ? S 23:40 0:00 nginx: worker process
root 23638 0.0 0.0 112708 984 pts/0 R+ 23:41 0:00 grep --color=auto nginx
root 51891 0.0 0.0 20544 792 ? Ss 21:49 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 67798 0.0 0.1 20552 1588 ? S 22:07 0:00 nginx: master process /usr/local/nginx/sbin/nginx
[root@web3 nginx-1.16.1]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM