How to Install and Configure Nginx on Ubuntu

Nginx is one of the most widely used web servers today, known for its speed, low memory footprint, and ability to handle high traffic loads. Whether you’re hosting a WordPress site, a Magento store, or a custom application, Nginx is often the first layer your visitors’ requests hit.

In this guide, we’ll walk through setting up Nginx from scratch on an Ubuntu server — the same process we followed when setting up our own production server.

Note: This post covers installation and basic configuration only. Server hardening and security parameters are covered in a separate post — “How to Secure Your Server” — coming soon.

Prerequisites

  • A server running Ubuntu 22.04 or 24.04 LTS
  • A non-root user with sudo privileges
  • A registered domain name (optional, but needed if you want to serve a real site)

Step 1: Update the System

Always start with an updated package list:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

sudo apt install nginx -y

Once installed, Nginx starts automatically. Verify it’s running:

sudo systemctl status nginx

You should see active (running) in the output.

Step 3: Allow Nginx Through the Firewall

If ufw is enabled, allow HTTP/HTTPS traffic:

sudo ufw allow 'Nginx Full'
sudo ufw status

Nginx Full opens both port 80 (HTTP) and 443 (HTTPS).

Step 4: Verify in the Browser

Visit your server’s public IP address:

http://your-server-ip

You should see the default “Welcome to nginx!” page. This confirms Nginx is installed and serving traffic correctly.

Step 5: Understand the Nginx Directory Structure

A few key locations you’ll work with regularly:

PathPurpose
/etc/nginx/nginx.confMain configuration file
/etc/nginx/sites-available/Where you define server blocks (virtual hosts)
/etc/nginx/sites-enabled/Symlinks to active server blocks
/var/www/Default web root for your sites
/var/log/nginx/Access and error logs

Step 6: Create a Server Block for Your Domain

Create the web root directory:

sudo mkdir -p /var/www/yourdomain.com/html
sudo chown -R $USER:$USER /var/www/yourdomain.com/html

Create a new server block configuration:

sudo nano /etc/nginx/sites-available/yourdomain.com

Add the following:

server {
    listen 80;
    listen [::]:80;

    root /var/www/yourdomain.com/html;
    index index.html index.htm index.php;

    server_name yourdomain.com www.yourdomain.com;

    location / {
        try_files $uri $uri/ =404;
    }
}

Step 7: Enable the Site

Create a symlink to sites-enabled:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

Test the configuration for syntax errors:

sudo nginx -t

If the test passes, reload Nginx:

sudo systemctl reload nginx

Step 8: Point Your Domain and Add SSL

Once your domain’s DNS A record points to the server’s IP, secure it with a free SSL certificate via Let’s Encrypt:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Certbot automatically updates your Nginx config to redirect HTTP to HTTPS and sets up auto-renewal.

sudo certbot renew --dry-run

Wrapping Up

At this point, you have a working Nginx server, a configured server block for your domain, and a valid SSL certificate. This is the same foundation we use for hosting our own WordPress and Magento environments.

Next up in this series: hardening this server — SSH key auth, firewall rules, fail2ban, and other security parameters to lock things down before going live.

Need help setting up or migrating your store to a production-ready server? Get in touch with our team — server setup and store migration is one of our core services.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *