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_module


Options:
-?,-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

安装nginx源码包,您可以按照以下步骤进行操作: 1. 首先,您需要下载nginx源码包。您可以从官方网站上下载最新版本的源码包。您可以在http://nginx.org/download/找到最新的源码包下载链接。 2. 下载完成后,解压源码包。您可以使用tar命令来解压文件。例如,如果您的源码包是nginx-1.20.1.tar.gz,您可以使用以下命令进行解压: ``` tar -zxvf nginx-1.20.1.tar.gz ``` 3. 进入解压后的目录: ``` cd nginx-1.20.1 ``` 4. 在源码目录中,执行configure命令进行配置。该命令将根据您的系统环境进行一些必要的配置: ``` ./configure ``` 5. 配置完成后,运行make命令编译源码: ``` make ``` 6. 编译完成后,运行make install命令将nginx安装到系统中: ``` make install ``` 7. 安装完成后,您可以检查是否成功安装nginx。您可以使用id命令来检查是否存在nginx用户: ``` id nginx ``` 以上就是nginx源码安装的一般步骤。请注意,这里只提供了基本的安装步骤,具体的安装过程可能会因系统环境和配置需求而有所不同。建议您在安装前阅读官方文档或参考更详细的安装指南以确保正确安装nginx。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [nginx源码安装](https://blog.csdn.net/weixin_49185464/article/details/127326489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妍妍的宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值