Introduction
Developing a C# application on the .NET platform is only half the battle. To make your project accessible to users on the internet, it must be properly deployed on a server. VPS (Virtual Private Server) is an ideal solution for small and medium businesses and indie developers: it provides full control over the environment at an affordable price. In this article, we will cover the complete process of deploying a C# application on a VPS running Linux (Ubuntu 22.04). You will learn how to prepare the server, publish the application, configure a reverse proxy with Nginx, and ensure automatic startup via systemd.
1. Preparing the VPS and Installing .NET Runtime
1.1. Connecting to the Server
After purchasing a VPS, you will receive an IP address and a root password. Connect via SSH:
ssh root@your-server-ipIt is recommended to immediately create a separate user for security:
adduser appuserusermod -aG sudo appusersu - appuser1.2. Installing .NET Runtime
For a C# application to run on the server, the ASP.NET Core Runtime is needed. Add the Microsoft repository and install the package:
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-get updatesudo apt-get install -y aspnetcore-runtime-8.0Verify the installation:
dotnet --list-runtimesYou should see a line with the ASP.NET Core 8.0.0 version.
2. Publishing the Application and Uploading to the Server
2.1. Publishing on the Local Machine
On your computer with Visual Studio or .NET CLI, open the project and execute the publish command:
dotnet publish -c Release -o ./publishThis command creates a publish folder containing all the files necessary to run the application (including runtime dependencies if you are using FDD — Framework-Dependent Deployment).
2.2. Uploading to the VPS
Copy the publish folder to the server using scp or sftp. On the local machine, execute:
scp -r ./publish appuser@your-server-ip:/home/appuser/myappNow, on the server, verify that the files are in place:
ls -la /home/appuser/myapp3. Configuring systemd for Automatic Startup
To ensure the application starts at system boot and automatically restarts after failures, create a systemd unit.
3.1. Creating the Service File
Create the file /etc/systemd/system/myapp.service with root privileges:
sudo nano /etc/systemd/system/myapp.serviceInsert the following content:
[Unit]Description=My ASP.NET Core ApplicationAfter=network.target
[Service]WorkingDirectory=/home/appuser/myappExecStart=/usr/bin/dotnet /home/appuser/myapp/YourAppName.dllRestart=alwaysRestartSec=10KillSignal=SIGINTSyslogIdentifier=myappUser=appuserEnvironment=ASPNETCORE_ENVIRONMENT=ProductionEnvironment=ASPNETCORE_URLS=http://localhost:5000
[Install]WantedBy=multi-user.targetReplace YourAppName.dll with the actual name of your .dll file (usually matches the project name).
3.2. Starting and Enabling the Service
sudo systemctl daemon-reloadsudo systemctl start myappsudo systemctl enable myappCheck the status:
sudo systemctl status myappIf everything is configured correctly, you will see the status active (running). The application is now listening on port 5000 on the local interface.
4. Configuring Nginx as a Reverse Proxy
To make the application accessible from the outside on standard ports (80/443) and to add an SSL certificate, install and configure Nginx.
4.1. Installing Nginx
sudo apt updatesudo apt install nginx -y