第1章 源编译码安装
一、安装依赖
- 安装GCC编译器,用于编译C源码。
bash
$ sudo apt install gcc make
bash
$ sudo yum install gcc make
- 安装PCRE2,支持正则表达式,用于Nginx Core和Rewrite模块。
bash
wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.gz
tar -xf pcre2-10.42.tar.gz
cd pcre2-10.42
sudo make && sudo make install
1
2
3
4
2
3
4
- 安装zlib,支持数据压缩,用于Nginx Gzip模块。
bash
wget https://zlib.net/zlib-1.3.1.tar.gz
tar -xf zlib-1.3.1.tar.gz
cd zlib-1.3.1
./configure
sudo make && sudo make install
1
2
3
4
5
2
3
4
5
- 安装OpenSSL,支持HTTPS协议,用于Nginx SSL模块以及其他。
bash
wget https://www.openssl.org/source/openssl-3.0.14.tar.gz
tar -xf openssl-3.0.14.tar.gz
cd openssl-3.0.14
# 查询支持的Linux内核
./Configure LIST | grep -i linux
./Configure linux-aarch64 --prefix=/usr
sudo make && sudo make install
1
2
3
4
5
6
7
2
3
4
5
6
7
二、下载源码
从Nginx官网下载源码:https://nginx.org/download/
bash
wget https://nginx.org/download/nginx-1.27.0.tar.gz
tar -xf nginx-1.27.0.tar.gz
1
2
2
三、配置构建
配置构建参数以及需要支持的Nginx模块。
bash
cd nginx-1.27.0
./configure --with-http_ssl_module --with-stream
1
2
2
参数 | 默认值 | 描述 |
---|---|---|
--prefix=<PATH> | /usr/local/nginx | Nginx安装路径,以及其他配置的相对路径 |
--with-http_ssl_module | 启用HTTPS支持,要求SSL库,例如:OpenSSL | |
--with-stream | 启用TCP和UDP代码功能 |
四、完成安装
bash
sudo make && sudo make install
1
Nginx常用命令:
bash
# 查看版本号
nginx -v
# 查看版本号以及编译配置参数
nginx -V
# 测试配置文件
nginx -t
# 启动、停止、重载配置命令
nginx -x [signal]
signal
有如下类型:
stop
:快速停止quit
:优雅停止reload
:重载配置文件reopen
:重新生成日志文件
Nginx Service配置文件:
ini
[Unit]
Description=Nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target