Introduction: Why VPS is the Ideal Solution for C# Applications?
Development in C# and the .NET platform has long ceased to be the exclusive prerogative of Windows servers. With the release of .NET Core (and now .NET 5/6/7/8), developers gained the ability to run their applications on Linux, which opened access to inexpensive and high-performance VPS servers. Deploying a C# application on a VPS is not only a budget-saving measure but also complete control over the infrastructure: from choosing the OS to configuring security.
In this guide, we will walk through the full deployment cycle of an ASP.NET Core application on a remote server. You will learn how to prepare the server, configure the Nginx reverse proxy server, launch the application via Kestrel, and secure it with an SSL certificate. All steps are accompanied by code examples and console commands.
1. Server Preparation and Installing .NET Runtime
Choosing an OS and Connecting via SSH
For deploying a C# application on a VPS, it is recommended to use Ubuntu 22.04 LTS or Debian 11. After renting the server, connect to it via SSH:
ssh root@your_server_IPUpdate the package manager and install the necessary utilities:
apt update && apt upgrade -yapt install curl wget git -yInstalling .NET SDK or Runtime
To run a ready-made application, it is enough to install the ASP.NET Core Runtime. If you plan to compile code directly on the server, install the SDK. Add the Microsoft repository and install .NET 8:
wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.debdpkg -i packages-microsoft-prod.debapt updateapt install aspnetcore-runtime-8.0 -yVerify the installation:
dotnet --list-runtimesThe output should contain a line with the ASP.NET Core 8.0.x version.
2. Preparing and Publishing the Application
Configuring the Project for Production
In your ASP.NET Core project, open the Program.cs file and ensure the application listens on the correct URL. For working via Kestrel, set port 5000:
var builder = WebApplication.CreateBuilder(args);builder.WebHost.UseUrls("http://0.0.0.0:5000");var app = builder.Build();app.MapGet("/", () => "Hello from VPS!");app.Run();Also, add a section for logs and database connection strings in appsettings.json if they are used.
Publishing the Application (Self-contained or Framework-dependent)
Choose a publishing method. Self-contained includes all dependencies, which is convenient if .NET Runtime is not installed on the server. Framework-dependent requires the Runtime to be installed. Run the command in the project folder:
dotnet publish -c Release -o ./publishAfter completion, all necessary files will appear in the publish folder. Archive them:
tar -czf app.tar.gz ./publishUploading to the Server
Use SCP or SFTP to transfer the archive. Example via SCP (run from the local machine):
scp app.tar.gz root@your_IP:/var/www/myapp/On the server, extract the archive:
mkdir -p /var/www/myapptar -xzf /var/www/myapp/app.tar.gz -C /var/www/myapp/3. Configuring Kestrel and Creating a systemd Service
Launching the Application via Kestrel
Kestrel is the built-in web server for ASP.NET Core. For a test run, execute:
cd /var/www/myapp/publishdotnet YourAppName.dllThe application should start on port 5000. Check it via curl http://localhost:5000. If everything works, stop the process (Ctrl+C).
Creating a systemd Unit File
To make the application start automatically at system boot and restart on failures, create a systemd service. Create the file:
nano /etc/systemd/syst