Recompile Nginx packaged by Apt on Ubuntu

byGinkSat, 22 Apr 2023

Get the source of Nginx

Before doing this step, you should check your Apt's sources.list to make sure both the binary and source package lists are enabled. The source package (deb-src) is commonly disabled by default so need to manually update it like this:

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu jammy main restricted
deb-src http://archive.ubuntu.com/ubuntu jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu jammy-updates main restricted
deb-src http://archive.ubuntu.com/ubuntu jammy-updates main restricted
  • Prepare dependencies and get the source from Apt source list:
cd /opt
sudo apt-get update
sudo apt-get build-dep nginx
apt-get source nginx

Get the modules

  • In this example, we'll install ngx_headers_more module, a great module giving us the ability to add or modify new http headers directly from Nginx. Let's check its repo here.
  • Put the module source code inisde nginx-1.xx.x/debian/modules/
cd nginx-1.18.0/debian/modules/
curl -L https://github.com/openresty/headers-more-nginx-module/archive/refs/heads/master.zip -o headers-more-nginx-module-master.zip
unzip headers-more-nginx-module-master.zip

Modify nginx configuration to add the module

  • The list of modules can be configured via nginx-1.xx.x/debian/rules file. Open that file and you can see the flags for each configuration like nginx-common, nginx-full, nginx-extra...
  • Let's append the module to common_configure_flags as it will be applied for all package results.
...
--with-http_slice_module \
--with-threads \
--add-dynamic-module=$(MODULESDIR)/headers-more-nginx-module-master

Rebuild the binary packages

After updating the rules, move back the root source directory and rebuild it again.

cd /opt/nginx-1.18.0
dpkg-buildpackage -uc -b

It will take awhile to finish and all new *.deb packages will be located in the parent directory.

cd /opt
ls -la
libnginx-mod-http-auth-pam_1.18.0-6ubuntu14.3_amd64.deb
libnginx-mod-http-cache-purge_1.18.0-6ubuntu14.3_amd64.deb
...
libnginx-mod-stream_1.18.0-6ubuntu14.3_amd64.deb
nginx-common_1.18.0-6ubuntu14.3_all.deb
nginx-core_1.18.0-6ubuntu14.3_amd64.deb
nginx-doc_1.18.0-6ubuntu14.3_all.deb
nginx-extras_1.18.0-6ubuntu14.3_amd64.deb
nginx-full_1.18.0-6ubuntu14.3_amd64.deb
nginx-light_1.18.0-6ubuntu14.3_amd64.deb
nginx_1.18.0-6ubuntu14.3_amd64.deb

Remove existing nginx and install the new modified packages

You may remove all of existing configuration by apt purge also. In this case, I just remove the binary and keep the configuration:

sudo apt remove nginx

Remember to install all needed libs first:

sudo dpkg -i nginx-common_1.18.0-6ubuntu14.3_all.deb
sudo dpkg -i lib*.deb
sudo dpkg -i nginx-core_1.18.0-6ubuntu14.3_amd64.deb

Then choose the desired nginx package based on your need. Nginx-full for example:

sudo dpkg -i nginx-full_1.18.0-6ubuntu14.3_amd64.deb

Almost done now. And to avoid apt may update newer nginx package in the future, you can pin it to this version by:

sudo apt-mark hold nginx-common nginx-core nginx-full

Great. Now we have a new Nginx runing with new modules and working exactly the same way of prebuilt Nginx from Apt.


© 2016-2024  GinkCode.com