Blog

Installing the Official NGINX OSS Package via Script

By Jay

Aug 7, 2023 | 7 minutes read

Series: HomeLab

Tags: blog, tech, nginx

This blog post will explore the benefits of using the official NGINX OSS package and walk you through the installation process. Additionally, I’ll share a script I created for installing and setting up NGINX, offering you a handy tool to simplify the installation process for those who prefer using a script. Ansible aficionados will be happy to know that I’ll be covering how to use the amazing Ansible Collection for NGINX for NGINX installation, configuration, and upgrades in the next blog post.

NGINX is the most popular webserver and reverse proxy in the world, and if you work in tech there is a very good chance that you’ve used it in the past. But did you know that not all NGINX installations are created equal?

The official Open Source NGINX (OSS) package from F5/NGINX software repositories is the primary open-source release from NGINX and is the recommended option for most users - but you have other choices, such as the package managers for your distribution (e.g., Canonical, RedHat) or cloud-based images (e.g., AWS, Azure, GCP). If you’re feeling adventurous, you can even build NGINX from source. However, for most users the official NGINX OSS package is the recommended choice.

Why is that? Let’s take a look at some of the benefits:

Reliability and Stability The official NGINX OSS package is developed and maintained directly by the core NGINX team, ensuring a high level of reliability and stability. It undergoes rigorous testing and quality control before each release, making it a trustworthy option for production environments.

Security Updates and Patches NGINX is a widely used server, and like any software, it may have vulnerabilities from time to time. The official package provides prompt security updates and patches to address any discovered vulnerabilities, minimizing the risk of security breaches.

Feature Updates and Improvements The NGINX team continuously works on enhancing the web server’s performance and features. By using the official package, you can stay up-to-date with the latest improvements, bug fixes, and performance optimizations.

Compatibility and Integration Third-party tools and modules often integrate more seamlessly with the official NGINX OSS package since it adheres to standard NGINX conventions. This compatibility ensures a smoother experience when incorporating additional functionality into your NGINX setup.

Frequent Releases and Long-Term Support (LTS) The official NGINX OSS package follows a release schedule with frequent updates. Additionally, long-term support (LTS) versions are available for those who prioritize stability over new features and are looking for extended maintenance.

Transparent Licensing The official NGINX OSS package is licensed under the 2-clause BSD license, which is a permissive and open-source license. This licensing model allows you to use and modify NGINX without the need for complex legal considerations.

Full NGINX Functionality The official NGINX OSS package includes the complete feature set of NGINX, allowing you to take advantage of all the core functionalities and capabilities that NGINX offers as a web server and reverse proxy.

Familiarity and Documentation As the official release, the NGINX OSS package aligns with the official documentation, making it easier to find relevant resources and examples. This consistency contributes to a smoother learning curve and troubleshooting process.

In summary, using the official NGINX OSS package provides you with a reliable, well-supported, and feature-rich installation server that is actively maintained and improved by the NGINX team and community. Its transparency, security updates, and compatibility make it a solid choice for deploying NGINX in various environments. That said, the other options are valid choices as well, and you should choose the one that best fits your needs.

NGINX provides a well documented approach to installing NGINX OSS on your platform that you can view at the NGINX Documentation Site here. However, this approach requires a lot of manual work on your system and because it needs to address all possible scenarios, it is not as streamlined as it could be. To get around this, I’ve built a simple script that will detect your operating system, add the necessary prerequisites, update the repository data, and then install NGINX OSS.

Please note that you should never run a script on your system without first reviewing it to ensure that it is doing what you expect it to do. I’ve tried to make this script as simple as possible, but you should always review any script before running it on your system.

#!/bin/bash

# Usage: This script installs NGINX on various Linux distributions
# Run it as root or with sudo privileges
# Usage: sudo ./install_nginx.sh

# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

# Prompt the user to continue
read -p "This script will install NGINX on your system. Do you wish to continue? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

