Deploying to a small shared VPS
Lessons from running many small apps on one cheap VPS with coding agents doing the deploys. Each item is a trap that cost time once. If the user keeps a private server guide (host alias, app list, ports, domains), read it first: it overrides the placeholders here. Placeholders below: <alias> is the SSH alias, <agent> the agent's Linux user, <app> the app name, <port> its local port, <sub>.example.com its address.
1. Access
- Connect through an alias in
~/.ssh/config, so commands never carry an IP address, user or key path:
ThenHost <alias> HostName <server address> User <agent> IdentityFile ~/.ssh/<agent-key> IdentitiesOnly yesssh <alias>for a shell,ssh <alias> '<command>'for one command. - Give agents their own Linux account and their own key, separate from the owner's login and from root. Each agent platform (local laptop, a cloud agent) gets its own key pair, with a comment naming it at the end of its line in
~/.ssh/authorized_keys. To cut one off, delete that line. - Never print, copy or commit a private key. Keys for cloud agents go into that platform's secret store.
- Do not use the owner's own accounts or root unless the owner says so.
- Disable password login once key login works: a file in
/etc/ssh/sshd_config.d/withPasswordAuthentication noandKbdInteractiveAuthentication no. Runsudo sshd -t, reload ssh, then open a second session with the key before closing the first. Keep the provider's web console as the way back in.
2. What the agent account may do
Decide this once and write it in the private guide. A workable setup:
- passwordless
sudo(sudo -nworks), membership in thedockergroup, and its own home with~/apps/<app>per app; - everything outside home (
/etc/nginx,/etc/systemd/system,/var/www,/opt) needssudo; - tools installed once: git, Node with pnpm, Python with uv, Docker, nginx, certbot, tmux.
Allowed without asking: read anything, deploy or redeploy the app it was asked about, read logs. Ask first: anything that touches another app, a reboot, apt upgrade, docker system prune, firewall changes, DNS changes, copying secrets, a project's first public launch.
3. Look before you change
The server changes under you: other agents deploy, ports fill up, disk runs low. Check live state, not notes:
ssh <alias> 'ls ~/apps; sudo ss -ltnp | grep LISTEN; free -h; df -h /'
ssh <alias> 'systemctl list-units --type=service --state=running --no-pager'
ssh <alias> 'docker ps'
ssh <alias> 'ls /etc/nginx/sites-enabled'
Pick a free port from ss -ltnp, not from a list in a file.
4. Deploy
Build on the laptop, not the server. A small VPS is shared and slow; ship the build output.
Ship tracked files only (no .git, node_modules or local .env):
git ls-files -z | rsync -az --from0 --files-from=- ./ <alias>:apps/<app>/
rsync -az dist/ <alias>:apps/<app>/dist/ # build output, if it is not tracked
Secrets go in ~/apps/<app>/.env on the server, created there, chmod 600. Never in the repo, never in the unit file, never echoed in a command that lands in shell history.
Static site: rsync the build to /var/www/<sub> (or the app folder) and point an nginx root at it. No service needed.
App as a systemd service: /etc/systemd/system/<app>.service
[Unit]
Description=<app>
After=network-online.target
Wants=network-online.target
[Service]
User=<agent>
WorkingDirectory=/home/<agent>/apps/<app>
EnvironmentFile=/home/<agent>/apps/<app>/.env
ExecStart=<absolute path to the start command>
Restart=always
RestartSec=3
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
Then sudo systemctl daemon-reload && sudo systemctl enable --now <app>. After a redeploy, sudo systemctl restart <app>.
App in Docker: keep a docker-compose.yml per app in its folder. Publish ports as 127.0.0.1:<port>:<container port>, never bare <port>:<port>. After shipping new code, docker compose up -d --build --force-recreate; without --force-recreate the old container can keep running the old code.
Unattended jobs (watchers, scheduled scripts) run on the server as a systemd service or timer, not on a laptop that sleeps.
5. Web address: nginx and certbot
- One file per site:
/etc/nginx/sites-available/<sub>.example.com.conf, symlinked intosites-enabled. Copy a working site's file as the template. - Order for a new subdomain:
- a port-80 block only, serving
/.well-known/acme-challenge/from/var/www/certbot; sudo nginx -t && sudo systemctl reload nginx;sudo certbot certonly --webroot -w /var/www/certbot -d <sub>.example.com;- add the 443 block with the certificate paths and
proxy_pass http://127.0.0.1:<port>;(orrootfor a static site), plus a redirect from 80 to 443; nginx -tand reload again.
- a port-80 block only, serving
- Always
nginx -tbefore a reload, and reload, never restart. One broken file plus a restart takes every site on the server down. - certbot installs a renewal timer; check it with
systemctl list-timers | grep certbot. - A script that regenerates a site's config overwrites manual additions (an analytics include, extra headers). Re-add them after running it, or teach the script about them.
6. Firewall and binding
- ufw allows only 22, 80 and 443 from outside:
sudo ufw status verbose. - Apps listen on
127.0.0.1:<port>; nginx is the only public entry. - Docker publishes ports through its own iptables rules and bypasses ufw. A container started with
-p 8080:8080is open to the internet whatever ufw says. Bind to127.0.0.1. - Databases (Postgres, Redis) bind to localhost or live only on a Docker network.
- fail2ban is optional once password login is off; the bigger win is key-only SSH.
7. DNS
- A wildcard record (
*.example.com→ the server) means a new subdomain needs no DNS change, only nginx and a certificate. - Without one, add an A (and AAAA if the server has IPv6) record per subdomain. Use the DNS provider's API or CLI rather than handing the owner console steps, after the owner says yes.
- If the DNS provider proxies traffic (Cloudflare's orange cloud), certbot's HTTP challenge and real client IPs behave differently; leave records unproxied unless the setup expects the proxy.
- If the domain has DMARC set to
quarantineorreject, an app that sends mail as@example.comfrom the server needs its sender added to SPF (and DKIM), or its mail lands in spam. - Comment each record with what it is for and the date, so the next agent knows what is safe to touch.
8. Logs and long jobs
ssh <alias> 'journalctl -u <app> -n 100 --no-pager' # service logs
ssh <alias> 'docker compose -f ~/apps/<app>/docker-compose.yml logs --tail 100'
ssh <alias> 'sudo tail -n 50 /var/log/nginx/error.log' # proxy errors, 502s
- A dropped SSH connection kills a foreground command. Anything longer than a minute (tests, a migration, a benchmark, a large download) runs detached with a log, and you poll the log:
ssh <alias> 'mkdir -p ~/logs; nohup <command> > ~/logs/<name>.log 2>&1 < /dev/null & echo $!' ssh <alias> 'tail -n 20 ~/logs/<name>.log'tmux new -d -s <name> '<command>'also works. - Before stopping a live app to measure or migrate, say so and say how long it will be down.
9. Self-hosted analytics
- One analytics instance (Umami, Plausible CE, GoatCounter) in Docker serves every site on the server. Bind it to localhost and give it its own subdomain for the dashboard.
- Serve the tracker script and its collect endpoint first-party: an nginx
location /<path>/in each site's 443 block that proxies to the analytics container, kept in a shared snippet and pulled in withinclude snippets/<name>.conf;. Ad blockers then do not single it out. - Agents usually lack the dashboard password. Read numbers from the analytics database directly (
docker compose exec -T db psql ...) rather than asking for it. - Record each site's tracking id somewhere on the server, and say in the privacy page what is collected.
10. Cloud API versus SSH
- The provider's API or CLI (
hcloud,doctl,linode-cli) manages the account: servers, sizes, volumes, firewalls, snapshots, billing. Deploying needs none of it. - Its token is usually read-write for the whole account and can delete the server. Keep it in a keychain or secret store behind a wrapper, never in a command, environment file or repo.
- Read-only calls (
list,describe) are fine. Anything that creates, resizes, powers off, rebuilds or deletes, or adds cost, needs the owner's yes in the current session, with the exact command and the monthly price shown first. - Before detaching or deleting a volume, check
/etc/fstaband any systemd unit that requires the mount. A missing mount listed in fstab withoutnofailcan stop the server from booting. - Some changes need the server powered off (changing the server type) or are only possible one way (a disk cannot shrink back after an upgrade with disk). Check the provider's docs before running them.
- Popular server types sell out per location. Check stock in each nearby location before promising a size.
11. Shared-server etiquette
- Touch only the app you were asked to work on. Do not stop, restart, edit or delete other services, containers, sites or files under
/optand/var/www, even if they look abandoned. Other Linux users may own them. - No global changes without asking: package upgrades, kernel or reboot, Docker prune, firewall, sshd, nginx main config.
- When you add an app, site, port or timer, update the owner's private server guide in the same piece of work.
- Before a redeploy, the change has been reviewed. After it, test the live URL end to end with realistic requests, not only
systemctl statusandcurl -I. When the change was about speed or cost, report the numbers before and after. Deploy to every place the project is served.
12. Traps hit in practice
- Non-interactive SSH has a short PATH.
ssh <alias> 'uv run ...'fails whenuvlives in~/.local/bin; call tools by absolute path in one-off commands and in unit files. - sshd drop-in files: the first value wins. A
PasswordAuthentication noin a later-sorting file is ignored if an earlier one (often a cloud-init file like50-cloud-init.conf) saysyes. Name yours with a low number (10-key-only.conf) and confirm withsudo sshd -T | grep -i passwordauth. .envfiles created by rsync or an editor default to 644.chmod 600them, and check withfind ~/apps -name .env -perm -o=r.- A container keeps old code after
docker compose up -dif the image tag did not change. Use--build --force-recreate. - Restarting nginx with one bad file takes every site down.
nginx -t, then reload. - Moving data off a volume: copy with
rsync -aHAX, verify withrsync -naic --checksum(no output means identical), comment out the fstab line, reboot once to prove it boots, then delete the volume. - Long benchmarks over SSH die with the connection. Detach them (section 8).
- App code that is fine on a laptop breaks under real concurrency. For example a SQLite connection shared across worker threads fails intermittently. Load-test the deployed app, not only the local one.
- A deprecated provider CLI command can still print data but warn on stderr; read the warning and use the replacement before scripting it.