保姆级 | 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 环境依赖项。 

        

更新镜像源。

apt-get update

        

安装 GCC 库。

apt-get install gcc

        

安装 make 库。

apt-get install make

        

安装 PCRE 库。

apt-get install libpcre3 libpcre3-dev

        

安装 OpenSSL 库。

apt-get install openssl

        

安装 zlib 库。

apt-get install zlib1g zlib1g-dev

        

安装 GD 库。

apt-get install libgd-dev libgdal-dev

        

        

0x03 Nginx下载

        需要注意的是,使用编译安装时间会比较久。但是编译安装更适合生产环境,所以我个人更倾向于使用编译安装。

        

在开始之前我们需要先创建 nginx 用户,之所以这么做主要是为了降低服务器风险,让 nginx 以最小的权限运行。

useradd -s /sbin/nologin nginx

        

访问 nginx 官网。

https://nginx.org/en/download.html

        

下载 Nginx1.22.1 。

wget https://nginx.org/download/nginx-1.22.1.tar.gz

        

解压 Nginx1.22.1 。

tar -zxvf nginx-1.22.1.tar.gz

        

        

0x04 运行配置脚本

打开解压缩后的 nginx-1.22.1 文件。

cd nginx-1.22.1

        

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

./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 。

make && make install

        

        

0x06 Nginx验证启动

访问 Nginx1.22.1 的启动文件夹。

cd /usr/local/nginx/sbin

        

启动 Nginx1.22.1 。

./nginx

        

验证 Nginx1.22.1 文件。

./nginx -t
./nginx -v

        

查看 Nginx1.22.1 启动状态。

netstat -ntlp | grep nginx

        

        

0x07 Nginx服务配置

创建 Nginx1.22.1 配置文件。

vim /usr/lib/systemd/system/nginx.service

        

编辑 Nginx1.22.1 运行配置文件,保存并退出。 注意:在写入配置文件时,最好将中文注释去除。 

[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 运行配置文件添加权限。

chmod +x /usr/lib/systemd/system/nginx.service

        

打开 Nginx1.22.1 配置文件。

vim /usr/local/nginx/conf/nginx.conf

        

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

        

        

0x08 Nginx重载访问

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

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

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

尼泊罗河伯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值