如何在本地100%还原Nginx官方Deb包,包含源码编译和打包成DEB包(Ubuntu 20.04)
可解决问题
-
通过修改 Nginx 源码隐藏标识和版本号,例如:cloud.tencent.com/developer/a…
-
需要编译 Nginx 的第三方插件,例如:github.com/openresty/h…
-
其他需要通过修改源码重新编译并打包成 Deb 包进行分发的情况。
前置环境
Ubuntu版本 20.04.6 LTS
已安装过 nginx 1.18.0
下载Nginx官方Deb包
官方配置文档地址:nginx.org/en/linux_pa…
前四步按照官方文档的步骤一步步执行下去,需要注意的是第5、6步,5和6是选择稳定版本或主线版本,二者选其一就行
编译Nginx源码
准备源码和环境
-
下载Nginx源码,官方下载地址:nginx.org/download/ng…
-
下载pcre源码,官方下载地址:github.com/PCRE2Projec…
-
下载zlib源码,官方下载地址:zlib.net/current/zli…
-
下载openssl源码,官方下载地址:www.openssl.org/source/open…
-
安装编译环境
sudo apt-get install make build-essential
组织文件目录
将上一步下载下来的源码包进行解压、并重命名,与nginx源码放置在同一目录下
.
├── nginx-1.24.0
├── nginx-1.24.0.tar.gz
├── openssl
├── openssl-1.1.1w.tar.gz
├── pcre
├── pcre2-10.42.tar.gz
├── zlib
└── zlib.tar.gz
查看Nginx官方的编译参数
nginx -V
nginx version: nginx/1.24.0
built by gcc 9.3.0 (Ubuntu 9.3.0-10ubuntu2)
built with OpenSSL 1.1.1f 31 Mar 2020
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 --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-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.24.0/debian/debuild-base/nginx-1.24.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
返回参数中 "configure arguments" 后面的就是Nginx的编译参数,复制编译参数备用(这里吐槽一下掘金的markdown语法适配情况,引用标记的换行有问题,被迫换成了代码块)
开始编译
切换当前目录为 nginx 源码所在目录,例如: ~/Downloads/nginx-1.20.0
./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 --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-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.24.0/debian/debuild-base/nginx-1.24.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --with-pcre=../pcre --with-openssl=../openssl --with-zlib=../zlib
make
执行完上述命令后 nginx-1.24.0 目录下会多出一个 objs 目录,其中的 nginx 就是编译的可执行nginx 文件
ubuntu@ubuntu:~/Downloads/nginx-1.24.0/objs$ ls
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
打包Nginx的Deb包
准备deb包文件
-
apt安装时的缓存文件目录在 /var/cache/apt/archives 中,使用 mv 命令将 nginx_1.24.0-1~focal_amd64.deb 文件移动到别的目录下
-
如果不存在的话使用 sudo apt-get install -d nginx 命令重新获取,-d 参数的作用是只下载不安装
提取 Nginx 的 deb 包信息
-
查看 nginx 的 deb 中文件组织方式
dpkg -c nginx_1.24.0-1~focal_amd64.debdrwxr-xr-x root/root 0 2024-01-17 16:47 ./ drwxr-xr-x root/root 0 2023-04-12 01:03 ./usr/lib/nginx/modules/ drwxr-xr-x root/root 0 2023-04-12 01:03 ./usr/sbin/ -rwxr-xr-x root/root 13912784 2024-01-17 18:29 ./usr/sbin/nginx -rwxr-xr-x root/root 1534840 2023-04-12 01:03 ./usr/sbin/nginx-debug lrwxrwxrwx root/root 0 2023-04-12 01:03 ./etc/nginx/modules -> /usr/lib/nginx/modules ......./usr/sbin/nginx 是官方的 nginx 可执行文件
./usr/lib/nginx/modules 目录下用来存放自己编译的第三方插件(非必须)
-
解压 deb 包
dpkg -X nginx_1.24.0-1~focal_amd64.deb nginx -
解压 deb 包中的 control 信息(重新打包时需要使用到)
dpkg -e nginx_1.24.0-1~focal_amd64.deb nginx/DEBIAN
替换 Nginx 的打包文件
-
替换 nginx 可执行文件,将上面自己编译好的 nginx 可执行文件拷贝至 解压后的 Nginx 包目录 nginx/usr/sbin/nginx 中
-
如果需要第三方插件,并且已编译出来,将 objs 中编译好的 .so 文件拷贝至 解压后的 Nginx 包目录 nginx/usr/lib/nginx/modules 中
打包 Nginx 的 deb包
-
新建 nginx_build 目录
mkdir nginx_build -
打包自己编译的 nginx
dpkg -b nginx nginx_build/ -
查看打包好的 nginx 包
cd nginx_build tree. └── nginx_1.24.0-1~focal_amd64.deb 0 directories, 1 file
到此就已经完成了 100% 还原官方打包的 Nginx 的 Deb 包了,这样可以直接将这个重新编译的包分发并进行安装了