Nginx 1.27 编译安装并使用 Quic 和 headers_more 模块

前言

最近新整了一台服务器，由于只有 1c1g 所以不打算装面板
然后就想到了土法编译安装 Nginx，顺便还能水个文章

前置

如果是镜像及其精简的服务器，那么大概率缺东西
浅装一下

Copy
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev git

安装

下载 Nginx 源码

Copy
wget https://nginx.org/download/nginx-1.27.3.tar.gz
tar -zxvf nginx-1.27.3.tar.gz
cd nginx-1.27.3

下载需要的模块和 OpenSSL

Copy
git clone https://github.com/openresty/headers-more-nginx-module
git clone https://github.com/quictls/openssl
mv openssl-openssl-3.0.13-quic openssl

配置编译选项

Copy
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-openssl=./openssl \
--with-openssl-opt="enable-tls1_3" \
--with-cc-opt="-I/usr/local/include" \
--with-ld-opt="-L/usr/local/lib" \
--add-module=./headers-more-nginx-module

开跑！

Copy
make -j$(nproc)
make install

稍作等待，不出意外 Nginx 就已经安装完成了
验证一下

Copy
root@Miao:~# nginx -V
nginx version: nginx/1.27.3
built by gcc 12.2.0 (Debian 12.2.0-14)
built with OpenSSL 3.1.7+quic 3 Sep 2024
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-openssl=./openssl --with-openssl-opt=enable-tls1_3 --with-cc-opt=-I/usr/local/include --with-ld-opt=-L/usr/local/lib --add-module=./headers-more-nginx-module
root@Miao:~#

配置 systemd

装完发现默认是没有 systemd service 配置的
既然这样那就自己写一个

Copy
[Unit]
Description=Nginx - A high performance web server and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动

Copy
systemctl enable nginx
systemctl start nginx

这样就完成了
看看状态

Copy
root@Miao:~# systemctl status nginx
● nginx.service - Nginx - A high performance web server and reverse proxy server
Loaded: loaded (/etc/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Sun 2024-12-22 16:20:27 UTC; 12s ago
Docs: https://nginx.org/en/docs/
Main PID: 36430 (nginx)
Tasks: 2 (limit: 1063)
Memory: 1.9M
CPU: 19ms
CGroup: /system.slice/nginx.service
├─36430 "nginx: master process /usr/sbin/nginx"
└─36431 "nginx: worker process"

12月 22 16:20:27 Miao systemd[1]: Starting nginx.service - Nginx - A high performance web server and rever>
12月 22 16:20:27 Miao nginx[36428]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
12月 22 16:20:27 Miao nginx[36428]: nginx: configuration file /etc/nginx/nginx.conf test is successful
12月 22 16:20:27 Miao systemd[1]: Started nginx.service - Nginx - A high performance web server and revers>
lines 1-18/18 (END)

这样就算是大功告成了
如果文章对你有用，还请点个赞，谢谢喵

此文由 Mix Space 同步更新至 xLog
原始链接为 https://blog.xcnya.cn/posts/maintain/127.html