跳到主要内容

Debian/Ubuntu 构建 Nginx 添加 QUIC 支持

安装编译依赖环境,本文用 Debian12.x 和 Ubuntu20.04 做测试验证。

sudo apt install build-essential ca-certificates zlib1g-dev libpcre3 libpcre3-dev tar unzip libssl-dev mercurial libunwind-dev pkg-config make cmake golang gcc git wget

说明:Debian 如果采用 root 用户登录则不需要增加 sudo 来安装,Ubuntu 同理。


编译 HTTP/3/QUIC 需要的依赖(可选)

克隆依赖库(使用 Gitee 镜像)

git clone https://gitee.com/fenghuolingyun/boringssl.git
  • 原始仓库地址:git clone https://boringssl.googlesource.com/boringssl

开始编译依赖:

cd boringssl
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make

注意:这里有可能由于网络原因导致 golang 模块无法拉取而报错。我们添加代理支持,使用阿里云代理:

export GO111MODULE=on
export GOPROXY=https://mirrors.aliyun.com/goproxy/

然后执行 make,完成后返回上级目录:

cd ../../

下载 Nginx 源码(版本可替换)

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

开始编译操作:

tar -xf nginx-1.25.4.tar.gz
cd nginx-1.25.4

配置构建参数(全模块配置):

./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/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 \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_v3_module \
--with-cc=c++ \
--with-cc-opt='-I../boringssl/include -x c' \
--with-ld-opt='-L../boringssl/build'

执行编译安装:

make && make install

创建缺失的目录:

mkdir -p /var/cache/nginx/
mkdir -p /var/log/nginx/
mkdir -p /usr/lib/nginx/modules # 此目录非必须,方便后续添加动态加载类型模块

添加 nginx 用户组:

useradd -m nginx

编写 systemd 服务文件(用于 systemctl 管理):

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

将以下内容粘贴进去:

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

重载 systemd 并管理服务:

systemctl daemon-reload

现在你可以使用 systemctl 管理我们编译的 Nginx:

配置开机启动

systemctl enable nginx

执行启动

systemctl start nginx

停止 Nginx 服务

systemctl stop nginx

重启 Nginx 服务

systemctl restart nginx

显示 Nginx 服务的状态

systemctl status nginx

禁用 Nginx 服务,使其不会在系统启动时自动启动

systemctl disable nginx