The Complete NGINX on Ubuntu Series: Part 1 – Installation and Basic Configuration

The Complete NGINX on Ubuntu Series: Part 1 – Installation and Basic Configuration

This entry is part 1 of 5 in the series The Complete NGINX on Ubuntu Series

Welcome to the first part of our comprehensive NGINX on Ubuntu series! Over the next 22 posts, we’ll take you from complete beginner to advanced NGINX administrator. Today, we’ll start with the absolute basics: installing NGINX on Ubuntu 24.04 and understanding its fundamental configuration.

What is NGINX?

NGINX (pronounced “engine-x”) is a high-performance web server, reverse proxy, and load balancer. Originally created by Igor Sysoev in 2004, NGINX has become one of the most popular web servers worldwide, powering over 400 million websites including Netflix, Airbnb, and WordPress.com.

Why Choose NGINX Over Apache?

NGINX offers several advantages that make it particularly suitable for modern web applications:

  • High Performance: Uses an event-driven, asynchronous architecture
  • Low Memory Usage: Handles thousands of concurrent connections efficiently
  • Reverse Proxy: Excellent for microservices and load balancing
  • Static Content: Serves static files incredibly fast
graph TD
    A[Client Request] --> B[NGINX Server]
    B --> C{Request Type}
    C -->|Static Files| D[Serve Directly]
    C -->|Dynamic Content| E[Forward to Backend]
    D --> F[Return to Client]
    E --> G[PHP/Node.js/Python]
    G --> F

Prerequisites

Before we begin, ensure you have:

  • Ubuntu 24.04 LTS server (fresh installation recommended)
  • Root or sudo access
  • Basic command line knowledge
  • Internet connection for package downloads

Method 1: Installing NGINX from Ubuntu Repository (Recommended for Beginners)

The easiest way to install NGINX is through Ubuntu’s official package repository. This method provides automatic updates and security patches.

Step 1: Update System Packages

# Update package index
sudo apt update

# Upgrade existing packages (optional but recommended)
sudo apt upgrade -y

Step 2: Install NGINX

# Install NGINX
sudo apt install nginx -y

# Verify installation
nginx -v

You should see output similar to: nginx version: nginx/1.24.0 (Ubuntu)

Step 3: Start and Enable NGINX

# Start NGINX service
sudo systemctl start nginx

# Enable NGINX to start on boot
sudo systemctl enable nginx

# Check service status
sudo systemctl status nginx

Method 2: Installing from Official NGINX Repository

For the latest stable version, you can install from NGINX’s official repository:

# Install prerequisites
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

# Import NGINX signing key
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor -o /usr/share/keyrings/nginx-keyring.gpg

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

# Update and install
sudo apt update
sudo apt install nginx -y

Testing Your NGINX Installation

Once installed, test if NGINX is working correctly:

# Test NGINX configuration
sudo nginx -t

# Check if NGINX is listening on port 80
sudo netstat -tulnp | grep :80

# Alternative check with ss command
sudo ss -tulnp | grep :80

Open your web browser and navigate to your server’s IP address. You should see the default NGINX welcome page:

# Find your server's IP address
ip addr show
# or
hostname -I

Understanding NGINX Directory Structure

NGINX follows a standard directory structure on Ubuntu. Understanding this is crucial for effective management:

graph TD
    A[/etc/nginx/] --> B[nginx.conf - Main Config]
    A --> C[sites-available/ - Virtual Hosts]
    A --> D[sites-enabled/ - Active Sites]
    A --> E[conf.d/ - Additional Configs]
    A --> F[snippets/ - Reusable Configs]
    
    G[/var/log/nginx/] --> H[access.log]
    G --> I[error.log]
    
    J[/var/www/html/] --> K[Default Web Root]
    
    L[/usr/share/nginx/html/] --> M[Default Pages]

Key Directories Explained:

  • /etc/nginx/nginx.conf: Main configuration file
  • /etc/nginx/sites-available/: Available site configurations
  • /etc/nginx/sites-enabled/: Symbolic links to enabled sites
  • /var/www/html/: Default document root
  • /var/log/nginx/: Access and error logs

Basic NGINX Commands

Master these essential commands for day-to-day NGINX management:

# Service Management
sudo systemctl start nginx      # Start NGINX
sudo systemctl stop nginx       # Stop NGINX
sudo systemctl restart nginx    # Restart NGINX
sudo systemctl reload nginx     # Reload configuration
sudo systemctl status nginx     # Check status

# Configuration Testing
sudo nginx -t                   # Test configuration syntax
sudo nginx -T                   # Test and dump configuration

# Manual Control
sudo nginx -s reload            # Reload configuration
sudo nginx -s reopen            # Reopen log files
sudo nginx -s stop              # Fast shutdown
sudo nginx -s quit              # Graceful shutdown

Firewall Configuration

If you’re using UFW (Ubuntu’s default firewall), allow HTTP and HTTPS traffic:

# Check available app profiles
sudo ufw app list

# Allow NGINX Full (HTTP and HTTPS)
sudo ufw allow 'Nginx Full'

# Alternative: Allow specific ports
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# Check firewall status
sudo ufw status

Troubleshooting Common Installation Issues

Issue 1: Port 80 Already in Use

# Check what's using port 80
sudo netstat -tulnp | grep :80
# or
sudo lsof -i :80

# If Apache is running, stop it
sudo systemctl stop apache2
sudo systemctl disable apache2

Issue 2: Permission Denied

# Check NGINX user
ps aux | grep nginx

# Ensure correct permissions on web directory
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

Your First Custom Page

Let’s create a simple custom page to verify everything works:

# Create a custom index page
sudo nano /var/www/html/index.html

Add this content:

<!DOCTYPE html>
<html>
<head>
    <title>My NGINX Server</title>
</head>
<body>
    <h1>Welcome to My NGINX Server!</h1>
    <p>NGINX is successfully running on Ubuntu 24.04</p>
    <p>Server time: <script>document.write(new Date());</script></p>
</body>
</html>

Save the file (Ctrl+X, Y, Enter) and reload your browser. You should see your custom page!

What’s Next?

Congratulations! You’ve successfully installed NGINX on Ubuntu and created your first custom page. In Part 2 of our series, we’ll dive deeper into understanding NGINX configuration files and directory structure.

Coming up in Part 2: Understanding NGINX Configuration Files and Directory Structure

References and Further Reading


This is Part 1 of our 22-part series “The Complete NGINX on Ubuntu Guide.” Follow along as we progress from beginner basics to advanced enterprise configurations. Have questions? Leave them in the comments below!

NavigateThe Complete NGINX on Ubuntu Series: Part 2 – Understanding Configuration Files and Directory Structure >>

Written by:

339 Posts

View All Posts
Follow Me :