Advanced Deployment of C# (.NET) Application on VPS: Step-by-Step Guide
Deploying a C# application on a VPS is not just about copying files to the server. For a production environment, comprehensive configuration is required: from reverse proxy setup to deployment automation. In this article, we will cover advanced deployment techniques used by experienced developers. You will learn how to configure Nginx, set up HTTPS, implement CI/CD, and monitoring.
1. Server Preparation and .NET Runtime Installation
Before starting the deployment, ensure your VPS is running Ubuntu 22.04 or newer. Connect via SSH and perform basic setup.
# System updatesudo apt update && sudo apt upgrade -y
# Installing .NET SDK (for building) and Runtime (for running)wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.shchmod +x dotnet-install.shsudo ./dotnet-install.sh --channel 8.0 --install-dir /usr/share/dotnetsudo ln -s /usr/share/dotnet/dotnet /usr/local/bin/dotnet
# Verify installationdotnet --list-runtimesFor production, it is recommended to use only the Runtime to reduce the image size. The SDK is only needed on the build machine (e.g., in CI/CD).
2. Optimizing the Application for Production
Before deployment, configure the application to work in a production environment. This includes Kestrel configuration, logging, and error handling.
2.1. Configuring Program.cs
In the Program.cs file, specify an explicit port and enable HTTPS redirection (if using a reverse proxy).
var builder = WebApplication.CreateBuilder(args);
// Configure Kestrel to work behind a reverse proxybuilder.WebHost.ConfigureKestrel(options =>{ options.Listen(IPAddress.Any, 5000); // HTTP port for Nginx});
// Add servicesbuilder.Services.AddControllersWithViews();builder.Services.AddHealthChecks();
var app = builder.Build();
// Middleware for productionif (!app.Environment.IsDevelopment()){ app.UseExceptionHandler("/Home/Error"); app.UseHsts();}
app.UseStaticFiles();app.UseRouting();app.UseAuthorization();
app.MapHealthChecks("/health");app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();2.2. Building and Publishing
Build the application as self-contained to avoid depending on the Runtime installed on the server.
dotnet publish -c Release -r linux-x64 --self-contained true -o ./publishThis command will create a publish folder with all the necessary files. Transfer it to the server using scp or rsync.
3. Configuring Nginx as a Reverse Proxy
Nginx will accept incoming requests and forward them to your C# application. This improves performance and security.
3.1. Installation and Basic Configuration
sudo apt install nginx -ysudo systemctl start nginxsudo systemctl enable nginxCreate a configuration file for your application:
sudo nano /etc/nginx/sites-available/myappPaste the following code:
server { listen 80; server_name your-domain.com www.your-domain.com;
location / { proxy_pass http://localhost: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; }
location /health { access_log off; return 200 "Healthy"; add_header Content-Type text/plain; }}Activate the site and restart Nginx:
<