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

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

1 week ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

1 week ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

1 week ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

2 weeks ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

2 weeks ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

2 weeks ago