HomeLinuxHow to Install Nginx on Ubuntu: PPA/Official Repository, UFW Firewall, and Configuration Structure

How to Install Nginx on Ubuntu: PPA/Official Repository, UFW Firewall, and Configuration Structure

S
SysAlbania
August 12, 2026
5 min246 views
How to Install Nginx on Ubuntu: PPA/Official Repository, UFW Firewall, and Configuration Structure

An essential skill for Linux system administrators, web developers, and cybersecurity professionals is knowing how to install, secure, and manage Nginx on Ubuntu. Nginx is a high-performance web server, reverse proxy, and load balancer known for its low memory usage and ability to handle high concurrent traffic.


Nginx stands as one of the leading web servers globally, playing a crucial role in hosting numerous high-traffic websites. Its lightweight architecture allows it to function effectively as both a web server and a reverse proxy.

This guide provides comprehensive instructions for installing Nginx on Ubuntu, configuring the firewall, managing the Nginx process, and establishing server blocks to host multiple domains on a single server.


To install and configure Nginx on Ubuntu, you can use the Ubuntu default repositories, the official Nginx PPA, or the upstream Nginx repository, followed by configuring the UFW firewall and organizing your sites within Nginx’s standard directory structure.


1. Installation Options

Option A: Ubuntu Default Repository (Easiest & Most Stable)

The standard Ubuntu package repository provides an enterprise-ready, thoroughly tested version of Nginx. This method offers the most streamlined setup and integrates seamlessly with Ubuntu system updates.

Bash
sudo apt update
sudo apt install nginx -y

Option B: Official Nginx Repository (Latest Mainline/Stable)

If you require access to the newest upstream features, security patches, or HTTP/3 capabilities, install directly from the official Nginx PPA repository.

Bash
# Install required dependencies
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y

# Import official Nginx signing key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg > /dev/null

# Add official Nginx Mainline repository
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

# Set APT pinning to prefer Nginx packages over Ubuntu defaults
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx

# Update package lists and install Nginx
sudo apt update
sudo apt install nginx -y

2. UFW Firewall Configuration

Ubuntu utilizes Uncomplicated Firewall (UFW) to control network traffic. Network services must explicitly be granted permission to accept incoming HTTP and HTTPS traffic on ports 80 and 443.

Check Available Application Profiles

Bash
sudo ufw app list

Allow Web Traffic

  • Ubuntu Repository Package: Uses UFW application profiles.

    Bash
    # Allow HTTP (Port 80) and HTTPS (Port 443)
    sudo ufw allow 'Nginx Full'
    
    # Alternatively, allow HTTP only:
    # sudo ufw allow 'Nginx HTTP'
    
  • Official Upstream Package: The upstream package does not include UFW application profiles, so ports must be enabled explicitly.

    Bash
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    

Verify Firewall Rules

Bash
sudo ufw status

3. Nginx Configuration Structure

Nginx configuration files are organized logically within /etc/nginx/. Understanding this hierarchy is essential for managing virtual hosts (Server Blocks) and server directives.

File / DirectoryDescription & Functionality
/etc/nginx/nginx.confMain global configuration file. Defines worker processes, event models, error logs, and performance directives.
/etc/nginx/sites-available/Stores site-specific Server Block configurations. Files here are inactive until linked to sites-enabled.
/etc/nginx/sites-enabled/Contains active symbolic links pointing back to configurations in sites-available/.
/etc/nginx/conf.d/Drop-in directory for HTTP configurations. Official upstream Nginx packages use this directory directly for active site configurations.
/etc/nginx/snippets/Holds reusable configuration snippets, such as SSL hardening rules, security headers, or FastCGI PHP parameters.
/var/log/nginx/Stores default operational logs: access.log (incoming requests) and error.log (system warnings and diagnostic errors).
/var/www/html/Default root directory where web content files are hosted.

4. Basic Management Commands

Control the Nginx system service using standard systemctl commands and test configuration syntax to prevent service interruptions.

Bash
# Verify configuration syntax before reloading
sudo nginx -t

# Apply configuration changes without dropping connections
sudo systemctl reload nginx

# System service management
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# Enable Nginx to start automatically on boot
sudo systemctl enable nginx

Related Topics
NginxUbuntuLinuxDevOpsSysAdminWebDevelopmentWebHostingCloudComputingOpenSourceServerManagement
Up next
Using Wordlists for Brute Force Attack in Kali Linux
Linux · 5 min
Read

Related Articles

Comments(0)

$ sign in to comment