How To

Install Nginx on Ubuntu 18.04: UFW, Service Control, and Config

Nginx (pronounced “engine x”) is a free, open-source, high-performance HTTP server and reverse proxy. It handles a large number of concurrent connections with a small memory footprint per connection, which makes it a popular choice for high-traffic sites, APIs, and microservice infrastructures.

Nginx can work as a standalone web server or as a reverse proxy in front of another application server. Compared to Apache, its event-driven, non-blocking architecture scales much better under concurrent connection load.

This guide covers how to install Nginx on Ubuntu 18.04, open the firewall with UFW, manage the service with systemctl, and understand the Nginx configuration directory structure.

<strong>Prerequisite:</strong>&nbsp;You need sudo access. Make sure Apache or another service is not running on port 80 or 443.

Install Nginx on Ubuntu and Open Firewall Ports

Nginx packages are available in the Ubuntu 18.04 default repositories:

bashsudo apt updatesudo apt install nginx

The Nginx service starts automatically after installation. Verify it is running:

bashsudo systemctl status nginx

The output should show Active: active (running).

Open UFW ports. Use the Nginx Full profile to open both HTTP (port 80) and HTTPS (port 443) with one command:

bashsudo ufw allow 'Nginx Full'

Verify with sudo ufw status. Visit http://YOUR_SERVER_IP in a browser — the default Nginx welcome page confirms the server is installed and reachable.

Manage the Nginx Service with systemctl

All Nginx service operations use standard systemd commands:

ActionCommand
Stop Nginxsudo systemctl stop nginx
Start Nginxsudo systemctl start nginx
Restart Nginxsudo systemctl restart nginx
Reload config onlysudo systemctl reload nginx
Disable auto-start at bootsudo systemctl disable nginx
Re-enable auto-start at bootsudo systemctl enable nginx

Use reload when you have only changed configuration files. Reload applies the new config without dropping active connections, which is important on live servers. Use restart only when installing new Nginx modules or making changes that require a full process restart.

Nginx Configuration File Structure and Best Practices

Understanding the Nginx directory layout is essential for managing multiple domains without creating config conflicts.

  • /etc/nginx/ — root directory for all Nginx configuration files
  • /etc/nginx/nginx.conf — main configuration file; sets global settings like worker processes and connection limits
  • /etc/nginx/sites-available/ — stores server block config files for each domain; files here are inactive until symlinked
  • /etc/nginx/sites-enabled/ — contains symbolic links to active server block files
  • /etc/nginx/snippets/ — reusable config fragments you can include across multiple server blocks (useful for SSL settings)
  • /var/log/nginx/ — stores access.log and error.log; create a separate log pair for each server block

Naming convention. Name each config file after the domain it serves, for example /etc/nginx/sites-available/example.com.conf. Activate it by creating a symlink:

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

Common document root locations include /var/www/example.com/home/user/example.com, and /opt/example.com. Pick one location and apply it consistently across all your virtual hosts to simplify maintenance.

Nginx is now installed and running on your Ubuntu 18.04 server. To add HTTPS, follow the guide on securing Nginx with a free Let’s Encrypt SSL certificate. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

Install Nginx on Ubuntu 16.04: UFW, PPA, and Config Structure

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server and reverse proxy. It handles…

43 minutes ago

Secure Nginx with Let’s Encrypt on Ubuntu 18.04: SSL Setup Guide

Let's Encrypt is a free, automated, and open certificate authority run by the Internet Security Research…

50 minutes ago

Install PHP on Ubuntu 18.04: Apache, Nginx FPM, and Ondrej PPA

PHP is the most widely used server-side scripting language for web development. Ubuntu 18.04 ships with PHP…

54 minutes ago

Install Skype on Ubuntu 18.04: .deb Package and Auto-Updates

Skype is one of the most widely used communication platforms in the world. It lets you…

59 minutes ago

Install Samba on Ubuntu 18.04: Configure Shares and User Access

Samba is a free, open-source implementation of the SMB/CIFS network protocol that lets Linux servers share…

22 hours ago

Set Up an OpenVPN Server on Ubuntu 18.04 with EasyRSA and UFW

Running your own VPN gives you full control over your traffic, privacy, and connection security. It encrypts…

22 hours ago