簡介
當初次編譯安裝nginx時,http_ssl_module
模塊默認是不編譯進nginx的二進制文件當中,如果需要添加 ssl 證書。也就是使用 https協議。那么則需要添加 http_ssl_module
模塊。假設你的nginx安裝包目錄在/home/johnson/nginx-1.17.5
,下面會用到
小知識點:使用/home/johnson/nginx-1.17.5/configure --help
命令,可以看到很多 --with
和 --without
開頭的模塊選項。
- --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的二進制文件當中。
編譯添加新模塊
當需要添加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中。如下:
ngx_module_t *ngx_modules[] = {
&ngx_core_module,
...
&ngx_http_ssl_module,
...
可以看到http_ssl_module
模塊要安裝到nginx當中,然后使用make
命令,把http_ssl_module
編譯進nginx的二進制文件當中
cd /home/johnson/nginx-1.17.5
make
執行完上述命令后,/home/johnson/nginx-1.17.5/objs/nginx
該文件就是編譯后的nginx二進制文件,然后咱們就需要進行熱部署升級了。
熱部署
假設你的nginx安裝目錄在/usr/local/nginx
當中。
1.備份正在使用的nginx二進制文件
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
2.使用最新的nginx二進制文件替換掉正在使用的nginx二進制文件
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的熱部署。
個人博客網址: https://colablog.cn/
如果我的文章幫助到您,可以關注我的微信公眾號,第一時間分享文章給您