UFW (Uncomplicated Firewall) is Ubuntu's friendly front-end to iptables. It lets you control which ports are open with simple commands.

Basic setup

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH          # don't lock yourself out!
sudo ufw enable

Opening common services

sudo ufw allow 'Nginx Full'    # ports 80 and 443
sudo ufw allow 443/tcp
sudo ufw allow from 203.0.113.5 to any port 5432   # DB from one IP only

Managing rules

sudo ufw status numbered
sudo ufw delete 3
sudo ufw disable

Best practice

Deny everything by default and open only what you need. Keep the SSH rule in place before enabling UFW, and restrict sensitive ports (databases, admin panels) to specific IPs rather than the whole internet. A tight firewall dramatically reduces your attack surface.