Getting a new VPS (from DigitalOcean, Hetzner, Linode, etc.) is exciting. But the moment that server goes online with a public IP, automated bots start scanning it for vulnerabilities. If you leave it with default settings or a weak root password, it's only a matter of hours before it gets compromised.
In this tutorial, I'll show you exactly how to secure a fresh Ubuntu/Debian VPS and install Docker so you can start deploying your apps safely.
1. Update Your System First
The very first thing you should do is patch any outdated software.
apt-get update -y && apt-get upgrade -y
2. Configure a Firewall (UFW)
You only want to expose the ports you actually need. For a web server, that's SSH (22), HTTP (80), and HTTPS (443).
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
3. Disable Password Authentication
This is crucial! Passwords can be brute-forced. SSH keys cannot.
First, make sure you've added your SSH public key to ~/.ssh/authorized_keys. Then, edit your SSH config:
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
systemctl restart sshd
4. Install Fail2Ban
Fail2Ban automatically monitors your logs and bans IP addresses that show malicious signs, like too many password failures.
apt-get install fail2ban -y
systemctl enable fail2ban
systemctl start fail2ban
5. Install Docker & Compose V2
Now that the server is secure, we can install Docker. Don't use the default apt install docker.io as it's usually outdated.
# Add Docker's official GPG key and repo, then:
apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
Want to skip the manual work?
If you deploy servers often, doing this manually every time is a headache.
I wrote the Ultimate VPS Secure Setup Kit — a one-click bash script that automates this entire process. It handles the firewall, SSH hardening, Fail2Ban, shared memory security, unattended upgrades, and installs the latest Docker Engine.
You can grab the script here for just a few bucks:
👉 Ultimate VPS Secure Setup Kit on Gumroad
Just upload it, run ./setup.sh, and your server is hardened and ready for production in under 3 minutes.
Happy coding! 🐉
Top comments (0)