0x00 前言
Nginx 是一个 HTTP 和反向代理服务器, 邮件代理服务器, 和通用 TCP/UDP 代理服务器, 最初由伊戈尔·西索耶夫(Igor Sysoev)撰写。采用编译安装可以根据自身需要自定义配置,让服务器有更高的安全性和稳定性。
0x01 环境说明
|-------------------|
| HECS(云耀云服务器) |
| Ubuntu 22.04 |
| Nginx 1.22.1 |
| GCC 11.3.0 |
| Make 4.3 |
| PCRE 8.39 |
| OpenSSL 3.0.2 |
| zlib 1.2.11 |
| GD 2.3.0 |
0x02 准备工作
在 Nginx 编译安装之前,我们先要提前做一些准备工作。需要安装 GCC 、 Make 、 PCRE 、 OpenSSL 、 zlib 、 GD 环境依赖项。
Ⅰ更新镜像源。

bash
apt-get update
Ⅱ安装 GCC 库。

bash
apt-get install gcc
Ⅲ安装 make 库。

bash
apt-get install make
Ⅳ安装 PCRE 库。

bash
apt-get install libpcre3 libpcre3-dev
Ⅴ安装 OpenSSL 库。

bash
apt-get install openssl
Ⅵ安装 zlib 库。

bash
apt-get install zlib1g zlib1g-dev
Ⅶ安装 GD 库。

bash
apt-get install libgd-dev libgdal-dev
0x03 Nginx下载
需要注意的是,使用编译安装时间会比较久。但是编译安装更适合生产环境,所以我个人更倾向于使用编译安装。
Ⅰ在开始之前我们需要先创建 nginx 用户,之所以这么做主要是为了降低服务器风险,让 nginx 以最小的权限运行。

useradd -s /sbin/nologin nginx
Ⅱ访问 nginx 官网。

bash
https://nginx.org/en/download.html
Ⅲ下载 Nginx1.22.1 。

bash
wget https://nginx.org/download/nginx-1.22.1.tar.gz
Ⅳ解压 Nginx1.22.1 。

bash
tar -zxvf nginx-1.22.1.tar.gz
0x04 运行配置脚本
Ⅰ打开解压缩后的 nginx-1.22.1 文件。

bash
cd nginx-1.22.1
Ⅱ运行 ./configure 配置脚本,这个脚本主要用来检查 nginx-1.22.1 环境所需的依赖项并配置编译选项。

bash
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-mail \
--with-pcre \
--with-stream \
--with-threads \
--with-file-aio \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_dav_module \
--with-mail_ssl_module \
--with-http_slice_module \
--with-stream_ssl_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_addition_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_auth_request_module \
--with-http_image_filter_module
关于配置命令的使用说明:
|---------------------------------|---------------------------------|
| 命令 | 说明 |
| --prefix=/usr/local/nginx | 指定安装目录为/usr/local/nginx。 |
| --user=nginx | 指定运行Nginx的用户为nginx。 |
| --group=nginx | 指定运行Nginx的用户组为nginx。 |
| --with-mail | 启用邮件模块,用于处理邮件代理。 |
| --with-pcre | 启用PCRE模块,用于支持正则表达式。 |
| --with-stream | 启用Stream模块,用于支持TCP和UDP代理。 |
| --with-threads | 启用Threads模块,用于支持多线程操作。 |
| --with-file-aio | 启用File AIO模块,用于支持异步文件IO。 |
| --with-http_v2_module | 启用HTTP/2模块,用于支持HTTP/2协议。 |
| --with-http_flv_module | 启用FLV模块,用于支持FLV流媒体。 |
| --with-http_mp4_module | 启用MP4模块,用于支持MP4流媒体。 |
| --with-http_ssl_module | 启用SSL模块,用于支持HTTPS。 |
| --with-http_sub_module | 启用Substitution模块,用于支持内容替换。 |
| --with-http_dav_module | 启用DAV模块,用于支持WebDAV。 |
| --with-mail_ssl_module | 启用Mail SSL模块,用于支持安全邮件传输。 |
| --with-http_slice_module | 启用Slice模块,用于支持分片上传。 |
| --with-stream_ssl_module | 启用Stream SSL模块,用于支持安全TCP和UDP代理。 |
| --with-http_realip_module | 启用Real IP模块,用于获取真实的客户端IP地址。 |
| --with-http_gunzip_module | 启用Gzip模块,用于支持压缩和解压缩。 |
| --with-http_addition_module | 启用Addition模块,用于支持响应内容的追加。 |
| --with-http_secure_link_module | 启用Secure Link模块,用于生成和验证安全链接。 |
| --with-http_stub_status_module | 启用Stub Status模块,用于获取Nginx的运行状态。 |
| --with-http_gzip_static_module | 启用Gzip Static模块,用于支持静态文件的压缩。 |
| --with-http_random_index_module | 启用Random Index模块,用于随机选择索引文件。 |
| --with-http_auth_request_module | 启用Auth Request模块,用于验证用户授权请求。 |
| --with-http_image_filter_module | 启用Image Filter模块,用于处理图像。 |
0x05 Nginx编译安装
Ⅰ编译安装 Nginx1.22.1 。

