Compiling dynamic modules into Nginx on CentOS 7

NGinx is a lightweight web server, that has grown in popularity in the latest years. It is one of the most popular web servers after Apache. It uses different concepts than Apache and can handle large quantities ot bandwidth if configured properly.

In this tutorial, we will build a dynamic module that is not included in the original source. This works with existing installations as well. Note, that we need to download NGinx's source and use the same parameters that have been used to compile it before. Otherwise we might get the error "module is not binary compatible".

Let us begin by downloading the latest stable NGinx version's source and installing the necessary build tools:

yum install gcc make -y
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar xvf nginx-1.12.0.tar.gz
cd nginx-1.12.0

In this example, we will use the Nginx's Redis module and build it as a dynamic module:

wget http://people.freebsd.org/~osa/ngx_http_redis-0.3.8.tar.gz
tar xvf ngx_http_redis-0.3.8.tar.gz

If you already have NGinx installed even from RPM or the official repository, you can check the parameteres with which NGinx was build by using the command:

nginx -V

You shoud see output like the following:

nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

Now, copy the configure parameteres and append the after the ./configure command and add the --add-dynamic-module parameter for our new module at the end, so it looks like this. If you do not have NGinx previously installed, you can still use these parameteres to compile new NGinx installation:

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-dynamic-module=/usr/local/src/nginx-1.12.0/ngx_http_redis-0.3.8

After this issue make:

make modules
make

and

make install

after this, edit your NGinx configuration file:

vi /etc/nginx/nginx.conf

and add the following line in Main context:

load_module '/usr/lib64/nginx/modules/ngx_http_redis_module.so';

Main context means that the line should be added at the top, like in this example:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

load_module '/usr/lib64/nginx/modules/ngx_http_redis_module.so';

After this, our new module is ready to be configured and used. The final step is to restart our NGinx servive:

service nginx restart

Note, that some dynamic modules are already available in the official NGinx repository. For example, to install GeoIP module as a dynamic one, you can simply do it via yum:

nginx-mod-http-geoip

Including it in the configuration then happens like usual with the load_module directive.

Comments