UFW (Uncomplicated Firewall) is the easiest way to protect a Linux server.
sudo apt install ufw sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow 22/tcp # SSH – do this first! sudo ufw allow 80/tcp # HTTP sudo ufw allow 443/tcp # HTTPS sudo ufw enable sudo ufw status verbose
Important: always allow your SSH port before enabling the firewall, otherwise you lock yourself out. If that happens, use the console in your control panel to fix the rules.
Only open the ports you really need, and restrict sensitive services (databases, admin panels) to your own IP address: sudo ufw allow from YOUR.IP to any port 3306.