IDONCLOUDHOSTING & NETWORK SERVICES

How to Install Nginx on Ubuntu Print

  • 0

Nginx is a popular open-source web server used to host websites, applications, APIs and reverse proxies.

This guide explains how to install Nginx on Ubuntu, configure basic firewall access and verify that the web server is working correctly.

Step 1: Update Ubuntu

Before installing Nginx, update the package list:

sudo apt update

Install available updates:

sudo apt upgrade -y

Step 2: Install Nginx

Install Nginx using APT:

sudo apt install nginx -y

After installation, Nginx should start automatically.

Step 3: Check Nginx Status

Check whether Nginx is running:

sudo systemctl status nginx

You should see:

active (running)

Press q to exit the status screen.

You can also verify that Nginx is listening on port 80:

sudo ss -ltnp | grep ':80'

Step 4: Enable Nginx at Boot

Nginx is normally enabled automatically after installation.

Check:

sudo systemctl is-enabled nginx

If necessary, enable it:

sudo systemctl enable nginx

This ensures Nginx starts automatically after a server reboot.

Step 5: Configure the Firewall

If UFW is enabled, check the current status:

sudo ufw status

List available application profiles:

sudo ufw app list

You may see:

Nginx Full
Nginx HTTP
Nginx HTTPS

To allow HTTP traffic:

sudo ufw allow 'Nginx HTTP'

If you plan to use both HTTP and HTTPS:

sudo ufw allow 'Nginx Full'

Before changing firewall rules on a remote server, make sure SSH access remains allowed.

Step 6: Test Nginx

Find your server IP address:

ip addr

Then open:

http://SERVER-IP

Replace SERVER-IP with the public IPv4 address of your server.

If Nginx is working correctly, you should see the default Nginx welcome page.

You can also test from the command line:

curl -I http://SERVER-IP

A successful response may include:

HTTP/1.1 200 OK
Server: nginx

Step 7: Important Nginx Directories

The main Nginx configuration file is:

/etc/nginx/nginx.conf

Website configurations are commonly stored in:

/etc/nginx/sites-available/

Enabled website configurations are normally located in:

/etc/nginx/sites-enabled/

The default web root is usually:

/var/www/html

The default website configuration is normally:

/etc/nginx/sites-available/default

Step 8: Create a Simple Test Page

Open the default Nginx page:

sudo nano /var/www/html/index.html

Add:

<!DOCTYPE html>
<html>
<head>
    <title>My Server</title>
</head>
<body>
    <h1>Nginx is working!</h1>
</body>
</html>

Save the file.

Now open your server IP again:

http://SERVER-IP

You should see the new page.

Step 9: Test Nginx Configuration

Before applying configuration changes, always test Nginx:

sudo nginx -t

If the configuration is valid, Nginx will report that the syntax is OK and the configuration test was successful.

Reload Nginx:

sudo systemctl reload nginx

Start, Stop and Restart Nginx

Start Nginx:

sudo systemctl start nginx

Stop Nginx:

sudo systemctl stop nginx

Restart Nginx:

sudo systemctl restart nginx

Reload Nginx without stopping the web server:

sudo systemctl reload nginx

Check Nginx Logs

Nginx logs are useful when troubleshooting website problems.

Access log:

/var/log/nginx/access.log

Error log:

/var/log/nginx/error.log

Monitor the error log:

sudo tail -f /var/log/nginx/error.log

Monitor incoming requests:

sudo tail -f /var/log/nginx/access.log

Press Ctrl + C to stop monitoring.

Configure a Domain with Nginx

Before configuring a domain, make sure its DNS A record points to your server’s public IPv4 address.

For example:

example.com → 192.0.2.10

Create a website directory:

sudo mkdir -p /var/www/example.com

Create a basic page:

sudo nano /var/www/example.com/index.html

Then create an Nginx configuration:

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

Add:

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

    server_name example.com www.example.com;

    root /var/www/example.com;
    index index.html;

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

Enable the website:

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

Test the configuration:

sudo nginx -t

If the configuration is valid, reload Nginx:

sudo systemctl reload nginx

Your domain should now be served by Nginx.

Add HTTPS with Let’s Encrypt

Once the domain points to the server and works over HTTP, you can secure it with a free Let’s Encrypt SSL certificate.

Install Certbot and the Nginx plugin:

sudo apt install certbot python3-certbot-nginx -y

Request a certificate:

sudo certbot --nginx -d example.com -d www.example.com

Replace example.com with your actual domain.

Certbot can automatically configure Nginx to use HTTPS.

Troubleshooting

Nginx Will Not Start

Test the configuration:

sudo nginx -t

Check the service:

sudo systemctl status nginx

Check recent errors:

sudo tail -50 /var/log/nginx/error.log

Port 80 Is Already in Use

Check which process is using port 80:

sudo ss -ltnp | grep ':80'

Another web server or application may already be using the port.

Domain Does Not Load

Check that the domain resolves to the correct IP:

dig +short example.com

Check Nginx:

sudo systemctl status nginx

Check the firewall:

sudo ufw status

Check the Nginx configuration:

sudo nginx -t

Keep Nginx Updated

Keep your Ubuntu packages updated:

sudo apt update
sudo apt upgrade -y

Regular updates help keep your web server secure and stable.

IDONCLOUD Server Infrastructure

IDONCLOUD provides VPS and Dedicated Server infrastructure for websites, applications and Linux workloads, with IPv4, IPv6 and high-bandwidth connectivity options.

Our Knowledge Base provides practical guides for Linux, networking, web servers and server administration.

Visit idoncloud.com to explore IDONCLOUD infrastructure.

Related Guides

  • How to Point a Domain Name to Your Server
  • How to Install Let’s Encrypt SSL on Ubuntu with Certbot
  • How to Redirect HTTP to HTTPS with Nginx
  • How to Create an Nginx Reverse Proxy
  • How to Check Open Ports on a Linux Server

Was this answer helpful?

« Back