3 Ways to Install NGINX on Ubuntu: A Comprehensive Guide

Updated: January 19, 2024 By: Guest Contributor Post a comment

Introduction

NGINX is a powerful and efficient web server that’s known for its high performance and low resource consumption. This tutorial explores various methods to install NGINX on Ubuntu, providing detailed steps and ensuring you can get your web server up and running in no time.

Solution 1: Install NGINX using Ubuntu’s Package Manager

This solution entails using Ubuntu’s built-in package manager, APT (Advanced Package Tool), to install NGINX. It is the easiest and most straightforward method to install NGINX on Ubuntu, requiring minimal user input.

  • Step 1: Update the package lists to ensure you get the latest version of the package.
  • Step 2: Install NGINX using APT.
  • Step 3: Verify the installation and start the NGINX service.

Example:

# Update the package repository to get the latest package information
sudo apt update

# Install Nginx using the package manager
sudo apt install nginx

# Check the status of the Nginx service using systemctl
systemctl status nginx

Output:

nginx.service - A high-performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2024-05-09 10:37:00 UTC; 10min ago

Notes: Installing NGINX through the package manager ensures that you get a stable version tested with your version of Ubuntu. It is the recommended approach for most use cases. However, the version in the official Ubuntu repository may not be the latest.

Solution 2: Install NGINX From the Official NGINX Repository

For those who prefer the latest features over the possibly older, stabilized version in Ubuntu’s default repositories, installing NGINX directly from the official NGINX repository is the way to go.

  • Step 1: Add the official NGINX repository to your list of sources.
  • Step 2: Update the package lists.
  • Step 3: Install NGINX.
  • Step 4: Start and verify the NGINX service.

Example:

# Download the Nginx signing key using wget
wget http://nginx.org/keys/nginx_signing.key

# Add the Nginx signing key to the system's keyring
sudo apt-key add nginx_signing.key

# Add the Nginx repository to the package sources list with the appropriate codename using printf and tee
printf "deb http://nginx.org/packages/ubuntu/ `lsb_release -cs` nginx\ndeb-src http://nginx.org/packages/ubuntu/ `lsb_release -cs` nginx\n" | sudo tee /etc/apt/sources.list.d/nginx.list

# Update the package repository to get the latest package information
sudo apt update

# Install Nginx using the package manager
sudo apt install nginx

# Check the status of the Nginx service using systemctl
systemctl status nginx

Notes: This method gives you access to the latest NGINX features and updates. There might be compatibility issues with other software expecting the version from the Ubuntu’s default repositories. Always understand the changelogs before upgrading to newer versions.

Solution 3: Compile NGINX from Source

For the ultimate in control and the ability to customize the NGINX build, you can compile NGINX directly from the source.

  • Step 1: Install the required build tools and dependencies.
  • Step 2: Download NGINX source code.
  • Step 3: Configure the build options.
  • Step 4: Compile and install NGINX.

Example:

# Update the package repository to get the latest package information
sudo apt update

# Install essential development tools and libraries required for building Nginx
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

# Download the Nginx source code
wget https://nginx.org/download/nginx-1.21.1.tar.gz

# Extract the downloaded archive
tar -zxvf nginx-1.21.1.tar.gz

# Change to the Nginx source code directory
cd nginx-1.21.1/

# Configure Nginx build options
./configure

# Compile Nginx
make

# Install Nginx (requires sudo privileges)
sudo make install

No output unless there is an error; a successful installation will be completed quietly.

Notes: This method gives you complete control over the features included in your NGINX build. However, it is significantly more complex and requires more maintenance than using precompiled packages. It is meant for advanced users with specific needs.

Conclusion

Each method of installing NGINX on Ubuntu serves a different purpose, catering to the varied needs of users. For many users, using Ubuntu’s package manager will supply a stable and easy-to-maintain setup. The official NGINX repository is a go-to for those requiring the latest features, while compiling from source provides the most customization and control at the cost of simplicity. Understanding the benefits and drawbacks of each method will inform users to choose the best approach for their specific circumstances.