When installing nginx using standard OS tools on Linux (apt, aptitude, yum, dnf), it is not possible to configure its installation to add or remove any modules and nginx is installed “as is”.
Let’s assume, for example, that we need to add the http_mp4_module module to nginx. The output of the nginx -V command will show us that nginx was built without it.
nginx -V
Result:
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-lUTckl/nginx-1.18.0=.
-fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time
-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC'
--prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log
--error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid
--modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy
--http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug
--with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module
--with-http_realip_module --with-http_auth_request_module --with-http_v2_module
--with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module
--with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic
--with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic
--with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
Let’s save the nginx -V command output to some text editor – this information will be useful to us during configuration. We see that the nginx version is installed 1.18.0 — we download the same version.:
wget http://nginx.org/download/nginx-1.18.0.tar.gz
Unpack the archive and go to the nginx-1.18.0 folder.:
tar -xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
Next, we will need to install additional packages into the system to build it.
For Debian/Ubuntu, we perform:
apt install build-essential
After the suggestion to continue the installation, Do you want to continue? [Y/n] — press Y.
For CentOS/AlmaLinux, we perform:
yum install gcc gcc-c++ kernel-devel
yum groupinstall 'Development Tools'
After the suggestion to continue the installation — Is this ok [y/d/N] — press y.
After installing the packages, we proceed to configure nginx with the addition of the http_mp4_module module.
To do this, copy the output of the nginx -V command from the text editor, starting with –prefix= and up to the first –add-module= (we do not need all the –add_module= present in the output). After that, we write in the console ./configure and paste the copied image from the editor. In our case, there is a conclusion:
--add-dynamic-module=/build/nginx-d8gVax/nginx-1.18.0/debian/modules/http-geoip2
therefore, we simply copy everything except this line.
Add –with-http_mp4_module to the end of the line to make it look like this:
./configure --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf
--http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
--lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit
--with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
--with-http_auth_request_module --with-http_v2_module --with-http_dav_module
--with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module
--with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module
--with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module
--with-mail=dynamic --with-mail_ssl_module --with-http_mp4_module
Press Enter and wait for the process to end.
Errors may occur during the configuration process. The ways to eliminate them are described below.
Mistake:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
For Debian/Ubuntu, it is fixed by installing libpcre++-dev:
apt install libpcre++-dev
For CentOS/AlmaLinux, it is fixed by installing pcre-devel:
yum install pcre-devel
Error:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
For Debian/Ubuntu:
apt install libssl-dev
For CentOS/AlmaLinux:
yum install openssl-devel
Error:
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.
For Debian/Ubuntu:
apt install libgeoip-dev
For CentOS/AlmaLinux:
yum install GeoIP-devel
Error:
./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.
For Debian/Ubuntu:
apt install libxslt1-dev
For CentOS/AlmaLinux:
yum install libxslt-devel
Error:
./configure: error: the HTTP gzip module requires the zlib library
You can either do not enable the module or install the libraries.
For Debian/Ubuntu:
apt install zlib1g-dev
For CentOS/AlmaLinux:
yum install zlib-devel
Error:
./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.
For Debian/Ubuntu:
apt install libgd-dev
For CentOS/AlmaLinux:
yum install gd gd-devel
Each time after apt install or yum install, we restart the missing package.
./configure --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf
--http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
--lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit
--with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
--with-http_auth_request_module --with-http_v2_module --with-http_dav_module
--with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module
--with-http_gzip_static_module --with-http_sub_module --with-http_mp4_module
After successful completion of the configuration, we will see on the screen something like:
Configuration summary
+ using threads
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/share/nginx"
nginx binary file: "/usr/share/nginx/sbin/nginx"
nginx modules path: "/usr/lib/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/lib/nginx/body"
nginx http proxy temporary files: "/var/lib/nginx/proxy"
nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi"
nginx http uwsgi temporary files: "/var/lib/nginx/uwsgi"
nginx http scgi temporary files: "/var/lib/nginx/scgi"
Now you can build the nginx binary — run 2 commands:
make
make install
At the end of the build, we check that nginx is assembled with the module we need.:
/usr/share/nginx/sbin/nginx -V
Result:
nginx version: nginx/1.18.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf
--http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
--lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit
--with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
--with-http_auth_request_module --with-http_v2_module --with-http_dav_module
--with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module
--with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module
--with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module
--with-mail=dynamic --with-mail_ssl_module --with-http_mp4_module
As you can see, –with-http_mp4_module is present in the command output — everything worked out. It remains to replace the current nginx binary with the new one that we have just compiled.
Stopping nginx:
systemctl stop nginx
Rename (just in case) the current nginx to nginx_back:
mv /usr/sbin/nginx /usr/sbin/nginx_back
Moving the newly assembled binary to its place:
mv /usr/share/nginx/sbin/nginx /usr/sbin/nginx
Deleting the unnecessary folder /etc/nginx/sbin:
rm -Rf /usr/share/nginx/sbin
We check again that nginx is now the right one for us.:
nginx -V
Result:
nginx version: nginx/1.18.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf
--http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
--lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit
--with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
--with-http_auth_request_module --with-http_v2_module --with-http_dav_module
--with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module
--with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module
--with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module
--with-mail=dynamic --with-mail_ssl_module --with-http_mp4_module
Launching nginx:
systemctl start nginx
Deleting the unnecessary folder and the nginx-1.8.1 archive:
cd ../
rm -Rf nginx-1.18.0/
rm -Rf nginx-1.18.0.tar.gz
