Debian系统重新打包nginx格式deb软件包

因为某些原因,我需要使用Nginx的某个模块,但是要用的那个模块正好Nginx不自带,于是我们需要自己加上对应的某个模块之后再次编译Nginx。
但是我又不想到正式的服务器上去安装编译所需要的那一大堆依赖,于是就想着能否借助Nginx官方的代码、相关编译程序再次自行二次打包,折腾了一下,还真被我打包成功了,这里记录一下。

我这次是在Debian9上操作的,但是讲道理,从我之前的打包过程来说,Debian8也是一样能用的,整个打包步骤,和系统几乎没什么关系,所以系统是Debian9还是Debian8都无所谓了,当然,我没在Debian7上试过。

添加Nginx官方源

首先,需要添加Nginx官方官方源仓库,如果没有添加的,可以参考这篇文章先行添加再操作:Linux安装官方最新版的Nginx

安装编译环境所需要的依赖

要编译肯定需要有编译环境支持了,一行命令安装编译环境:

apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev git

下载Nginx源代码及相关打包需要的文件

执行下面几步:

cd ~
mkdir -p ~/new/nginx_rebulid/ #创建新目录  用于保存我们所要使用的文件
cd ~/new/nginx_rebulid/
apt-get source nginx #下载Nginx源代码
apt-get build-dep nginx  #安装编译Nginx所需要的依赖

准备我们所需要的模块

重新打包编译之前,我们需要先行准备我们所需要的模块,比如说Nginx Module for Google,嗯,你懂的。
以下是我的操作步骤:

cd /usr/local/src
git clone https://github.com/cuber/ngx_http_google_filter_module
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

除了这个模块,你也能使用别的模块,只需要确定好模块所存放的目录即可,下面就会用到。

修改打包规则

要重新打包,我们肯定需要修改官方原有的打包规则了,以下是操作步骤:

cd ~/new/nginx_rebulid/nginx-{nginx版本号}/debin/
vim rules

建议备份上述文件,然后修改如下这部分:

config.status.nginx: config.env.nginx
        cd $(BUILDDIR_nginx) && \
        CFLAGS="" ./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=www --group=www --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="$(CFLAGS)" --with-ld-opt="$(LDFLAGS)" --add-module=/usr/local/src/ngx_http_google_filter_module --add-module=/usr/local/src/ngx_http_substitutions_filter_module --add-module=/usr/local/src/ngx_cache_purge
        touch $@
config.status.nginx_debug: config.env.nginx_debug
        cd $(BUILDDIR_nginx_debug) && \
        CFLAGS="" ./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=www --group=www --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="$(CFLAGS)" --with-ld-opt="$(LDFLAGS)" --with-debug --add-module=/usr/local/src/ngx_http_google_filter_module --add-module=/usr/local/src/ngx_http_substitutions_filter_module --add-module=/usr/local/src/ngx_cache_purge

参照原有的写法,在后面加上我们所需要编译进去的模块,然后保存退出。

重新打包编译Nginx

修改之后我们就可以重新打包了,以下是命令:

cd ~/new/nginx_rebulid/nginx-{nginx版本号}/
dpkg-buildpackage -b

一串串字符串滚过之后,如果没有报错,我们的Nginx应该是编译好了。
编译好的Nginx安装包在~/new/nginx_rebuild,名字以nginx开始,以.deb结尾。
然后在本机或者拷贝到别的机器,用如下命令就可以安装了:

dpkg -i nginx_1.12.2-1~stretch_amd64.deb

安装之后就能看到我们所需要的模块,查看命令如下:

root@BWH123:~/new/nginx_source# nginx -V
nginx version: nginx/1.12.2
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0f  25 May 2017
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=/root/new/nginx_source/nginx-1.12.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=/usr/local/src/ngx_http_google_filter_module --add-module=/usr/local/src/ngx_http_substitutions_filter_module --add-module=/usr/local/src/ngx_cache_purge

后面已经有我们所需要的模块了,说明二次打包已经成功了。
参考文章:https://linux.cn/article-5707-1.html