Deploying a C# Application on VPS: A Step-by-Step Guide from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why a VPS is the Ideal Place for Your C# Application

\n

Developing 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\n

Server Preparation and Installing Required Software

\n

1. Connecting to the VPS and Basic Setup

\n

After gaining access to the VPS (IP address, login and password, or SSH key), connect via terminal:

\n
ssh root@your_server_IP
\n

Update the package database and install curl (needed for downloading .NET):

\n
apt update && apt upgrade -y\napt install curl -y
\n\n

2. Installing .NET SDK/Runtime

\n

Microsoft 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:

\n
wget 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
\n

Verify the installation:

\n
dotnet --version
\n\n

3. Installing Nginx and Git

\n

Nginx will act as a reverse proxy, forwarding requests from port 80 to your application. Git will be needed for cloning the repository:

\n
apt install -y nginx git\nsystemctl enable nginx\nsystemctl start nginx
\n\n

Preparing and Publishing the C# Application

\n

1. Creating or Cloning the Project

\n

For 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):

\n
mkdir /var/www/myapp\ncd /var/www/myapp\ndotnet new webapi --name MyApp\ncd MyApp
\n\n

2. Publishing the Application (self-contained)

\n

To make the application work without the SDK installed on the server, let's publish it in self-contained mode:

\n
dotnet publish -c Release -r linux-x64 --self-contained true -o /var/www/myapp/publish
\n

The --self-contained true flag includes all .NET dependencies in the publish folder. The alternative is --self-contained false (requires the Runtime to be installed).

\n\n

3. Test Run

\n

Check that the application starts (by default on port 5000):

\n
cd /var/www/myapp/publish\n./MyApp --urls "http://0.0.0.0:5000"
\n

Open http://your_IP:5000 in your browser — you should see the API response. Stop the process (Ctrl+C).

\n\n

Configuring Nginx as a Reverse Proxy

\n

1. Site Configuration

\n

Let's create a configuration file for Nginx:

\n
nano /etc/nginx/sites-available/myapp
\n

Paste the following code (replace your_domain_or_ip with your domain or IP):

\n
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\n

2. Activating the Configuration

\n
ln -s /etc/nginx/sites-available/myapp /etc/nginx                

Blogs

Book Recommendations