「云原生 | Nginx」手把手教你通过源码构建 Nginx

2 篇文章 0 订阅
2 篇文章 0 订阅

一、介绍

NGINX 是一款用于 Web 服务、反向代理、缓存、负载平衡、媒体流等的开源软件。它最初是为实现最高性能和稳定性而设计的 Web 服务器。

二、安装

1. 下载并解压

下载 nginx 源码包,并解压到自己本机。博主本次操作目录如下:

[root@shangliang nginx-1.22.0]# pwd
/usr/local/nginx/nginx-1.22.0
[root@shangliang nginx-1.22.0]# ls
CHANGES  CHANGES.ru  LICENSE  README  auto  conf  configure  contrib  html  man  src

2. 构建 Nginx

通过 configure 命令来进行构建,构建后会生成一个 Makefile 文件

2.1 参数说明

configure 命令支持以下参数:
(参数前面为两个连续的 - ,文章内显示的效果为一个 -

  1. –help
    输出帮助信息
  2. –prefix=PATH
    定义程序安装目录,后续程序执行的相对目录均为此目录。默认:/usr/local/nginx
  3. –sbin-path=PATH
    定义 nginx 可执行文件。默认:<prefix目录>/sbin/nginx
  4. –modules-path=PATH
    定义将安装的 nginx 动态模块的目录。默认:<prefix目录>/modules
  5. –conf-path=PATH
    定义配置文件。默认:prefix/conf/nginx.conf
  6. –error-log-path=path
    定义错误告警文件。默认:prefix/logs/error.log
  7. –pid-path=path
    定义主进程 ID 的文件。默认:prefix/logs/nginx.pid
  8. –user=name
    运行用户名称。默认:nobody
  9. –with- XXX
    构建 XXX 模块
  10. –without- XXX
    不构建 XXX 模块

2.2 构建命令示例

./configure --sbin-path=/usr/local/nginx-1.22/nginx \
--conf-path=/usr/local/nginx-1.22/nginx.conf \
--pid-path=/usr/local/nginx-1.22/nginx.pid
2.2.1 报错一 requires the PCRE library.

./configure: error: the HTTP rewrite module requires the PCRE library.
方式一:
构建命令增加 --without-http_rewrite_module ,不构建 rewrite 模块。此种方式会导致我们不能使用 rewrite 模块的相关功能。
方式二:
安装 PCRE 库。命令 yum -y install pcre-devel

2.2.2 报错二 requires the zlib library.

./configure: error: the HTTP gzip module requires the zlib library.
方式一: 构建命令增加 --without-http_gzip_module 不构建 gzip 模块。此种方式会导致我们不能使用 gzip 模块的相关功能。
方式二: 安装 zlib 库。命令 yum install -y zlib-devel

2.3 开始构建

构建命令:

./configure --sbin-path=/usr/local/nginx-1.22/nginx \
--conf-path=/usr/local/nginx-1.22/nginx.conf \
--pid-path=/usr/local/nginx-1.22/nginx.pid

如下信息,代表构建成功

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx-1.22/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx-1.22"
  nginx configuration file: "/usr/local/nginx-1.22/nginx.conf"
  nginx pid file: "/usr/local/nginx-1.22/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

依次执行 make 命令和 make install 命令完成构建

2.4 构建完成

进入我们构建时设置的目录,查看文件信息

[root@shangliang nginx-1.22]# pwd
/usr/local/nginx-1.22
[root@shangliang nginx-1.22]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx       nginx.conf.default  scgi_params.default  uwsgi_params.default
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf  scgi_params         uwsgi_params         win-utf
[root@shangliang nginx-1.22]#

3. 启动验证

进入 nginx 执行命令目录,执行 ./nginx -c nginx.conf (如未指定配置文件,则会取 --conf-path 的信息作为配置文件)

报错:nginx: [emerg] open() "/usr/local/nginx/nginx.conf" failed (2: No such file or directory)
原因: 因为我们构建的时候没有设置 --prefix 参数,默认使用 /usr/local/nginx ,所以会以该默认目录作为相对目录查询配置文件 nginx.conf

调整启动命令: ./nginx -c /usr/local/nginx-1.22/nginx.conf
访问 http://127.0.0.1:80 , 验证安装是否成功。

[root@shangliang nginx-1.22]# curl http://127.0.0.1:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

三、常用命令

1. 启动 ./nginx

./nginx 启动 nginx ,默认配置文件为 --conf-path 参数,可以通过 -c 文件 指定使用的配置文件,如使用相对地址,则相对地址为 --prefix 目录。

2. 停止 ./nginx -s stop|quit

./nginx -s quit 方式是等待 nginx 进程将任务处理完毕后停止
./nginx -s stop 方式是强制停止 nginx ,效果同 kill 命令

3. 重载配置 ./nginx -s reload

修改 nginx 配置文件后,可通过 ./nginx -s reload 命令重新加载配置文件而不需要停止 nginx 然后再启动。

4. 查看 nignx 版本

./nginx -v

[root@shangliang nginx-1.22]# ./nginx -v
nginx version: nginx/1.22.0

4. 查看编译参数

./nginx -V

[root@shangliang nginx-1.22]# ./nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
configure arguments: --sbin-path=/usr/local/nginx-1.22/nginx --conf-path=/usr/local/nginx-1.22/nginx.conf --pid-path=/usr/local/nginx-1.22/nginx.pid

5. 查看默认模块

cat auto/options | grep YES 此命令需要在 nginx 源码路径下查看。

[root@shangliang nginx-1.22.0]# cat auto/options | grep YES
HTTP=YES
HTTP_CACHE=YES
HTTP_CHARSET=YES
HTTP_GZIP=YES
HTTP_SSI=YES
HTTP_ACCESS=YES
HTTP_AUTH_BASIC=YES
HTTP_MIRROR=YES
HTTP_USERID=YES
HTTP_AUTOINDEX=YES
HTTP_GEO=YES
HTTP_MAP=YES
......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值