Introduction: Why a VPS is the Ideal Place for Your C# Application
\nDeveloping in C# within the .NET ecosystem provides powerful tools for creating web applications, APIs, and microservices. However, writing the code is only half the battle. To make your application accessible to users, it needs to be deployed on a server. A Virtual Private Server (VPS) provides full control over the environment, which is critical for a production environment. In this guide, we will walk through the journey from zero to a fully functioning C# application on a VPS running Ubuntu. You will learn how to install the .NET Runtime, configure the Nginx reverse proxy server, run the application as a systemd service, and ensure its automatic restart.
\n\nServer Preparation and Installing Required Software
\n1. Connecting to the VPS and Basic Setup
\nAfter gaining access to the VPS (IP address, login and password, or SSH key), connect via terminal:
\nssh root@your_server_IP\nUpdate the package database and install curl (needed for downloading .NET):
\napt update && apt upgrade -y\napt install curl -y\n\n2. Installing .NET SDK/Runtime
\nMicrosoft recommends installing .NET via the official repository. We will add it and install the SDK (for building) or Runtime (for running only). For a production server, the Runtime is usually sufficient, but for deployment convenience, we will install the SDK:
\nwget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb\ndpkg -i packages-microsoft-prod.deb\napt update\napt install -y dotnet-sdk-8.0\nVerify the installation:
\ndotnet --version\n\n3. Installing Nginx and Git
\nNginx will act as a reverse proxy, forwarding requests from port 80 to your application. Git will be needed for cloning the repository:
\napt install -y nginx git\nsystemctl enable nginx\nsystemctl start nginx\n\nPreparing and Publishing the C# Application
\n1. Creating or Cloning the Project
\nFor example, let's create a simple ASP.NET Core Web API application directly on the server (in a real project, you would clone a repository):
\nmkdir /var/www/myapp\ncd /var/www/myapp\ndotnet new webapi --name MyApp\ncd MyApp\n\n2. Publishing the Application (self-contained)
\nTo make the application work without the SDK installed on the server, let's publish it in self-contained mode:
\ndotnet publish -c Release -r linux-x64 --self-contained true -o /var/www/myapp/publish\nThe --self-contained true flag includes all .NET dependencies in the publish folder. The alternative is --self-contained false (requires the Runtime to be installed).
3. Test Run
\nCheck that the application starts (by default on port 5000):
\ncd /var/www/myapp/publish\n./MyApp --urls "http://0.0.0.0:5000"\nOpen http://your_IP:5000 in your browser — you should see the API response. Stop the process (Ctrl+C).
Configuring Nginx as a Reverse Proxy
\n1. Site Configuration
\nLet's create a configuration file for Nginx:
\nnano /etc/nginx/sites-available/myapp\nPaste the following code (replace your_domain_or_ip with your domain or IP):
server {\n listen 80;\n server_name your_domain_or_ip;\n\n location / {\n proxy_pass http://localhost:5000;\n proxy_http_version 1.1;\n proxy_set_header Upgrade $http_upgrade;\n proxy_set_header Connection keep-alive;\n proxy_set_header Host $host;\n proxy_cache_bypass $http_upgrade;\n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n proxy_set_header X-Forwarded-Proto $scheme;\n }\n}\n\n2. Activating the Configuration
\nln -s /etc/nginx/sites-available/myapp /etc/nginx