# Function to install NGINX on CentOS/RHEL/Oracle Linux/AlmaLinux/Rocky Linux
install_nginx_centos() {
    # Install necessary utilities
    yum install yum-utils -y || { echo 'Installing yum-utils failed' ; exit 1; }

    # Install EPEL repository
    yum install epel-release -y || { echo 'Installing EPEL repository failed' ; exit 1; }

    # Update the repository
    yum update -y || { echo 'Updating repository failed' ; exit 1; }

    # Install NGINX
    yum install nginx -y || { echo 'Installing NGINX failed' ; exit 1; }

    # Start NGINX
    systemctl start nginx || { echo 'Starting NGINX failed' ; exit 1; }

    # Enable NGINX to start on boot
    systemctl enable nginx || { echo 'Enabling NGINX to start on boot failed' ; exit 1; }
}

# Function to install NGINX on Debian
install_nginx_debian() {
    # Update the Debian repository information
    apt-get update -y || { echo 'Updating repository information failed' ; exit 1; }

    # Install prerequisites
    apt-get install curl gnupg2 ca-certificates lsb-release debian-archive-keyring -y || { echo 'Installing prerequisites failed' ; exit 1; }

    # Import an official NGINX signing key
    curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null || { echo 'Importing NGINX signing key failed' ; exit 1; }

    # Set up the apt repository for stable NGINX packages
    echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | tee /etc/apt/sources.list.d/nginx.list || { echo 'Setting up the apt repository failed' ; exit 1; }

    # Set up repository pinning
    echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | tee /etc/apt/preferences.d/99nginx || { echo 'Setting up repository pinning failed' ; exit 1; }

    # Install the NGINX package
    apt update || { echo 'Updating repository information failed' ; exit 1; }
    apt install nginx -y || { echo 'Installing NGINX failed' ; exit 1; }

    # Start NGINX
    systemctl start nginx || { echo 'Starting NGINX failed' ; exit 1; }

    # Enable NGINX to start on boot
    systemctl enable nginx || { echo 'Enabling NGINX to start on boot failed' ; exit 1; }
}

# Function to install NGINX on Ubuntu
install_nginx_ubuntu() {
    # Update the Ubuntu repository information
    apt-get update -y || { echo 'Updating repository information failed' ; exit 1; }

    # Install NGINX
    apt-get install nginx -y || { echo 'Installing NGINX failed' ; exit 1; }

    # Start NGINX
    systemctl start nginx || { echo 'Starting NGINX failed' ; exit 1; }

    # Enable NGINX to start on boot
    systemctl enable nginx || { echo 'Enabling NGINX to start on boot failed' ; exit 1; }
}

# Check the Linux distribution and call the appropriate function
if [ -f /etc/os-release ]; then
    . /etc/os-release
    case $ID in
        centos|rhel|oraclelinux|almalinux|rocky)
            install_nginx_centos
            ;;
        debian)
            install_nginx_debian
            ;;
        ubuntu)
            install_nginx_ubuntu
            ;;
        *)
            echo "Unsupported Linux distribution"
            exit 1
            ;;
    esac
else
    echo "Not a Linux system or Linux distribution not supported"
    exit 1
fi

# Test if NGINX is running
if systemctl status nginx >/dev/null 2>&1; then
    echo "NGINX is running successfully"
else
    echo "NGINX is not running. Please check the installation"
    exit 1
fi

Installing the official Open Source NGINX from F5/NGINX software repositories is an essential skill for any NGINX enthusiast. Understanding the various NGINX packages available and choosing the appropriate method for your system or cloud platform ensures a successful deployment. Whether you opt for the official NGINX OSS package, distribution-specific packages, or cloud-based images, the flexibility provided by NGINX empowers you to optimize your web server according to your needs. As you prepare to explore the world of Ansible for NGINX installations in the next blog post, enjoy the power and versatility of NGINX in your web infrastructure. Happy NGINX-ing!