I find myself re-learning how to set up a VPS a few times a year, so I decided to write down my latest process here. No idea if it’s useful for others, but it will at least be useful to me a couple months from now!
Setup will be:
- Debian 13 Trixie,
- running some web service with
systemd, - built & deployed from my local machine
Not yet included here, but I often also:
- add a reverse proxy (I usually use Caddy: Debian install instructions)
- write a GitHub deploy action, so I (or an agent) can push from my repo
- Warning: since I use passwordless sudo for convenience below, give the GH Action an SSH key for a separate deploy user, so it doesn’t get root.
Overview
My general process is:
- Write a base cloud config (also known as “cloud init” and “user data”), a declarative server setup supported by most providers
- Write a
systemdunit file - Build my web service
rsyncit and all accompanying files to the server- Start it (and set it to start after reboot)
- When something changes: rebuild, resync, and reload the service
Cloud Config
Pros: more declarative than a setup script. Cons: harder to debug than a setup script.
Replace my_user, my_server_user, github_username, and 12345 with a random high port number. You can skip ufw/firewall if using your provider’s, e.g. Hetzner’s free firewall. (Warning: Docker circumvents ufw!) I set my_user to be the same as my local user for convenience, so I can just run ssh -p12345 ip.
I usually keep a copy in the project’s repo (but make sure to hide your port number if public!).
#cloud-config
users:
- name: my_user
groups: users
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_import_id: ["gh:github_username"] # Fetches github.com/github_username.keys
ssh_authorized_keys: ["ssh-ed25519 AAAA..."] # Backup if GitHub is down (too frequent nowadays)
- name: my_server_user
system: true
shell: /usr/sbin/nologin
package_update: true
package_upgrade: true
packages:
- fail2ban
- ufw
- unattended-upgrades
- rsync
write_files:
# Reboot for kernel upgrades, etc.
- path: /etc/apt/apt.conf.d/51unattended-upgrades-local
content: |
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00"; // UTC
- path: /etc/ssh/sshd_config.d/00-ssh-hardening.conf
content: |
PermitRootLogin no
PasswordAuthentication no
Port 12345
KbdInteractiveAuthentication no
MaxAuthTries 4
AllowTcpForwarding yes
X11Forwarding no
AllowAgentForwarding no
AllowUsers my_user
runcmd:
# Server directory, accessible by my_user
- install -d -o my_server_user -g my_server_user -m 2770 /opt/my_server
- usermod -aG my_server_user my_user
# Fail2ban (ban IPs with repeated failed SSH logins)
- printf "[sshd]\nenabled = true\nport = 12345\nbackend = systemd\n" > /etc/fail2ban/jail.local
- systemctl enable fail2ban
# Firewall
- ufw allow 80/tcp # HTTP
- ufw allow 443 # HTTPS (with UDP for HTTP/3)
- ufw allow 12345/tcp # SSH
- ufw --force enable
# Restarts after cloud-config finishes
power_state:
mode: reboot
At first, your server will be accessible based on your provider’s configuration (e.g. port 22, with an SSH key you gave). (Note: apparently this is provider-dependent, but this has been my experience.) If you SSH in, you can watch the cloud config logs go by with:
sudo tail -f /var/log/cloud-init-output.log
After cloud-config finishes (a couple minutes), it will be accessible at your high port number (with your GitHub keys, and assuming you set my_user to your local username):
ssh -p12345 ip_address
Running with systemd
Example file that:
- Runs your server binary at
/opt/my_server/bin, - Loads environment variables from
/opt/my_server/prod.env,- I recommend creating a tracked
.env.example, and.envandprod.envfor your local/deployed instances (untracked if you have secrets!)
- I recommend creating a tracked
- and generally sandboxes it to only have access to its folder
[Unit]
Description=My Server
After=network.target network-online.target
Wants=network-online.target
[Service]
Type=exec
User=my_server_user
Group=my_server_user
ExecStart=/opt/my_server/bin
Restart=on-failure
RestartSec=2
TimeoutStopSec=5s
LimitNOFILE=1048576
EnvironmentFile=/opt/my_server/prod.env
WorkingDirectory=/opt/my_server
# Allow binding to ports below 1024 (80, 443)
AmbientCapabilities=CAP_NET_BIND_SERVICE
# Sandboxing
PrivateTmp=true
ProtectSystem=strict
ReadWritePaths=/opt/my_server
ProtectHome=true
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
Copying & Running
Commands below use a variable for your VPS’ IP:
export VPS=198.51.100.1
Copy your service files:
rsync -tvz -e "ssh -p12345" ./bin ./prod.env $VPS:/opt/my_server/
rsync -tvz -e "ssh -p12345" --rsync-path="sudo rsync" ./my_server.service $VPS:/etc/systemd/system/
Then to “enable” (start at boot) and run your service:
ssh -p12345 $VPS "sudo systemctl enable --now my_server" # --now starts service
To check status & logs:
ssh -p12345 $VPS "sudo systemctl status my_server"
ssh -t -p12345 $VPS "sudo journalctl -u my_server" # all logs (-t for paging)
ssh -t -p12345 $VPS "sudo journalctl -u my_server -f" # follow live
To stop (& start again):
ssh -p12345 $VPS "sudo systemctl stop my_server"
ssh -p12345 $VPS "sudo systemctl start my_server"
After you edit something (make sure to rsync!):
ssh -p12345 $VPS "sudo systemctl daemon-reload && sudo systemctl restart my_server"
Debates
Non-default SSH port number: anyone can still find your SSH port, of course, but this avoids a lot of extra traffic.
Automatic reboots for kernel upgrades: downtime traded off for a bit of hardening in these unprecedented times.