当前位置: 首页 > news >正文

NGINX的源码安装

章节

1 NGINX 的源码安装

2 NGINX 核心配置详解

3 NGINX 之 location 匹配优先级

4 NGINX 基础参数与功能

目录

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环境变量

2.4 修改主配置文件

2.5 创建NGINX用户

2.6 启动NGINX

2.7 编写系统服务脚本

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实现 挂起旧进程开启新进程


1 NGINX获取

NGINX官方网站icon-default.png?t=N7T8https://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 协议
--with-http_v2_module \ 
# 启用 Real IP 模块,这对于处理反向代理的情况非常有用
--with-http_realip_module \ 
# 启用 GZIP 静态文件压缩模块,可以自动对静态文件进行压缩
--with-http_gzip_static_module \ 
# 启用 stub status 模块,可以提供基本的服务器状态页面
--with-http_stub_status_module \ 
# 使用系统 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

最后发现又出现了错误缺少OpenSSL

安装openssl-devel.x86_64

[root@RHEL-9 nginx-1.24.0]# yum install openssl-devel.x86_64 -y

在编译的最后面又出现错误了

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已经安装完成了

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相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有一个样板配置文件,是以.default为后缀,使用时可将其复制并将default后缀去掉即可。
# html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
# logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
# sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

2.3 配置NGINX环境变量

[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_moduleOptions:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显示版本和编译参数
-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 #
发送信号,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 创建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 编写系统服务脚本

[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进程发送USR2信号
  • master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin
  • master进程用新Nginx文件启动新master进程成为旧master的子进程,系统中将有新旧两个Nginx
  • 进程共同提供Web服务,当前新的请求仍然由旧Nginxworker进程进行处理,将新生成的master
  • 程的PID存放至新生成的pid文件nginx.pid
  • 向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止
  • 向旧master进程发送QUIT信号,关闭老master,并删除Nginx.pid.oldbin文件
  • 如果发现升级有问题,可以回滚∶向老master发送HUP,向新master发送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实现 挂起旧进程开启新进程

[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
#此时的版本已经变为 1.26.1 了 因为脚本文件发生了变化
[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 <old_pid>命令向旧版本的NGINX主进程发送USR2信号,
# 其中<old_pid>是旧版本主进程的PID。
# 发送USR2信号后,旧版本的主进程会创建一个新的主进程,并将旧的主进程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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Polars简明基础教程十一:可视化(一)
  • NTP时间服务器的工作原理解析
  • SSH端口转发
  • 12、springboot3 vue3开发平台-前端-记住我功能实现
  • Python向IP地址发送字符串
  • HTML静态网页成品作业(HTML+CSS)——原神介绍设计制作(4个页面)
  • 【Redis】如何从单机架构演化为分布式系统
  • 【JAVA入门】Day20 - 正则表达式
  • XSS之xss game
  • Epic Games 商店面向欧盟 iPhone 用户上线
  • K8S系列——(一)、Ubuntu上安装Helm
  • Java实现腾讯云人脸识别集成:如何为司机创建人脸模型
  • Angular由一个bug说起之九:AWS S3 文件下载问题
  • MySQL8 innoDB引擎的精髓
  • 日志管理工具 简介2 Graylog
  • python3.6+scrapy+mysql 爬虫实战
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • Angular2开发踩坑系列-生产环境编译
  • Cookie 在前端中的实践
  • GraphQL学习过程应该是这样的
  • javascript 哈希表
  • learning koa2.x
  • Python_网络编程
  • Python3爬取英雄联盟英雄皮肤大图
  • Python利用正则抓取网页内容保存到本地
  • Terraform入门 - 1. 安装Terraform
  • vue:响应原理
  • vue2.0项目引入element-ui
  • 阿里云购买磁盘后挂载
  • 创建一种深思熟虑的文化
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 技术胖1-4季视频复习— (看视频笔记)
  • 简单数学运算程序(不定期更新)
  • 我的zsh配置, 2019最新方案
  • 学习HTTP相关知识笔记
  • ​1:1公有云能力整体输出,腾讯云“七剑”下云端
  • ​学习笔记——动态路由——IS-IS中间系统到中间系统(报文/TLV)​
  • (2022 CVPR) Unbiased Teacher v2
  • (7)svelte 教程: Props(属性)
  • (javascript)再说document.body.scrollTop的使用问题
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (初研) Sentence-embedding fine-tune notebook
  • (附源码)springboot优课在线教学系统 毕业设计 081251
  • (七)Knockout 创建自定义绑定
  • (限时免费)震惊!流落人间的haproxy宝典被找到了!一切玄妙尽在此处!
  • (转)http-server应用
  • (转)负载均衡,回话保持,cookie
  • .describe() python_Python-Win32com-Excel
  • .NET 8.0 发布到 IIS
  • .NET MVC 验证码
  • .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
  • .net下的富文本编辑器FCKeditor的配置方法
  • .net与java建立WebService再互相调用