nginx使用熱部署添加新模塊

簡介

當初次編譯安裝nginx時,http_ssl_module 模塊默認是不編譯進nginx的二進制文件當中,若是須要添加 ssl 證書。也就是使用 https協議。那麼則須要添加 http_ssl_module 模塊。假設你的nginx安裝包目錄在/home/johnson/nginx-1.17.5,下面會用到nginx

小知識點:使用/home/johnson/nginx-1.17.5/configure --help 命令,能夠看到不少 --with--without 開頭的模塊選項。ubuntu

  • --with:默認是不編譯進nginx的二進制文件當中
  • --without:默認編譯進nginx的二進制文件當中
/home/johnson/nginx-1.17.5/configure --help
...
  --with-http_ssl_module             enable ngx_http_ssl_module
...
  --without-http_gzip_module         disable ngx_http_gzip_module
...
複製代碼

能夠看到http_ssl_module 模塊默認是不編譯進nginx的二進制文件當中。bash

編譯添加新模塊

當須要添加http_ssl_module模塊時,命令以下:微信

/home/johnson/nginx-1.17.5/configure --with-http_ssl_module
複製代碼

執行完該命令後,能夠在/home/johnson/nginx-1.17.5/objs/ngx_modules.c文件中看到哪些模塊要安裝到nginx中。以下:ui

ngx_module_t *ngx_modules[] = {
    &ngx_core_module,
...
    &ngx_http_ssl_module,
...

複製代碼

能夠看到http_ssl_module模塊要安裝到nginx當中,而後使用make命令,把http_ssl_module編譯進nginx的二進制文件當中spa

cd /home/johnson/nginx-1.17.5
make
複製代碼

執行完上述命令後,/home/johnson/nginx-1.17.5/objs/nginx該文件就是編譯後的nginx二進制文件,而後我們就須要進行熱部署升級了。code

熱部署

假設你的nginx安裝目錄在/usr/local/nginx當中。 1.備份正在使用的nginx二進制文件cdn

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
複製代碼

2.使用最新的nginx二進制文件替換掉正在使用的nginx二進制文件blog

cp -r /home/johnson/nginx-1.17.5/objs/nginx /usr/local/nginx/sbin/ -f
複製代碼

3.查看正在運行nginx的master進程進程

ps -ef | grep nginx
root      6503     1  0 Jun23 ?        00:00:00 nginx: master process nginx
ubuntu   26317 19063  0 07:39 pts/0    00:00:00 grep --color=auto nginx
nobody   31869  6503  0 Jun27 ?        00:00:00 nginx: worker process
複製代碼

能夠看到,當前nginx的master進程號爲 6503。

4.告知正在運行的nginx的master進程,須要進行nginx升級

kill -USR2 6503
ps -ef | grep nginx
root      6503     1  0 Jun23 ?        00:00:00 nginx: master process nginx
root      7128  6503  0 08:05 ?        00:00:00 nginx: master process nginx
nobody    7129  7128  0 08:05 ?        00:00:00 nginx: worker process
root      7140 30619  0 08:05 pts/0    00:00:00 grep --color=auto nginx
nobody   31869  6503  0 Jun27 ?        00:00:00 nginx: worker process

複製代碼

能夠看到,執行完命令後會啓動新的nginx的master進程,新的master進程是由舊的master進程啓動的。若是沒有啓動,那麼能夠使用nginx -t查看配置文件是否正確,若是沒有問題,那麼通常是可以啓動新的master進程。

5.告知舊的nginx master進程,請優雅的關閉全部舊的worker進程

kill -WINCH 6503
root@VM-0-13-ubuntu:/usr/local/nginx# ps -ef | grep nginx
root      6503     1  0 Jun23 ?        00:00:00 nginx: master process nginx
root      7128  6503  0 08:05 ?        00:00:00 nginx: master process nginx
nobody    7129  7128  0 08:05 ?        00:00:00 nginx: worker process
root      9431 30619  0 08:17 pts/0    00:00:00 grep --color=auto nginx
複製代碼

能夠看到,舊的worker進程都已經關閉掉。若是發生了錯誤,則能夠使用nginx -s reload命令回退到舊版本當中。
若是發現一切都正常,沒有問題,那麼你能夠關閉掉舊的master進程。kill -9 6503,此時新的master進程的父進程(舊的master進程)被關閉後,那麼會把他的父進程改爲系統進程,系統進程的進程號爲 1。
此時就完美添加了新模塊和實現熱部署了!!!

總結

由於初次編譯nginx,可能沒想到要用到其餘模塊,或許也可能刪除某些模塊。此時每每就須要使用到nginx的熱部署。

參考文章:極客時間Nginx核心知識100講: 第10講

我的博客網址: colablog.cn/

若是個人文章幫助到您,能夠關注個人微信公衆號,第一時間分享文章給您

微信公衆號
相關文章
相關標籤/搜索