How to Install and Configure Nginx on Ubuntu

Learn how to install and configure Nginx on Ubuntu, set up the firewall, verify it's running, and manage the Nginx service with systemctl

Nginx is one of the most widely used web servers in the world, powering everything from small personal blogs to high-traffic production applications. If you’re setting up a new Linux server, learning how to install and configure Nginx on Ubuntu is one of the first — and most important — skills you’ll need.

In this guide, you’ll learn how to:

  • Install Nginx on your Ubuntu server
  • Configure the firewall to allow web traffic
  • Confirm Nginx is running correctly in your browser
  • Manage the Nginx service using systemctl
  • Get familiar with Nginx’s key configuration files and directories

By the end, you’ll have a fully working Nginx installation ready to serve a static site or act as a reverse proxy for an application.

Prerequisites

Before you begin, make sure you have:

  • An Ubuntu server that’s up and running. If you haven’t set one up yet, follow our guide on Setting Up Your Ubuntu Server on AWS EC2 first.
  • A non-root user with sudo privileges configured on that server
  • Basic comfort working in the terminal over SSH

Once those are in place, you’re ready to install Nginx.

Step 1: Install Nginx

Nginx is available directly from Ubuntu’s default package repositories, which makes installation straightforward using apt.

First, update your local package index so you’re installing the latest available version:

				
					sudo apt update
				
			

Then install Nginx:

				
					sudo apt install nginx -y
				
			

Once the installation finishes, Nginx starts automatically. You can confirm it’s running with:

				
					sudo systemctl status nginx
				
			

You should see output showing the service as active (running).

Step 2: Set Up the Firewall

If you’re running UFW (Uncomplicated Firewall) — which is common on Ubuntu servers — you’ll need to open a port so traffic can actually reach Nginx. When Nginx is installed, it automatically registers a few application profiles with UFW.

You can see them with:

				
					sudo ufw app list
				
			

You should see output similar to:

				
					Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH
				
			
  • Nginx HTTP opens port 80 (regular, unencrypted web traffic)
  • Nginx HTTPS opens port 443 (encrypted traffic only)
  • Nginx Full opens both 80 and 443
  • OpenSSH opens port 22 (SSH access)

To allow HTTP, HTTPS, and SSH traffic, allow OpenSSH first, then Nginx Full:

				
					sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
				
			

⚠️ Important: Always allow OpenSSH before enabling UFW. If you enable the firewall without an SSH rule in place, you'll lose your SSH connection and lock yourself out of the server.

Now enable UFW:

				
					sudo ufw enable
				
			

Confirm the rules were added:

				
					sudo ufw status
				
			

You should see OpenSSH and Nginx Full both allowed:

				
					Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)                ALLOW      Anywhere (v6)
Nginx Full (v6)             ALLOW      Anywhere (v6)
				
			

💡 Opening port 443 here doesn’t mean HTTPS is working yet — Nginx isn’t actually listening on that port until you configure SSL. It just means the firewall is ready and you won’t need to revisit it later. We cover the SSL setup itself in Securing Nginx with Let’s Encrypt via Certbot.

Step 3: Check in Your Web Browser

With Nginx installed and the firewall configured, it’s time to confirm everything works by loading the default Nginx page in a browser.

First, grab your server’s public IP address and then open a browser and navigate to:

				
					http://your_server_ip
				
			

You should see the default Nginx welcome page with the message “Welcome to nginx!” This confirms Nginx is installed correctly and reachable over the internet.

Step 4: Manage the Nginx Process

Now that Nginx is running, you’ll want to know how to control it. Nginx runs as a systemd service, so all the standard systemctl commands apply.

Stops the Nginx service

				
					sudo systemctl stop nginx
				
			

Starts the Nginx service

				
					sudo systemctl start nginx
				
			

Stops and restarts Nginx (drops active connections briefly)

				
					sudo systemctl restart nginx
				
			

Reloads Nginx without dropping connections — use this after config changes

				
					sudo systemctl reload nginx
				
			

Stops Nginx from starting automatically at boot

				
					sudo systemctl disable nginx
				
			

Re-enables Nginx to start automatically at boot

				
					sudo systemctl enable nginx
				
			

Now for the main event. Run Certbot with the Nginx plugin:

💡 Pro tip: Before reloading or restarting Nginx after a config change, always test the configuration for syntax errors first:

				
					sudo nginx -t
				
			

If the test passes, you’ll see syntax is ok and test is successful — then it’s safe to reload.

Step 5: Getting Familiar with Important Nginx Files and Directories

Now that you know how to manage the Nginx service, it’s worth understanding where its files actually live on disk.

Content

  • /var/www/html — The default web root. This is where Nginx looks for files to serve unless you configure a different directory in a server block.

Server Configuration

  • /etc/nginx — The main Nginx configuration directory
  • /etc/nginx/nginx.conf — The main Nginx configuration file, where global settings live.
  • /etc/nginx/sites-available/ — Where you define individual server blocks (virtual hosts) for each site. Files here aren’t active until linked into sites-enabled.
  • /etc/nginx/sites-enabled/ — Contains symbolic links to the server blocks in sites-available that are currently active.
  • /etc/nginx/snippets/ — Reusable configuration fragments you can include across multiple server blocks (SSL settings, common headers, etc.).

Log Files

  • /var/log/nginx/access.log — Logs every request made to your server.
  • /var/log/nginx/error.log — Logs any errors Nginx encounters, invaluable for troubleshooting.

Conclusion

You’ve now installed and configured Nginx on Ubuntu from the ground up — from installation and firewall rules to verifying it in the browser and understanding where its core files live. At this point, Nginx is ready to serve a static site or sit in front of an application as a reverse proxy.

From here, you might want to:

ON THIS PLACE