
Image retained from the original article.
Originally published 21 June 2023; rewritten and reviewed on 15 October 2026.
Running dotnet run over SSH and watching Kestrel start listening on port 5000 is a great feeling the first time you move a .NET app onto an Ubuntu VPS. Then you close your terminal—or the server reboots after a kernel update—and the site goes down.
dotnet run is a development command. It compiles on the fly, holds onto your SSH session, and runs under your personal login user. Turning that into a real deployment takes three things: a clean dotnet publish artifact, a dedicated system user, and a systemd service behind a reverse proxy.
Publish properly instead of running source on the server
Build and publish in Release mode so you aren’t shipping .cs source files or running Debug builds in production. You have two main choices:
- Framework-dependent (smaller output, ~5–20 MB): requires the matching .NET Runtime (
aspnetcore-runtime-8.0, for example) installed on the Linux host. - Self-contained (larger output, ~70–100+ MB): bundles the .NET runtime with your app, so the server doesn’t need
.NETinstalled globally, though you still need native OS libraries likelibicuandopenssl.
For a typical framework-dependent web app:
dotnet restore
dotnet publish -c Release -o ./publish
Copy the contents of ./publish to /var/www/myapp on the server (using rsync, scp, or your CI pipeline). Never deploy straight into /root or /home/ubuntu—keep web apps in a predictable directory like /var/www/<appname>.
Create a service user and a systemd unit
Don’t run your web application as root. Create a dedicated, non-login system account that only owns what the app needs:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin appuser
sudo chown -R appuser:appuser /var/www/myapp
Next, keep secrets (database connection strings, API keys, JWT signing keys) out of appsettings.json inside /var/www/myapp. Put them in an environment file owned by root with 0640 permissions so appuser can read it via systemd without other users on the box seeing it:
# /etc/myapp/myapp.env
ASPNETCORE_ENVIRONMENT=Production
ASPNETCORE_URLS=http://127.0.0.1:5000
ConnectionStrings__Default=Host=127.0.0.1;Database=myapp;Username=myapp;Password=strong-password
Now create /etc/systemd/system/myapp.service:
[Unit]
Description=MyApp ASP.NET Core Service
After=network.target
[Service]
WorkingDirectory=/var/www/myapp
ExecStart=/usr/bin/dotnet /var/www/myapp/MyApp.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=myapp
User=appuser
EnvironmentFile=/etc/myapp/myapp.env
[Install]
WantedBy=multi-user.target
Enable and start it, then watch the logs immediately to confirm it bound to 127.0.0.1:5000 cleanly:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
sudo systemctl status myapp
sudo journalctl -u myapp -f
Put Nginx in front of Kestrel
Binding Kestrel to 127.0.0.1:5000 keeps it off the public internet so Nginx (or Caddy, or Cloudflare Tunnel) handles TLS termination, compression, and static caching:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Remember to call app.UseForwardedHeaders(...) in Program.cs so ASP.NET Core respects X-Forwarded-Proto and X-Forwarded-For from 127.0.0.1. Otherwise, redirect URLs, OAuth callbacks, and client IP logging will all see http:// and 127.0.0.1.
Verify the failure and reboot path
Before calling the setup done, test the boring operational cases: run sudo systemctl restart myapp, kill the dotnet PID manually to verify Restart=always brings it back within 10 seconds, and reboot the VPS once if it isn’t in production yet. A deployment isn’t finished when the app starts—it’s finished when it recovers on its own at 3 a.m.