Compiling Nginx from source gives you greater control over enabled modules, custom third-party extensions, build flags, and compiler optimizations. However, building from scratch and enabling optional modules frequently introduces dependency and linker errors during both the ./configure and make phases.
Whether you are starting a fresh compilation from scratch or troubleshooting broken builds, this guide covers the entire installation process for Nginx 1.31.9 from source, documents the most common module compilation errors on Ubuntu/Debian and RHEL-based distributions, and walks through setting up a complete systemd service for production.
0. Starting the Installation: Download & Core Toolchain
Before enabling optional modules, your build system must have the compiler toolchain and core libraries (PCRE for regular expressions, zlib for gzip compression, and OpenSSL for TLS/HTTPS).
Step 1: Install Core Build Prerequisites
Ubuntu / Debian
sudo apt update
sudo apt install -y build-essential curl wget tar libpcre3-dev zlib1g-dev libssl-dev
RHEL / CentOS / Rocky Linux / AlmaLinux
sudo dnf install -y gcc gcc-c++ make curl wget tar pcre-devel zlib-devel openssl-devel
Step 2: Download and Extract Nginx 1.31.9 Source
Create a dedicated source workspace in /usr/local/src and fetch the official release tarball:
sudo mkdir -p /usr/local/src
cd /usr/local/src
sudo wget https://nginx.org/download/nginx-1.31.9.tar.gz
sudo tar -zxvf nginx-1.31.9.tar.gz
cd nginx-1.31.9
Once inside the source tree, you can configure your build. If you enable optional modules without their corresponding development packages, you will run into the errors detailed below.
1. HTTP XSLT Module Requires libxml2 and libxslt
Error
./configure: error: the HTTP XSLT module requires
the libxml2/libxslt libraries.
You can either do not enable the module
or install the libraries.
Cause
This error occurs when Nginx is configured with:
--with-http_xslt_module
The XSLT module requires the development headers and runtime libraries provided by libxml2 and libxslt.
Solution
Ubuntu / Debian
sudo apt update
sudo apt install -y libxml2-dev libxslt1-dev
RHEL / CentOS / Rocky Linux / AlmaLinux
sudo dnf install -y libxml2-devel libxslt-devel
Verification
Check whether the development headers exist:
ls /usr/include/libxml2
ls /usr/include/libxslt
After installing the dependencies, rerun your original ./configure command.
Alternative: Disable XSLT
If you do not need XML/XSLT transformations, remove:
--with-http_xslt_module
from your configuration options.
2. HTTP Image Filter Module Requires the GD Library
Error
./configure: error: the HTTP image filter module
requires the GD library.
You can either do not enable the module
or install the libraries.
Cause
This error occurs when compiling Nginx with:
--with-http_image_filter_module
The image filter module depends on the GD graphics library for dynamic image resizing, cropping, and rotation. Your system is missing the GD development headers.
Solution
Ubuntu / Debian
sudo apt update
sudo apt install -y libgd-dev
RHEL / CentOS / Rocky Linux / AlmaLinux
sudo dnf install -y gd-devel
Verification
On Ubuntu/Debian:
ls -l /usr/include/gd.h
Alternative: Disable Image Filtering
If Nginx does not need to process images on the fly, remove:
--with-http_image_filter_module
Most reverse proxy and static file hosting setups do not require this module.
3. GeoIP Module Requires MaxMind DB or Legacy GeoIP Library
Error
./configure: error: the GeoIP module requires
the MaxMind DB library or the legacy GeoIP library.
You can either do not enable the module
or install the library.
Cause
The built-in Nginx GeoIP module:
--with-http_geoip_module
uses the legacy GeoIP library.
Important distinction: The legacy GeoIP module and MaxMind DB (GeoIP2) are separate implementations. Installing libmaxminddb-dev alone does not satisfy the dependency for the built-in legacy GeoIP module. MaxMind DB support is generally provided through third-party modules such as ngx_http_geoip2_module.
Solution
Ubuntu / Debian
sudo apt update
sudo apt install -y libgeoip-dev
RHEL / CentOS / Rocky Linux / AlmaLinux
sudo dnf install -y GeoIP-devel
Verification
ls -l /usr/include/GeoIP.h
Alternative: Disable Legacy GeoIP
If you do not use legacy GeoIP databases, remove:
--with-http_geoip_module
You can handle modern IP geolocation at the application or CDN level (e.g. Cloudflare headers).
4. Perl Module Fails with Cannot Find -lperl
Error
During compilation (make), you may encounter:
/usr/bin/ld: cannot find -lperl: No such file or directory
collect2: error: ld returned 1 exit status
make[1]: *** [objs/Makefile:1690:
objs/ngx_http_perl_module.so] Error 1
make: *** [Makefile:10: build] Error 2
Cause
This error occurs when Nginx is configured with:
--with-http_perl_module
or:
--with-http_perl_module=dynamic
The Perl module requires the Perl development library, particularly libperl.so, for linking. Having Perl installed on your system does not necessarily mean its development library is available.
Solution
Ubuntu / Debian
sudo apt update
sudo apt install -y libperl-dev
RHEL / CentOS / Rocky Linux / AlmaLinux
sudo dnf install -y perl-devel
Verification
Check for the shared library:
find /usr/lib* -name "libperl.so*"
If you already completed ./configure successfully, you do not need to reconfigure—simply resume compilation:
make -j"$(nproc)"
Alternative: Disable the Perl Module
If you do not execute embedded Perl scripts inside Nginx, remove:
--with-http_perl_module
from your configuration options, then clean and rebuild:
make clean
make -j"$(nproc)"
5. Install All Optional Module Dependencies at Once
If you plan to compile Nginx with all optional modules enabled simultaneously, install all required dependencies together:
Ubuntu / Debian
sudo apt update
sudo apt install -y \
build-essential \
libpcre3-dev \
zlib1g-dev \
libssl-dev \
libxml2-dev \
libxslt1-dev \
libgd-dev \
libgeoip-dev \
libperl-dev
RHEL / CentOS / Rocky Linux / AlmaLinux
sudo dnf install -y \
gcc \
gcc-c++ \
make \
pcre-devel \
zlib-devel \
openssl-devel \
libxml2-devel \
libxslt-devel \
gd-devel \
GeoIP-devel \
perl-devel
6. Configuring and Compiling Nginx
With all prerequisites in place, configure the build. Here is a production-grade configuration including standard directories, user privileges, and optional modules:
cd /usr/local/src/nginx-1.31.9
./configure \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/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 \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_perl_module
Compile using all available CPU cores:
make -j"$(nproc)"
Verify the newly compiled binary in the build directory before installing:
./objs/nginx -v
Expected output:
nginx version: nginx/1.31.9
7. Installing Nginx and Configuring Systemd Service
Now that compilation succeeds without errors, proceed to install the files and register Nginx with the system service manager.
Step 1: Create the Nginx User and Group
Ensure the unprivileged nginx service account exists:
sudo useradd --system --no-create-home --shell /bin/false nginx 2>/dev/null || true
Step 2: Run Make Install
Install the compiled binaries, modules, and default configuration files:
sudo make install
Create the log and cache directories with proper ownership:
sudo mkdir -p /var/log/nginx /var/cache/nginx
sudo chown -R nginx:nginx /var/log/nginx /var/cache/nginx
Verify configuration syntax:
sudo nginx -t
Step 3: Create the Systemd Service File
Create /etc/systemd/system/nginx.service so systemd can supervise and auto-start Nginx:
sudo tee /etc/systemd/system/nginx.service > /dev/null << 'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
Step 4: Enable and Start the Service
Reload systemd daemon configurations, enable Nginx to start on boot, and start the service:
sudo systemctl daemon-reload
sudo systemctl enable --now nginx
sudo systemctl status nginx
Step 5: Configure the Firewall
Allow HTTP and HTTPS traffic through your firewall:
Ubuntu / Debian (UFW)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
RHEL / CentOS / Rocky Linux / AlmaLinux (Firewalld)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 6: Test HTTP Response
Confirm Nginx is actively serving requests:
curl -I http://localhost
You should receive an HTTP/1.1 200 OK response displaying Server: nginx/1.31.9.
8. Should You Enable Every Nginx Module?
Not necessarily.
Enabling optional modules increases the attack surface, adds external runtime dependencies, and can complicate long-term system updates.
Here is a quick reference:
| Module | What it does | Recommendation |
|---|---|---|
| XSLT | XML transformations | Optional |
| Image Filter | Resize and manipulate images | Optional |
| GeoIP | IP geolocation using legacy GeoIP | Optional |
| Perl | Execute Perl scripts inside Nginx | Optional |
| SSL | HTTPS / TLS encryption support | Essential |
| HTTP/2 | HTTP/2 binary protocol support | Recommended |
| Real IP | Restore client IP behind proxies & CDNs | Recommended |
| Stub Status | Basic metrics and connection health checks | Recommended |
Only enable modules that your architecture actively requires.
Conclusion
Most Nginx source compilation failures stem from missing development libraries rather than compiler bugs.
By preparing the base toolchain upfront, installing optional module headers when needed, and structuring the installation with systemd supervision, building Nginx 1.31.9 from source provides complete control over your web server while remaining maintainable and production-ready.