下载源码
从官方网站能找到一份简单的教程, 但是完全不够看
而且最重要的是编译最新版网上的绝大部分教程都有些过时了( 事实上这篇也没成功, 但是能用
# 源代码文件位置无所谓, 编译完成后可删除
# 这是 QUIC 分支, 也可以使用 stable 或者 mainline. 如果不想安装 hgsubversion 直接下载源码也是一样的效果.
hg clone https://hg.nginx.org/nginx-quic
mv nginx-quic nginx
# 或者
wget -O nginx http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx
# 解压产生的文件夹会带版本号
mv nginx<--version> nginx
git clone https://github.com/google/ngx_brotli.git
mv ngx_brotli boringssl
# ngx_brotli 依赖上游的 google/boringssl
cd brotli && git submodule update --init
cd ../nginx
没什么别的需求到这里就可以了, 如果有再去多拉几个模块下来待会一把梭
可选
用自带的软件包管理下载一份 nginx 并执行 nginx -V
看看参数, 开头一部分多半是在定义各种文件和文件夹的位置可以忽略, 重点是后面的各种模块.
编译
然后是头大的参数了, 不出意外会挨个出现不同的报错, 各种环境依赖的缺失...(主要针对 QUIC 分支
下面提供我的两份参数, 服务器部署的是 QUIC(1.23.4), 本地部署的是 Stable(1.24.0). 介于我测试死活都不支持 h3 协议, 所以也没什么区别, 推荐使用 Stable 版的, 至少这是真不支持 h3 的心里不会难受(
另外, 针对第二次编译我特意对照了一下参数, 比如 cc-opt 和 ld-opt 就比较长了. 确保不会因为缺少本来应该存在的模块导致出现奇奇怪怪的问题(大概吧
QUIC
./auto/configure \
--with-debug \
--build=kazusa.cc \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/logs/nginx.lock \
--http-client-body-temp-path=/usr/local/nginx/temp/client_temp \
--http-proxy-temp-path=/usr/local/nginx/temp/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/temp/scgi_temp \
--with-stream_quic_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_perl_module=dynamic \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-compat \
--with-file-aio \
--with-http_v2_module \
--with-http_v3_module \
--with-cc-opt='-I../boringssl/include' \
--with-ld-opt='-L../boringssl/build/ssl -L../boringssl/build/crypto' \
--user=www-data \
--group=www-data \
--add-module=/www/nginx-quic/brotli
Stable
sudo ./configure \
--with-debug \
--build=kazusa.dev \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/logs/nginx.lock \
--http-client-body-temp-path=/usr/local/nginx/temp/client_temp \
--http-proxy-temp-path=/usr/local/nginx/temp/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/temp/scgi_temp \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_perl_module \
--with-threads \
--with-http_degradation_module \
--with-pcre-jit \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-compat \
--with-file-aio \
--with-http_v2_module \
--with-cc-opt='-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -flto=auto -I../boringssl/include' \
--with-ld-opt='-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto=auto -L../boringssl/build/ssl -L../boringssl/build/crypto' \
--user=http \
--group=http \
--add-module=/www/compile/brotli \
--add-module=/www/compile/geoip2
两次编译的机器环境不同, 所以默认用户也不一样, 可以问问 GPT 自己的机器应该用什么用户和用户组来跑. 比如 CentOS 我记得是 www, 默认的是 nginx.
我这里是把所有文件路径都设在 /usr/local/nginx
下了, 可以根据自己的需求调整. 我认为这样不会产生污染, 删除的时候也很方便.
其中最后一行的模块路径需要根据自己的情况改. 这里使用 geoip2 来代替原有的 geoip. 我也不知道本地环境要这个干嘛, 反正先装上.
发行版不同需要安装的包名可能也不同, 这里给一个参考. 如果上面这行代码不报错了, 就可以开始下面的编译安装了!
# 刚才的路径下
make -j 4 && make install
mkdir /usr/local/nginx/temp
rm /usr/local/nginx/conf/*.default
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
nginx -t
nginx -V
示例输出
这里碰到了就顺便说一下, 一开始我上传图片失败了. 后面排查是权限问题, 因为我最开始的目录权限是 root, www-data 没有权限写文件, 如果习惯在根目录下创建网站根目录可能会碰到这样的情况. 进到对应网站的根目录下执行 sudo chown -R www-data:www-data ./
就行了
如果正常输出了刚才的编译参数到这里就安装完成了, 下面来完善一下!
服务添加到开机自启.
# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
添加环境变量
# 图省事, 注意是两个 '>', 一个可能直接火葬场
echo 'NGINX_HOME=/usr/local/nginx\nPATH=$PATH:$NGINX_HOME/sbin' >> /etc/environment
# 推荐, 手动写入下面两行
# vim /etc/environment
NGINX_HOME=/usr/local/nginx
PATH=$PATH:$NGINX_HOME/sbin
完事了 source /etc/environment
更新一下.
如果不懂除了路径以外不要改动其他参数.
保存配置后执行 systemctl daemon-reload && systemctl enable --now nginx.service
设置开机自启顺便现在启动.
不出意外应该会输出一行 Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
这也是真正有效的自启配置地址, 但是是通过打链接过去的所以两个文件是同一份.
此时应该已经可以访问 http://127.0.0.1
了. 接下来自己配 SSL 吧!
最后说一句, 用重启(systemctl restart nginx.service
)来代替重载(nginx -s reload
)可以节约很多调试的时间(
0 条评论