---
title: "VPS Setup"
description: "Sane defaults for locking down a fresh Debian/Ubuntu VPS: SSH keys, UFW, Tailscale, and Caddy."
---

> Documentation Index
> Fetch the complete documentation index at: https://codex.mirio.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# VPS Setup

A fresh VPS is wide open. These steps get it to a reasonable baseline: no
password logins, a tight firewall, and Tailscale for private access.

Tested on Debian 13 (trixie) and Ubuntu 26.04. Commands are shown with `sudo`;
if your provider logs you in as root, run them directly.

## Create a non-root user

SSH in using your provider's default username:

```bash
ssh <default-user>@your-server-ip
```

:::note
Default SSH usernames by provider:

| Provider      | Ubuntu              | Debian              |
| ------------- | ------------------- | ------------------- |
| AWS           | `ubuntu`            | `admin`             |
| GCP           | Google account name | Google account name |
| Azure         | `azureuser`         | `azureuser`         |
| Hetzner       | `root`              | `root`              |
| Digital Ocean | `root`              | `root`              |

:::

Create a `deploy` user and grant it sudo access:

```bash
sudo useradd -m -s /bin/bash deploy
sudo usermod -aG sudo deploy
sudo passwd deploy
```

Copy your SSH public key to the new user:

```bash
sudo mkdir -p /home/deploy/.ssh
sudo cp ~/.ssh/authorized_keys /home/deploy/.ssh/
sudo chown -R deploy:deploy /home/deploy/.ssh
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys
```

Verify you can log in as the new user before continuing:

```bash
ssh deploy@your-server-ip
```

From here on, use `deploy` rather than root.

## System updates

Apply pending security patches:

```bash
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl ufw
```

Enable automatic security updates:

```bash
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
```

## Swap

Many small VPS instances ship with no swap. Without it, the OOM killer will
start terminating processes silently when memory is exhausted. A 2 GB swapfile
is a cheap safety net:

```bash
sudo fallocate -l 2G /swapfile
```

:::note
On BTRFS filesystems, `fallocate` does not create a valid swapfile. Use `sudo dd if=/dev/zero
of=/swapfile bs=1M count=2048` instead.
:::

```bash
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
```

Verify:

```bash
free -h
```

:::tip
Reduce swappiness to keep the kernel from swapping too eagerly on a server:

```bash
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.d/99-swappiness.conf
sudo sysctl -p /etc/sysctl.d/99-swappiness.conf
```

:::

## SSH

### Harden `sshd_config`

Edit `/etc/ssh/sshd_config` and set:

```text
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
```

Reload:

```bash
sudo systemctl reload ssh
```

:::caution
Keep your current session open while you test login in a second terminal. If you lock yourself
out, you'll need console access through your VPS provider's dashboard.
:::

## Tailscale

Tailscale gives every machine a stable private IP and lets you scope firewall
rules to the `tailscale0` interface instead of managing per-IP allowlists.

```bash
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up
```

Follow the authentication URL printed to stdout. Once connected, your machine
appears at a stable `100.x.x.x` address in your tailnet.

:::note
After this point, SSH into the server using the Tailscale IP (`100.x.x.x`), not the public IP. The
public SSH port will be closed by UFW in the next step.
:::

## UFW

Reset any existing rules and apply a strict default:

```bash
sudo ufw reset
sudo ufw default deny incoming
sudo ufw default allow outgoing
# allow all traffic on the Tailscale interface
sudo ufw allow in on tailscale0
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
```

Verify:

```bash
sudo ufw status verbose
```

Expected output:

```text
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)

To                         Action      From
--                         ------      ----
Anywhere on tailscale0     ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
443/tcp                    ALLOW IN    Anywhere
```

:::note
Port 22 (SSH) is not opened in UFW because SSH over Tailscale (`tailscale0`) is already allowed.
If you need public SSH access, add `sudo ufw allow ssh` before enabling UFW.
:::

## fail2ban

fail2ban watches logs and temporarily bans IPs that repeatedly fail
authentication. With key-only SSH this matters less for port 22, but it still
catches bots hammering public HTTP endpoints:

