创建虚拟机,内存 4G 处理器 2 核,NAT 网络。
准备 Nginx 源码包,1.24 版本,用于实验。
一、下载 Nginx 源码包
Nginx 官网:<www.nginx.org>
download 下载相关的版本,如下图:

wget 下载 Nginx 源码包。
bash
wget https://nginx.org/download/nginx-1.24.0.tar.gz
解压。
bash
tar xzf nginx-1.24.0.tar.gz
解压后生成一个目录 nginx-1.24.0。
目录里 configure 是环境检测文件。
二、关闭 debug 功能
关闭 debug 功能,使安装软件占用内存变小。
bash
vim /root/nginx-1.24.0/auto/cc/gcc

三、检测环境
检测环境缺哪些东西。
bash
./configure --help
./configure \
--prefix=/usr/local/nginx \ # 安装目录
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module # 支持tcp的ssl加密
以上选择检测目前的环境是否满足这些选择,检测过程中会出现以下内容。
bash
checking for C compiler ... not found # 缺少c语言编译器
dnf install -y gcc # 安装gcc编译器
./configure: error: the HTTP rewrite module requires the PCRE library. # 缺少PCRE
dnf search pcre
dnf install -y pcre-devel.x86_64 # 安装pcre开发包
./configure: error: SSL modules require the OpenSSL library. # 缺少OpenSSL
dnf search openssl
dnf install -y openssl-devel.x86_64 # 安装openssl开发包
./configure: error: the HTTP gzip module requires the zlib library. # 缺少zlib
dnf search zlib
dnf install -y zlib-devel.x86_64 # 安装zlib开发包
检测完成会出现以下内容。

环境检测通过后形成一个 Makefile 文件,Makefile 生成以后,objs 文件里就会产生编译后生成的文件,这些文件才是软件运行时用所需要的。
四、编译
make 编译, -j2 不建议做,因为会导致内存溢出,make 就是将源文件与系统 c 语言服务器进行对接,编译的快慢取决于系统 CPU 的运算速率和内存大小,如果没有 make 命令,使用 dnf install -y make 安装 make。
make 命令用于根据 Makefile 文件中的规则和依赖关系,编译和构建项目。Makefile 中定义了如何编译源代码、生成目标文件、链接库等一系列构建步骤。当执行 make 命令时,它会检查文件的修改时间和依赖关系,只重新编译那些被修改或其依赖项有更新的部分,从而提高编译效率。
bash
make
objs/ 文件里生成 nginx(nginx 启动程序)和 src(包含 nginx 用到的所有模块/插件)。

接下啦需要把 objs 文件的内容拷贝到 /usr/local/nginx/ 里,执行 make install 命令,自动拷贝文件。
make install 通常是在 make 成功完成构建之后执行的。它的作用是将编译生成的可执行文件、库文件、配置文件等安装到指定的目录中,这些目录通常是系统的标准位置,以便软件能够在系统中正常运行和使用。
bash
make install

五、创建用户
创建启动 nginx 服务所需的用户。
bash
useradd -s /sbin/nologin -M nginx
id nginx
编辑环境变量文件,把 nginx 软件的命令执行路径添加到环境变量中,使 nginx 像系统命令那样进行。
bash
vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin # 这行添加进~/.bash_profile里
source ~/.bash_profile # 加载环境变量

运行 nginx,并查看 nginx 的进程和端口(检查 nginx 服务是否启动)。
bash
nginx

查看 nginx 版本信息。
bash
curl -I ip

六、拓展:修改版本信息
拓展:版本信息可改(隐藏版本信息)。
进入 /root/nginx-1.24.0/src/core 目录修改 nginx.h 文件内容。

七、配置服务启动脚本
在 /lib/systemd/system/ 目录下,创建一个 nginx.service 文件用于写服务启动脚本,注意:文件必须写在此目录下。
以下是服务启动脚本的具体内容,源码安装 nginx 的 pid 文件默认在 /usr/local/nginx/logs/nginx.pid 如果你的 pid 文件目录有所变化,这里需要跟着变化路径。
bash
vim /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
重新加载其配置文件,再启动 nginx 服务。
bash
systemctl daemon-reload
systemctl enable --now nginx
可以看到服务正常启动运行。
