How to Build NGINX from Source on Debian 12, 11 or 10

NGINX offers a robust solution for those using Debian-based systems and looking to optimize their web server setup. Recognized for its stability, efficiency, and ability to manage dynamic content, NGINX is a top choice for many. However, to truly harness its capabilities, building NGINX from source can be a game-changer. This guide will show you how to build NGINX from source on Debian 13 Trixie, Debian 12 Bookworm, Debian 11 Bullseye, or Debian 10 Buster, ensuring you get a tailored and high-performing web server.

Here’s why building NGINX from source is beneficial:

  • Flexibility: Customize NGINX to fit your specific requirements, allowing for a setup that aligns perfectly with your needs.
  • Control: Have complete oversight over your NGINX setup, ensuring it remains updated and secure.
  • Optimized Performance: By building from source, you can tweak NGINX to perform at its best on your specific hardware and environment.
  • Compatibility: Easily add or remove modules, ensuring NGINX integrates smoothly with other tools and software you use.
  • Enhanced Security: Implement security patches and updates as soon as they’re available, safeguarding your server and website.

Now, let’s proceed to the installation of Nginx via the source code.

Step 1: Update and Upgrade Debian

Before starting the installation, update your Debian system. Use these commands in the terminal:

sudo apt update
sudo apt upgrade

These commands will fetch the list of available updates and then upgrade your system, ensuring you’re working with the latest software.

Step 2: Install Required Dependencies

Install the required initial packages to compile Nginx with the following command:

sudo apt install build-essential libpcre3-dev libssl-dev zlib1g-dev libgd-dev

Step 3: Download NGINX Source Code

With the necessary dependencies installed, the next step is downloading the NGINX source code. Visit the NGINX website and choose the version that best suits your needs. You can opt for the latest mainline, stable version, or any other version.

Use the wget command to download your chosen version:

wget http://nginx.org/download/nginx-x.x.x.tar.gz

Replace x.x.x with the version number. For instance, to download the latest mainline version, 1.25.1, use the following:

wget https://nginx.org/download/nginx-1.25.1.tar.gz

Step 4: Extract the Source Code

The source code comes in a compressed tarball. Extract it using this command:

tar -xzvf nginx-1.25.1.tar.gz

Then, navigate to the newly extracted directory:

cd nginx-1.25.1

Step 5: Configure NGINX Options

For this step, set up the NGINX options from the source. This means choosing the paths and modules for your NGINX build. Use this command:

./configure --prefix=/var/www/html --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 --with-pcre  --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-http_v3_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module

Here’s what the options mean:

  • --prefix=/var/www/html: Sets the root directory for the install.
  • --sbin-path=/usr/sbin/nginx: Sets where the nginx program goes.
  • --conf-path=/etc/nginx/nginx.conf: Chooses the main NGINX configuration file location.
  • --http-log-path=/var/log/nginx/access.log and --error-log-path=/var/log/nginx/error.log: Define where the log files are.
  • --with-pcre: Turns on PCRE (Perl Compatible Regular Expressions) for configuration files.
  • --lock-path=/var/lock/nginx.lock and --pid-path=/var/run/nginx.pid: Set locations for the lock and pid files.
  • --with-http_ssl_module: Activates the SSL module for secure web connections.
  • --with-http_image_filter_module=dynamic: Turns on the image filter module.
  • --modules-path=/etc/nginx/modules: Defines where dynamic modules go.
  • --with-http_v2_module: Turns on the HTTP/2 module.
  • --with-stream=dynamic: Dynamically activates the stream module.
  • --with-http_addition_module and --with-http_mp4_module: Turn on the addition and MP4 modules.

If you don’t want to use the HTTP/3 module, just leave out --with-http_v3_module. HTTP/3 offers faster, more reliable web browsing.

Screenshot showcasing successful configuration of NGINX build on Debian Linux.
A close-up view of the successful configuration screen for NGINX build on Debian Linux.

Step 6: Install NGINX (Compile and Build NGINX)

After configuring the options for building NGINX from the source, it’s time to compile and install NGINX. This is a two-step process:

First, the make command compiles the NGINX source code using the options specified in the ./configure script. This creates the NGINX binary executable:

make
Screenshot of the "make complete" message during NGINX build installation on Debian Linux.
Final stages of the NGINX build process on Debian Linux, showcasing the “make complete” status.

Second, the sudo make install command installs the NGINX binary, configuration files, and other files to the prefix path specified in the ./configure script:

sudo make install

After installation, NGINX will be located in the sbin directory of the prefix path.

Screenshot of a completed NGINX build and installation on Debian Linux.
This is An illustrative example of a successful NGINX build and installation on Debian Linux.

Step 7: Create NGINX SystemD Service

After building and compiling NGINX from the source, creating a systemd process to manage the NGINX service on your system is crucial. Here’s how:

Create a new systemd service file:

sudo nano /etc/systemd/system/nginx.service

Add the following content to the file:

[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=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Reload the systemd daemon:

sudo systemctl daemon-reload

Start the NGINX service:

sudo systemctl start nginx

Enable the NGINX service to start automatically at boot:

sudo systemctl enable nginx

Finally, ensure the service is activated:

systemctl status nginx

Step 8: Test NGINX

To verify that NGINX is running correctly, open a web browser and navigate to the test page using your local host or server IP address:

http://localhost

Or replace localhost with your server IP address.

Screenshot of a test browser page confirming the successful installation and compilation of NGINX on Debian Linux.
Browser test page showcasing NGINX’s successful compilation and installation on Debian Linux.

Additional Commands & Tips

Compile NGINX with Additional Modules

You can enhance NGINX’s functionality by compiling it with additional modules. For instance, to use the Nginx HTTP push module, use the --add-module flag during the configuration of NGINX:

./configure --add-module=/path/to/nginx-http-push-module
make
sudo make install

Conclusion

This guide has provided a detailed walkthrough of compiling NGINX on Debian 12, 11, and 10. By compiling NGINX from the source, you can customize your installation to include additional modules not included in the default package. This allows you to have a tailored NGINX setup optimized for your needs.

Leave a Comment