```bash
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
```

The defaults are reasonable out of the box. Verify it's watching SSH:

```bash
sudo fail2ban-client status sshd
```

## Caddy

Caddy is a reverse proxy that provisions and renews TLS certificates
automatically via Let's Encrypt: no certbot, no cron jobs.

### Install

Add the official Caddy apt repository and install:

```bash
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
  | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
  | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update && sudo apt install -y caddy
```

### Caddyfile

The config lives at `/etc/caddy/Caddyfile`. Replace the default contents:

```caddy
app.example.com {
    reverse_proxy localhost:8080
}
```

Multiple apps on the same server:

```caddy
app.example.com {
    reverse_proxy localhost:8080
}

api.example.com {
    reverse_proxy localhost:3000
}
```

Path-based routing to different backends:

```caddy
example.com {
    reverse_proxy /api/* localhost:3000
    reverse_proxy * localhost:8080
}
```

Apply the config:

```bash
caddy fmt --overwrite /etc/caddy/Caddyfile
sudo systemctl reload caddy
```

### Logs

```bash
sudo journalctl -u caddy -f
```

Certificate errors, upstream connection failures, and access logs all appear
here. Most TLS issues are DNS-related: Caddy provisions certificates on first
request, so the domain must resolve to the server's public IP before that
request arrives.

:::tip
Validate config changes before reloading the live service:

```bash
caddy validate --config /etc/caddy/Caddyfile
```

:::

## Running your application

### Systemd unit file

Create `/etc/systemd/system/myapp.service`:

```ini
[Unit]
Description=My App
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/myapp
ExecStart=/home/deploy/myapp/server
EnvironmentFile=-/home/deploy/myapp/.env
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
```

The `-` prefix on `EnvironmentFile` means systemd will not fail if the file
does not exist. `Restart=on-failure` restarts the process only on non-zero
exit; `RestartSec=5` adds a delay to prevent tight crash loops.

Enable and start in one step:

```bash
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
```

`enable` registers the unit to start at boot. `--now` starts it immediately.

### Logs

```bash
sudo journalctl -u myapp -f
```

stdout and stderr both land here. The journal persists across restarts, so
you can inspect logs from a previous crash:

```bash
sudo journalctl -u myapp --since "1 hour ago"
```

### Deploying updates

Copy the new binary to a temporary path and `mv` it into place, then restart.
Writing directly over a running executable fails with `ETXTBSY` (text file
busy); `mv` sidesteps this by replacing the directory entry, while the running
process keeps executing the old inode until the restart:

```bash
scp ./server deploy@host:/home/deploy/myapp/server.new
ssh deploy@host 'mv /home/deploy/myapp/server.new /home/deploy/myapp/server && sudo systemctl restart myapp'
```

`mv` on the same filesystem is atomic: there is no window where a partial write
can be executed. The service will be briefly unavailable during the restart.

### Common commands

| Command                   | Effect                                   |
| ------------------------- | ---------------------------------------- |
| `systemctl start myapp`   | Start the service                        |
| `systemctl stop myapp`    | Stop the service                         |
| `systemctl restart myapp` | Stop then start                          |
| `systemctl status myapp`  | Current state and last few log lines     |
| `systemctl reload myapp`  | Send SIGHUP (only if the app handles it) |
| `journalctl -u myapp -f`  | Tail logs live                           |

## Checklist

- [ ] Non-root sudo user created, SSH key installed
- [ ] System packages updated, `unattended-upgrades` enabled
- [ ] Swap configured (2 GB swapfile, swappiness set to 10)
- [ ] `PermitRootLogin no` and `PasswordAuthentication no` in `sshd_config`
- [ ] Tailscale installed, SSHing via `100.x.x.x` from here on
- [ ] UFW active with default-deny incoming
- [ ] fail2ban enabled
- [ ] Caddy installed, Caddyfile configured, service running
- [ ] Application running as a systemd service, enabled at boot

Source: https://codex.mirio.dev/linux/vps-setup/index.mdx
