Knowledge blog

Deploying .Net Core apps in Linux  with Nginx reverse proxy.

As we know that .Net Core can be deployed into any platform including server less cloud platform here we are going to talk about deploying in Ubuntu Server with Nginx as reverse proxy. DotNet Core offers a light-weight self-hosting server called Kestrel which will run behind the Nginx to full fill our hosting requirement.

  1. Ubuntu Server
  2. Nginx Installed
Update the Microsoft package Repository
In Ubuntu 22 and higher you don’t need to import package repository as it is built-in. If you are in lower version of Ubuntu then you need to register and import the Microsoft package repository. Hence skip the step below if you are in Ubuntu 22 or higher.
Open your SSH terminal and perform below commands one by one.
# Get Ubuntu version
declare repo_version=$(if command -v lsb_release &> /dev/null; then lsb_release -r -s; else grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"'; fi)

# Download Microsoft signing key and repository
wget$repo_version/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

# Install Microsoft signing key and repository
sudo dpkg -i packages-microsoft-prod.deb

# Clean up
rm packages-microsoft-prod.deb 

Update your Repository

sudo apt update

Install .Net Core 8.0

sudo apt install aspnetcore-runtime-8.0

To Test your installation by running below command which should result the version information of .Net core runtime like below screen.

dotnet –info
Let’s create a .net core project, publish it and place it to web server
  1. Let’s create a WebAPI project using visual studio and publish it to a folder.
  2. Now create a folder in your Linux server for this app.
  3. Use filezilla or other FTP tools to move the published file to the linux directory you have just created.
  1. From the terminal navigate to the app folder.
cd /your/app/folder/
  1. Run the app on top of Kestrel web server using below command.
sudo dotnet YourAppName.dll

Now your app should be running at http://localhost: 5000. As Kestrel runs in foreground we need to keep the SSH session open. To avoid this we will need to register the Webapp as a service so it always runs in the background.

Kestrel is a lightweight cross-platform web server for ASP.NET core which is included by default on all ASP.NET core project templates.
Configure the ASP.NET Core Web App To Run as a Service:

To create the service, we first need to create the configurations in a systemd unit file. The unit file contains information regarding the unit, which is a service in this case. For services, it should have the .service extension and contain some information about the service. These files are required to be in the /etc/systemd/system directory.

Let’s create a service file called DotNetcore.service
nano /etc/systemd/system/ DotNetcore.service
Description=ASP.NET Core Web App Service on Ubuntu
WorkingDirectory= /var/www/Dotnet/webapi
ExecStart=/usr/bin/dotnet  /var/www/Dotnet/webapi /WebAPI.dll
# Restart service after 10 seconds if the dotnet service crashes:
# This user should exist on the server and have ownership of the deployment directory

save It.

Enable and start the new service using below commands.
sudo systemctl enable kestrel-app.service
sudo systemctl start kestrel-app.service
Configure Nginx to work as reverse proxy:
You can use IIS, Apache, Nginx or any other reverse proxy on top of .Net core runtime with the similar approach.

Assume, you already have installed and using Nginx if not just install it using sudo apt install nginx

Let’s edit the Nginx website configuration file. /etc/nginx/sites-available/default. I am opening using Notepad++ but you can simply use Nano to edit in terminal itself.

server {
 listen 80; 
 listen 443 ssl http2; 
 root /var/www/Dotnet/webapi; 
 ssl_certificate /etc/nginx/ssl/dd/demodooms.pem;
 ssl_certificate_key /etc/nginx/ssl/dd/demodooms.key;

    location / {
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;

Remember to change the highlighted values to yours. Also remove 443 and SSL if you only want to use http at port 80.

Now make a Nginx restart. sudo service nginx restart

Access your website and that should be running like a charm.

Scroll to Top