serverguy serverguy

How to Host ASP.NET Core on Linux with Nginx

Learn how to host your ASP.NET Core, Blazor, Dot Net on Linux with Nginx, a popular web server, using this easy step-by-step guide. Serverguy

Introduction

Microsoft’s .Net Core framework is a cross-platform framework, allowing us to host our .Net application on both Windows and Linux. Hosting ASP.NET Core on Linux with Nginx is a great way to take advantage of the performance and reliability benefits of Linux, while still being able to use the .NET Core framework. Nginx is a popular web server and reverse proxy that can be used to route requests to an ASP.NET Core application running on Linux. In this article, we’ll walk through the steps required to deploy an ASP.NET Core application on Linux with Nginx.

Prerequisites

  • A Linux server with ASP.NET Core installed
  • An ASP.NET Core application that you want to host
  • Nginx installed on the server

Install Required Packages

Install required .NET Module

The deployment of .NET applications in Linux involves running Apache or Nginx servers as a proxy servers to handle traffic from outside the machine and redirect it to the Kestrel server. We will use Nginx.

Add .Net Repository

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb

Run Update and install tool for HTTPS support

sudo apt update
sudo apt install -y apt-transport-https

Install .NET 6 SDK

sudo apt-get update && sudo apt-get install -y dotnet-sdk-6.0

Install .NET 6 Runtime

sudo apt-get install aspnetcore-runtime-6.0
sudo apt-get install dotnet-runtime-6.0

If your application is in .net 3. Then Uninstall SDK 6.0

sudo apt remove dotnet-sdk-6.0

Now Install SDK 3.0.

sudo apt install dotnet-sdk-3.1

Publish your .NET Application

Now that we have installed the dotnet required modules, we will need to publish our application. Create a .NET core application on Visual Studio by creating an MVC project or other kind of project, or opening an existing project.

  • Right Click on your project from the right side
  • Click on Publish
  • Now create a new publish profile, and browse the folder where you want to keep the project dll.
  • Click on publish so it will create your dll in the folder
  • Zip The Published folder As I named my one as TestAPI

Now transfer the zip file in your Linux server using filezilla or other FTP. Unzip it and keep in /var/www directory.

Create A Service File

Now that we have transfered our project and runtime is installed on the server, we will need to create a service file which will help the project to run on localhost:5000 port. And restart the process if the app is crashed or the server was rebooted. Create service file by running the following command:

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

You can choose any service file name, I have choosed testapi. Copy the following configuration in and paste it:

[Unit] 
Description= Test API
[Service] 
WorkingDirectory=/var/www/TestAPI
ExecStart=/usr/bin/dotnet /var/www/TestAPI/TestAPI.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=testapi
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

Now we will enable, start, restart the service file and check the status of the service file.

sudo systemctl enable testapi.service
sudo systemctl start testapi.service
sudo systemctl status testapi.service
sudo systemctl restart testapi.service

Install & Configure Nginx

So we have all the required .NET packages. Now we need to install the Nginx server. Install Nginx on your Linux server by running the following command:

sudo apt-get install nginx

Once Nginx is installed, you’ll need to configure it to route requests to your ASP.NET Core application. Create a new Nginx configuration file at /etc/nginx/sites-available/testapi (replace testapi with a name that makes sense for your application) with the following contents: I have shared nginx config with SSL if you don’t have SSL remove the 443 parts from the config file.

server {
        listen 443 ssl; # managed by Cloudflare
        ssl_certificate /home/serverguy/keys/ssl/testapi.pem; 
        ssl_certificate_key /home/serverguy/keys/ssl/testapi.key;

        server_name testapi.serverguy.dev www.testapi.serverguy.dev;

        add_header X-Frame-Options DENY;
        add_header X-Content-Type-Options nosniff;

location / {
		proxy_pass http://localhost:5000;
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection keep-alive;
		proxy_set_header Host $http_host;
		proxy_cache_bypass $http_upgrade;
	}
}
    server {
        if ($host = www.testapi.serverguy.dev) {
            return 301 https://$host$request_uri;
        } 

        if ($host = testapi.serverguy.dev) {
            return 301 https://$host$request_uri;
        } 
            listen 80;
            listen [::]:80;

            server_name testapi.serverguy.dev www.testapi.serverguy.dev;
        return 404; 
    }

Here is the HTTP Config without SSL.

server {
    listen        80;
    server_name   testapi.serverguy.dev;
    location / {
        proxy_pass         http://127.0.0.1:5000;
        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;
    }
}

Enable the Nginx configuration

Now we will have to enable the Nginx config file by following this command:

sudo ln -s /etc/nginx/sites-available/testapi /etc/nginx/sites-enabled

Then, test the Nginx configuration by running the following command:

sudo nginx -t

If the configuration is valid, restart Nginx by running the following command:

sudo systemctl restart nginx

That’s it! Your ASP.NET Core application should now be accessible through Nginx at the URL https://testapi.serverguy.dev.

Hope this will be helpful, I tried to keep the article as simple as possible. There might be some mistakes or extra words, pardon them and fell free to comment if you are stuck at any point. If you are planning to host multiple ASP.NET application follow this article.

  1. Hi, thanks for the article, it was really helpful.

    By the way, there is an error in the following string:
    sudo ln -s /etc/nginx/sites-available/testapi.conf /etc/nginx/sites-enabled

    Should be without .conf

Leave a Reply

Your email address will not be published. Required fields are marked *

Contabo opens DC in Singapore