Short answer
- Set up the firewall on a VPS with a default policy of deny for incoming and allow for outgoing traffic, then open explicitly only what you need.
- Towards the internet, leave open only 22 for SSH plus 80 and 443 for web, and open anything else one rule at a time, with a restricted source.
- Databases, Redis and admin panels listen only on localhost or the private network, never behind a global allow rule.
- Rate-limit SSH with ufw limit 22/tcp and check what really listens with ss -tulpn.
- Default policy
- deny incoming, allow outgoing
- Ports open to the internet
- 22/tcp, 80/tcp, 443/tcp
- Database
- bind to 127.0.0.1 or the private network, remote access through an SSH tunnel
Why you start from default deny
When the policy is “allow everything, block what I know”, every newly installed service becomes public automatically: a Redis without a password, a MongoDB with authentication off, an admin panel on 8080. Scanners find them within minutes. With “deny everything, allow what I know” the mistake flips: a forgotten service stays invisible until you deliberately open it. UFW does this in two commands and keeps the rules readable; underneath, it drives nftables or iptables.
What to do with the database and internal services
MySQL on 3306 and PostgreSQL on 5432 have no business being exposed to the internet, even with a password. Configure them to listen on 127.0.0.1 or on the private-network address, and reach them remotely through an SSH tunnel or a VPN. If the application runs on another server, open the port only for that server's IP. Docker deserves attention: publishing a port with -p bypasses UFW rules, so bind published ports to 127.0.0.1 or to the private interface.
What Romanian law says
A database holding personal data that is reachable from the internet is exactly what Article 32 GDPR treats as a lack of appropriate technical measures, and ANSPDCP has fined such cases in Romania. For entities registered with the DNSC under OUG 155/2024, network segmentation and access control are part of the risk-management measures in Article 11 of OUG 155/2024. A documented firewall with explainable rules is simple evidence in any audit.
Common VPS ports and the recommended exposure| Port / service | Exposure to the internet | How you reach it | UFW rule |
|---|
| 22 SSH | Yes, rate-limited and with a restricted source | SSH keys, VPN | ufw limit 22/tcp |
|---|
| 80 HTTP | Yes, for redirects and ACME validation | Public | ufw allow 80/tcp |
|---|
| 443 HTTPS | Yes | Public | ufw allow 443/tcp |
|---|
| 3306 MySQL / 5432 PostgreSQL | No | localhost, SSH tunnel, private network | ufw allow from 10.0.0.5 to any port 5432 |
|---|
| 6379 Redis | No | localhost, private network | no public rule |
|---|
Commands and configuration
UFW: default policy and the essential portssudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
Database reachable only from the application serversudo ufw allow from 10.0.0.5 to any port 5432 proto tcp
Check what is really listeningss -tulpn | grep LISTEN
Practical examples
SaaS team in Iasi with PostgreSQL on the same VPS
A colleague had set listen_addresses to '*' to connect from home and forgot to revert it. The database was reachable from anywhere, protected only by its password. A default-deny firewall covered the mistake, and remote access moved to an SSH tunnel.
Online shop in Cluj-Napoca using Redis for sessions
Redis published through Docker with -p 6379:6379 bypassed UFW and reached the internet without a password. A scanner found it the same day, with customer sessions readable. Binding it to 127.0.0.1 and adding a password closed the exposure.
Common mistakes
- Installing UFW but never enabling it, or enabling it with the default policy allow incoming.
- Opening 3306 or 5432 to any source so you can connect with a desktop client.
- Publishing Docker ports with -p and no address and assuming UFW covers them.
- Opening ports for debugging and never closing them again.
How 4hosting helps
- Firewall & Network Isolation designs the rules and the network segmentation from your included hours: 1–8 per month on VPS, 5–10 on dedicated servers depending on the plan and 10 on GPU servers, requested from the client panel, in the “DevOps Services” section.
- VPN & WireGuard moves internal panels and databases off the public internet.
- L3/L4 DDoS protection is included at network level; your firewall only decides which services are visible.
Related questions
UFW, nftables or iptables?
UFW is a front end over nftables or iptables, enough for a VPS with a few services, and it keeps rules readable. Move to nftables directly when you need large address sets, rules across several interfaces or performance at high traffic. Do not mix them on the same machine.
Can I open the database port for my home IP?
You can, with ufw allow from your address, but a home IP changes and the rule stays behind. An SSH tunnel or a VPN is safer: the database stays on localhost and you connect through an encrypted channel, with the same key you already use for SSH.
My provider has a firewall, do I still need one on the server?
Yes. The firewall in the provider's panel filters before the machine and is a useful first layer. The one on the server knows what actually listens, also protects traffic on the private network and stays with you if you move the server. Keep both consistent, with the same list of ports.