保姆级 | Nginx编译安装

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编译安装完成。由于作者水平有限,文中若有错误与不足欢迎留言,便于及时更正。

相关推荐
活跃的煤矿打工人14 分钟前
【星海saul随笔】Ubuntu基础知识
linux·运维·ubuntu
北京智和信通1 小时前
云平台和虚拟化智慧运维监控,全面提升故障感知与处置能力
运维·虚拟化·云平台·虚拟机监控
fasewer1 小时前
第五章 linux实战-挖矿 二
linux·运维·服务器
楚灵魈1 小时前
[Linux]从零开始的网站搭建教程
linux·运维·服务器
小小不董1 小时前
《Linux从小白到高手》理论篇:深入理解Linux的网络管理
linux·运维·服务器·数据库·php·dba
DY009J2 小时前
深度探索Kali Linux的精髓与实践应用
linux·运维·服务器
什么鬼昵称3 小时前
Pikachu- Over Permission-垂直越权
运维·服务器
码农小白3 小时前
linux驱动:(22)中断节点和中断函数
linux·运维·服务器
4647的码农历程3 小时前
Linux网络编程 -- 网络基础
linux·运维·网络
醉颜凉4 小时前
银河麒麟桌面操作系统V10 SP1:取消安装应用的安全授权认证
运维·安全·操作系统·国产化·麒麟·kylin os·安全授权认证