When installing nginx on Linux, it is not possible to configure the installation to add or remove modules and nginx is installed "as is".

If any module needs to be added, nginx needs to be rebuilt manually.

# nginx -V
nginx version: nginx/1.12.1  
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)  
built with OpenSSL 1.0.1e-fips on Feb 11, 2013  
TLS SNI support enabled  
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log  
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock
--pid-path=/var/run/nginx.pid --with-pcre-jit --with-http_gzip_static_module --with-http_ssl_module --with-ipv6
--without-http_browser_module -without-http_geo_module -without-http_limit_req_module -without-http_limit_zone_module
--without-http_memcached_module -without-http_referer_module -without-http_scgi_module -without-http_split_clients_module
--with-http_stub_status_module -without-http_ssi_module -without-http_userid_module -without-http_uwsgi_module
--add-module=/tmp/buildd/nginx-1.12.0/debian/modules/nginx-echo

Let's save the output of the nginx -V command to a text editor. This information will be useful for configuring the system
We see that we have nginx version 1.12.1 installed - download the same version:

# wget http://nginx.org/download/nginx-1.12.1.tar.gz

Unpack the archive and go to the folder nginx-1.12.1:

# tar -xvf nginx-1.12.1.tar.gz
# cd nginx-1.12.1

Next, you will need to install a preinstalled set of modules.

# bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
     --nginx-version $nginxversion

Save all modules:

bash <(curl -f -L -sS https://ngxpagespeed.com/install) -m  

After installing the packages, proceed to configure nginx with the addition of modules.

To do this copy from a text editor the output of the command nginx -V starting from --prefix= to the first --add-module= (all the --add_module= present in the output are not needed)
After that we write in the console ./configure and paste copied from the editor:

./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock
--pid-path=/var/run/nginx.pid --with-pcre-jit --with-http_gzip_static_module --with-http_ssl_module --with-ipv6
--without-http_browser_module -without-http_geoip_module -without-http_memcached_module -without-http_referer_module
--without-http_scgi_module -without-http_split_clients_module -without-http_stub_status_module -without-http_ssi_module
--without-http_userid_module -without-http_uwsgi_module 

At the end of the line add --add-dynamic-module=/root/incubator-pagespeed-ngx-latest-stable to make it like this:

./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock
--pid-path=/var/run/nginx.pid --with-pcre-jit --with-http_gzip_static_module --with-http_ssl_module --with-ipv6
--without-http_browser_module -without-http_geoip_module -without-http_memcached_module -without-http_referer_module
--without-http_scgi_module -without-http_split_clients_module -without-http_stub_status_module -without-http_ssi_module
--without-http_userid_module -without-http_uwsgi_module 
--add-dynamic-module=/root/incubator-pagespeed-ngx-latest-stable

Press Enter and wait for the process to finish.

Now you can build the nginx binary - run 2 commands:

# make
# make install

After completing the build, check that nginx is built with the module we want:

# /etc/nginx/sbin/nginx -V

Result:

nginx version: nginx/1.12.1  
TLS SNI support enabled  
configure arguments: ./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log  
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock
--pid-path=/var/run/nginx.pid --with-pcre-jit --with-http_gzip_static_module --with-http_ssl_module --with-ipv6
--without-http_browser_module -without-http_geoip_module -without-http_memcached_module -without-http_referer_module
--without-http_scgi_module -without-http_split_clients_module -without-http_stub_status_module -without-http_ssi_module
--without-http_userid_module -without-http_uwsgi_module 
--add-dynamic-module=/root/incubator-pagespeed-ngx-latest-stable

As you can see, --add-dynamic-module=/root/incubator-pagespeed-ngx-latest-stable is present in the output of the command. It remains to replace the current nginx binary with the new one we just built.

Let's stop nginx:

# service nginx stop

Rename (just in case) the current nginx to nginx_back:

# mv /usr/sbin/nginx /usr/sbin/nginx_back

Move the newly built binary in its place:

# mv /etc/nginx/sbin/nginx /usr/sbin/nginx

Delete the unnecessary /etc/nginx/sbin folder:

# rm -r -f /etc/nginx/sbin

Check again that we have the right nginx now:

# nginx -V

Result:

nginx version: nginx/1.12.1  
TLS SNI support enabled  
configure arguments: ./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log  
--http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --lock-path=/var/lock/nginx.lock
--pid-path=/var/run/nginx.pid --with-pcre-jit --with-http_gzip_static_module --with-http_ssl_module --with-ipv6
--without-http_browser_module -without-http_geoip_module -without-http_memcached_module -without-http_referer_module
--without-http_scgi_module -without-http_split_clients_module -without-http_stub_status_module -without-http_ssi_module
--without-http_userid_module -without-http_uwsgi_module 
--add-dynamic-module=/root/incubator-pagespeed-ngx-latest-stable

Run nginx:

# service nginx start

Delete unnecessary nginx-1.12.1 folder:

# cd ../
# rm -r -f nginx-1.12.1
Updated April 17, 2020