bash
make && make install
0x06 Nginx验证启动
Ⅰ访问 Nginx1.22.1 的启动文件夹。

bash
cd /usr/local/nginx/sbin
Ⅱ启动 Nginx1.22.1 。

bash
./nginx
Ⅲ验证 Nginx1.22.1 文件。

bash
./nginx -t
./nginx -v
Ⅳ查看 Nginx1.22.1 启动状态。

bash
netstat -ntlp | grep nginx
0x07 Nginx服务配置
Ⅰ创建 Nginx1.22.1 配置文件。

bash
vim /usr/lib/systemd/system/nginx.service
Ⅱ 编辑 Nginx1.22.1 运行配置文件,保存并退出。 注意:在写入配置文件时,最好将中文注释去除。

bash
[Unit]
Description=nginx - high performance web server # 描述服务功能
Documentation=http://nginx.org/en/docs/ # 获取更多Nginx的信息
After=network.target remote-fs.target nss-lookup.target # 指定了该服务在哪些其他服务之后启动
[Service]
Type=forking # 创建子进程运行服务
PIDFile=/usr/local/nginx/logs/nginx.pid # 指定保存nginx进程ID的文件路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf # 检查nginx配置文件的语法
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf # 启动nginx服务
ExecReload= /usr/local/nginx/sbin/nginx -s reload # 重启nginx服务
ExecStop= /usr/local/nginx/sbin/nginx -s stop # 停止nginx服务
PrivateTmp=true # 启用私有的临时目录
[Install]
WantedBy=multi-user.target # 多用户模式下启用该服务
Ⅲ给刚刚创建好的 Nginx1.22.1 运行配置文件添加权限。

bash
chmod +x /usr/lib/systemd/system/nginx.service
Ⅳ打开 Nginx1.22.1 配置文件。

bash
vim /usr/local/nginx/conf/nginx.conf
Ⅴ编辑 Nginx1.22.1 配置文件。这里修改端口为 88 端口号,并设置主机名建立连接,保存并退出。注意需要在服务器中开放 88 端口号。

0x08 Nginx重载访问
Ⅰ 重载 Nginx1.22.1 服务,并设置开机自启动。 注意:请严格按照步骤执行。

bash
systemctl daemon-reload
systemctl stop nginx.service
systemctl start nginx.service
systemctl reload nginx.service
systemctl restart nginx.service
systemctl status nginx
systemctl enable nginx.service
Ⅱ访问 Welcome to nginx! 页面。

0x09 参考文献
[1].帽子先生. nginx入门之----编译安装[EB/OL]. [2023-11-18]. https://zhuanlan.zhihu.com/p/128579141.
0x10 总结
至此Nginx编译安装完成。由于作者水平有限,文中若有错误与不足欢迎留言,便于及时更正。