柚子快報邀請碼778899分享:運維 NGINX的源碼安裝
柚子快報邀請碼778899分享:運維 NGINX的源碼安裝
章節(jié)
1 NGINX 的源碼安裝
2 NGINX 核心配置詳解
3 NGINX 之 location 匹配優(yōu)先級
4 NGINX 基礎(chǔ)參數(shù)與功能
目錄
1 NGINX獲取
2 NGINX編譯安裝
2.1 安裝所需要的依賴包
2.1.1 下載gcc
2.1.2 下載PCRE
2.1.3 下載OpenSSL
2.1.4 下載 zlib
2.2 重新編譯
2.2.1 ./configure
2.2.2?make編譯
2.2.3 make install 安裝
2.2.4 查看所生成目錄
2.3 配置NGINX環(huán)境變量
2.4 修改主配置文件
2.5 創(chuàng)建NGINX用戶
2.6 啟動NGINX
2.7 編寫系統(tǒng)服務腳本
3 NGINX模塊增加
3.1 清除之前編譯的重新編譯
3.2 覆蓋舊的腳本
4 NGINX的平滑升級
4.1 平滑升級與回滾理論
4.2 平滑升級實踐
4.2.1 解壓新版本NGINX編譯
4.2.2 拷貝編譯好的新腳本
4.2.3 kill -s USR2實現(xiàn) 掛起舊進程開啟新進程
1 NGINX獲取
NGINX官方網(wǎng)站https://nginx.org/en/
2 NGINX編譯安裝
[root@RHEL-9 ~]# mkdir /usr/local/src/nginx_source
[root@RHEL-9 ~]# cd /usr/local/src/nginx_source/
[root@RHEL-9 nginx_source]# wget https://nginx.org/download/nginx-1.24.0.tar.gz
[root@RHEL-9 nginx_source]# tar xzf nginx-1.24.0.tar.gz
[root@RHEL-9 nginx_source]# ls
echo-nginx-module-0.63.tar.gz nginx-1.24.0.tar.gz
nginx-1.24.0 nginx-1.26.1.tar.gz
[root@RHEL-9 nginx_source]# cd nginx-1.24.0/
[root@RHEL-9 nginx-1.24.0]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@RHEL-9 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module
checking for OS
+ Linux 5.14.0-162.6.1.el9_1.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
以下是解釋
# 編譯和配置 Nginx 的命令
./configure \
# 設置 Nginx 的安裝路徑為 /usr/local/nginx
--prefix=/usr/local/nginx \
# 指定 Nginx 進程將以 nginx 用戶身份運行
--user=nginx \
# 指定 Nginx 進程的組為 nginx
--group=nginx \
# 啟用 HTTP SSL 模塊,使得 Nginx 支持 HTTPS 連接
--with-http_ssl_module \
# 啟用 HTTP/2 模塊,支持 HTTP/2 協(xié)議
--with-http_v2_module \
# 啟用 Real IP 模塊,這對于處理反向代理的情況非常有用
--with-http_realip_module \
# 啟用 GZIP 靜態(tài)文件壓縮模塊,可以自動對靜態(tài)文件進行壓縮
--with-http_gzip_static_module \
# 啟用 stub status 模塊,可以提供基本的服務器狀態(tài)頁面
--with-http_stub_status_module \
# 使用系統(tǒng) PCRE 庫
--with-pcre \
# 啟用 Stream 模塊,用于處理 TCP 和 UDP 流量
--with-stream \
# 啟用 Stream SSL 模塊,使得 Stream 模塊支持 TLS/SSL 加密連接
--with-stream_ssl_module
2.1 安裝所需要的依賴包
2.1.1 下載gcc
[root@RHEL-9 nginx-1.24.0]# yum install gcc -y
再次編譯
在末尾的時候能看到這樣的報錯
2.1.2 下載PCRE
[root@RHEL-9 nginx-1.24.0]# yum search PCRE
[root@RHEL-9 nginx-1.24.0]# yum install pcre-devel.x86_64 -y
安裝過后再次編譯
2.1.3 下載OpenSSL
最后發(fā)現(xiàn)又出現(xiàn)了錯誤缺少OpenSSL
安裝openssl-devel.x86_64
[root@RHEL-9 nginx-1.24.0]# yum install openssl-devel.x86_64 -y
在編譯的最后面又出現(xiàn)錯誤了
2.1.4 下載 zlib
[root@RHEL-9 nginx-1.24.0]# yum install zlib-devel.x86_64 -y
2.2 重新編譯
2.2.1 ./configure
[root@RHEL-9 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module
這樣子證明NGINX已經(jīng)安裝完成了
2.2.2?make編譯
2.2.3 make install 安裝
2.2.4 查看所生成目錄
[root@Nginx nginx-1.24.0]# ls /usr/local/nginx/
conf html logs sbin
# conf:保存nginx所有的配置文件,其中nginx.conf是nginx服務器的最核心最主要的配置文件,其他的.conf則是用來配置nginx相關(guān)的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params兩個文件,配置文件一般都有一個樣板配置文件,是以.default為后綴,使用時可將其復制并將default后綴去掉即可。
# html目錄中保存了nginx服務器的web文件,但是可以更改為其他目錄保存web文件,另外還有一個50x的web文件是默認的錯誤頁面提示頁面。
# logs:用來保存nginx服務器的訪問日志錯誤日志等日志,logs目錄可以放在其他路徑,比如/var/logs/nginx里面。
# sbin:保存nginx二進制啟動腳本,可以接受不同的參數(shù)以實現(xiàn)不同的功能。
2.3 配置NGINX環(huán)境變量
[root@RHEL-9 ~]# source ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PATH=$PATH:/usr/local/nginx/sbin
[root@RHEL-9 ~]# source ~/.bash_profile
# 查看版本號
[root@RHEL-9 ~]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #顯示版本和編譯參數(shù)
-t : test configuration and exit #測試配置文件是否異
-T : test configuration, dump it and exit #測試并打印
-q : suppress non-error messages during configuration testing #靜默
模式
-s signal : send signal to a master process: stop, quit, reopen, reload #
發(fā)送信號,reload信號 會生成新的worker,但master不會重新生成
-p prefix : set prefix path (default: /etc/nginx/) #指定Nginx 目錄
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #
配置文件路徑
-g directives : set global directives out of configuration file #設置全局指令,注意和
配置文件不要同時配置,否則沖突
2.4 修改主配置文件
[root@RHEL-9 ~]# vim /usr/local/nginx/conf/nginx.conf
2.5 創(chuàng)建NGINX用戶
[root@RHEL-9 ~]# useradd -s /sbin/nologin -M nginx
2.6 啟動NGINX
啟動NGINX并查看端口?
# NGINX 啟動
[root@RHEL-9 ~]# nginx
[root@RHEL-9 ~]# ps aux | grep nginx
root 2340 0.0 0.0 9864 928 ? Ss 19:11 0:00 nginx: master process nginx
nginx 2341 0.0 0.2 13752 4532 ? S 19:11 0:00 nginx: worker process
root 2343 0.0 0.1 221680 2108 pts/1 S+ 19:11 0:00 grep --color=auto nginx
2.7 編寫系統(tǒng)服務腳本
[root@RHEL-9 sbin]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@RHEL-9 sbin]# systemctl daemon-reload
[root@RHEL-9 sbin]# killall nginx
[root@RHEL-9 sbin]# systemctl start nginx
[root@RHEL-9 sbin]# systemctl stop nginx
[root@RHEL-9 sbin]# systemctl start nginx
[root@RHEL-9 sbin]# systemctl status nginx
[root@RHEL-9 sbin]# systemctl restart nginx
3 NGINX模塊增加
3.1 清除之前編譯的重新編譯
# 清除之前的編譯
[root@RHEL-9 nginx-1.24.0]# make clean
rm -rf Makefile objs
[root@RHEL-9 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--add-module=/usr/local/src/nginx_source/echo-nginx-module-0.63/
# make 不要make install
[root@RHEL-9 nginx-1.24.0]# make
3.2 覆蓋舊的腳本
[root@RHEL-9 nginx-1.24.0]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@RHEL-9 nginx-1.24.0]# ls objs/
addon nginx ngx_auto_headers.h src
autoconf.err nginx.8 ngx_modules.c
Makefile ngx_auto_config.h ngx_modules.o
[root@RHEL-9 nginx-1.24.0]# \cp -f objs/nginx /usr/local/nginx/sbin/nginx
[root@RHEL-9 nginx-1.24.0]# nginx -V
nginx version: nginx/1.24.0
built by gcc 11.4.1 20231218 (Red Hat 11.4.1-3) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-stream_ssl_module --add-module=/usr/local/src/nginx_source/echo-nginx-module-0.63/
查看模塊是否增加成功
4 NGINX的平滑升級
4.1 平滑升級與回滾理論
將舊Nginx二進制文件換成新Nginx程序文件(注意先備份) 向master進程發(fā)送USR2信號 master進程修改pid文件名加上后綴.oldbin,成為nginx.pid.oldbin master進程用新Nginx文件啟動新master進程成為舊master的子進程,系統(tǒng)中將有新舊兩個Nginx主 進程共同提供Web服務,當前新的請求仍然由舊Nginx的worker進程進行處理,將新生成的master進 程的PID存放至新生成的pid文件nginx.pid 向舊的Nginx服務進程發(fā)送WINCH信號,使舊的Nginx worker進程平滑停止 向舊master進程發(fā)送QUIT信號,關(guān)閉老master,并刪除Nginx.pid.oldbin文件 如果發(fā)現(xiàn)升級有問題,可以回滾∶向老master發(fā)送HUP,向新master發(fā)送QUIT
4.2 平滑升級實踐
4.2.1 解壓新版本NGINX編譯
[root@RHEL-9 nginx_source]# tar -xzf nginx-1.26.1.tar.gz
[root@RHEL-9 nginx_source]# cd nginx-1.26.1/
[root@RHEL-9 nginx-1.26.1]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@RHEL-9 nginx-1.26.1]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--add-module=/usr/local/src/nginx_source/echo-nginx-module-0.63/
[root@RHEL-9 nginx-1.26.1]# make
4.2.2 拷貝編譯好的新腳本
[root@RHEL-9 nginx-1.26.1]# cp objs/nginx /usr/local/nginx/sbin/nginx.new
# 啟動NGINX
[root@RHEL-9 sbin]# nginx
[root@RHEL-9 sbin]# ps aux | grep nginx
root 9331 0.0 0.0 9892 940 ? Ss 20:13 0:00 nginx: master process nginx
nginx 9332 0.0 0.2 13792 4748 ? S 20:13 0:00 nginx: worker process
root 9334 0.0 0.1 221680 2340 pts/1 S+ 20:13 0:00 grep --color=auto nginx
[root@RHEL-9 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Thu, 15 Aug 2024 12:14:26 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 08:31:50 GMT
Connection: keep-alive
ETag: "66bdbcf6-267"
Accept-Ranges: bytes
[root@RHEL-9 sbin]# mv nginx nginx.old
[root@RHEL-9 sbin]# mv nginx.new nginx
# 此時的版本還是1.24
[root@RHEL-9 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Thu, 15 Aug 2024 12:14:52 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 08:31:50 GMT
Connection: keep-alive
ETag: "66bdbcf6-267"
Accept-Ranges: bytes
4.2.3 kill -s USR2實現(xiàn) 掛起舊進程開啟新進程
[root@RHEL-9 sbin]# ps aux | grep nginx
root 9331 0.0 0.0 9892 940 ? Ss 20:13 0:00 nginx: master process nginx
nginx 9332 0.0 0.2 13792 4748 ? S 20:13 0:00 nginx: worker process
root 9352 0.0 0.1 221680 2116 pts/1 S+ 20:15 0:00 grep --color=auto nginx
#此時的版本已經(jīng)變?yōu)?1.26.1 了 因為腳本文件發(fā)生了變化
[root@RHEL-9 sbin]# nginx -v
nginx version: nginx/1.26.1
#
[root@RHEL-9 sbin]# kill -USR2 9331
[root@RHEL-9 sbin]# ps aux | grep nginx
root 9331 0.0 0.1 9892 2692 ? Ss 20:13 0:00 nginx: master process nginx
nginx 9332 0.0 0.2 13792 4748 ? S 20:13 0:00 nginx: worker process
root 9354 0.0 0.3 9764 5960 ? S 20:15 0:00 nginx: master process nginx
nginx 9355 0.0 0.2 13780 4768 ? S 20:15 0:00 nginx: worker process
root 9357 0.0 0.1 221680 2412 pts/1 S+ 20:15 0:00 grep --color=auto nginx
# 使用kill -s USR2
# 其中
# 發(fā)送USR2信號后,舊版本的主進程會創(chuàng)建一個新的主進程,并將舊的主進程PID文件重命名為
# nginx.pid.oldbin,然后啟動新版本的主進程。
[root@RHEL-9 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.26.1 #新版本生效
Date: Thu, 15 Aug 2024 12:15:57 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Thu, 15 Aug 2024 08:31:50 GMT
Connection: keep-alive
ETag: "66bdbcf6-267"
Accept-Ranges: bytes
# 回收舊進程
[root@RHEL-9 sbin]# kill -WINCH 9331
[root@RHEL-9 sbin]# ps aux | grep nginx
root 9331 0.0 0.1 9892 2692 ? Ss 20:13 0:00 nginx: master process nginx
root 9354 0.0 0.3 9764 5960 ? S 20:15 0:00 nginx: master process nginx
nginx 9355 0.0 0.2 13780 4768 ? S 20:15 0:00 nginx: worker process
root 9361 0.0 0.1 221680 2328 pts/1 S+ 20:16 0:00 grep --color=auto nginx
[root@RHEL-9 sbin]# kill -s QUIT 9331
[root@RHEL-9 sbin]# ps aux | grep nginx
root 9354 0.0 0.3 9764 5960 ? S 20:15 0:00 nginx: master process nginx
nginx 9355 0.0 0.2 13780 4768 ? S 20:15 0:00 nginx: worker process
root 9378 0.0 0.1 221680 2260 pts/1 S+ 20:39 0:00 grep --color=auto nginx
柚子快報邀請碼778899分享:運維 NGINX的源碼安裝
推薦鏈接
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。