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

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

Developing a C# application is only half the battle. To make your project accessible to users on the internet, it must be deployed on a server. A Virtual Private Server (VPS) is an ideal solution for hosting .NET applications, as it gives you full control over the environment and allows you to configure everything for specific needs.

In this article, we will walk through the complete process of deploying a C# application (ASP.NET Core) on a VPS running Linux (Ubuntu 22.04). You will learn how to prepare the server, install the necessary software, publish the application, and configure a web server to work with it. All steps are accompanied by code examples and commands.



1. Preparing the VPS and Installing .NET SDK

The first step is preparing the server. We will use Ubuntu 22.04 LTS, as it is one of the most popular and stable operating systems for servers. Connect to the VPS via SSH:

ssh user@your-server-ip

After logging in, update the package list and install the necessary dependencies:

sudo apt updatesudo apt upgrade -ysudo apt install wget apt-transport-https software-properties-common -y

Now add the Microsoft repository and install .NET SDK. For .NET 8, run:

wget https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb -O packages-microsoft-prod.debsudo dpkg -i packages-microsoft-prod.debsudo apt updatesudo apt install dotnet-sdk-8.0 -y

Verify the installation:

dotnet --version

If you see a version number (e.g., 8.0.100), everything is installed correctly.

Important: If your application uses a database (e.g., PostgreSQL or SQLite), install the corresponding package. For PostgreSQL:

sudo apt install postgresql postgresql-contrib -y


2. Publishing and Uploading the Application to the Server

Now let's prepare the application itself for deployment. On your local computer (where development is done), open a terminal in the project root and publish in Release mode:

dotnet publish -c Release -o ./publish

This command will create a publish folder with all the necessary files to run the application on the server. Now you need to upload this folder to the VPS. Use SCP or SFTP. Example with SCP:

scp -r ./publish user@your-server-ip:/home/user/myapp

Where myapp is the name of the folder where your application will be stored on the server.

After uploading, connect to the server and move the files to a more suitable location, for example, /var/www/myapp:

sudo mkdir -p /var/www/myappsudo cp -r /home/user/myapp/* /var/www/myapp/

Don't forget to set the correct permissions for the folder:

sudo chown -R www-data:www-data /var/www/myappsudo chmod -R 755 /var/www/myapp


3. Configuring systemd for Automatic Startup

To ensure your application starts automatically on system boot and restarts in case of a failure, we will create a systemd service. Create a configuration file:

sudo nano /etc/systemd/system/myapp.service

Insert the following content (replace the paths and username with your own):

[Unit]Description=My ASP.NET Core ApplicationAfter=network.target

[Service]WorkingDirectory=/var/www/myappExecStart=/usr/bin/dotnet /var/www/myapp/YourApp.dllRestart=alwaysRestartSec=10KillSignal=SIGINTSyslogIdentifier=myappUser=www-dataEnvironment=ASPNETCORE_ENVIRONMENT=ProductionEnvironment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]WantedBy=multi-user.target

Save the file (Ctrl+O, then Enter) and exit (Ctrl+X). Now reload systemd and start the service:

sudo systemctl daemon-reloadsudo systemctl start myappsudo systemctl enable myapp

Check the status:

Blogs

Book Recommendations