Jump to main content

Host your .NET Core project on Linux with Nginx

2021-08-03 Linux
Have you been looking for hosting alternatives for your .NET Projects? Why not host them yourself? .NET Core is built to run on Linux as well as on Windows. This guide will take you trough all necessary steps to get your project running on a Linux server.

Get yourself a good server

The first step is to get yourself a server. Here bellow you’ll find a list with good server options. This guide will work for most Debian based operating systems.

Install the latest version of .NET Core

If you wish to install other versions of .NET Core or simply need the instructions for another operative system then visit: https://dotnet.microsoft.com/download/dotnet-core

Before we can install .NET Core we have to make sure that the package-installer knows where to find them. Run the following commands:

Add the Microsoft package signing key
sudo wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

Run these commands in the following order to install the runtime for .NET Core 5.0. If you wish to install another version then visit the link above for further instructions

Install .NET Core 5.0
sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install -y aspnetcore-runtime-5.0

Install Nginx

Since .NET Core has it's own built in webbserver which is somewhat limited, we'll use Nginx as a reverse proxy. 

Install Nginx
sudo apt-get install nginx
Make sure Nginx is running by typing in:
sudo systemctl status nginx
Start, stop or restart Nginx by typing the following commands
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
Tell Nginx where to find your website
sudo vim /etc/nginx/sites-available/default
server {
    listen        80;
    server_name   example.com;
    root /websites/mywebsite;
    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 $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;

Configure your service

Create a new service file
sudo vim /etc/systemd/system/MY-SERVICE-NAME.service
Service file example
Description=This is a sample application for my tutorial
WorkingDirectory=/websites/mywebsite/ #CHANGE this to your application location
ExecStart=/usr/bin/dotnet /websites/mywebsite/MyWeb.dll #CHANGE this to your application location

Enable your service
sudo systemctl enable MY-SERVICE-NAME.service
Start your service
sudo systemctl start MY-SERVICE-NAME.service

Install a SSL certificate with Letsencrypt

sudo add-apt-repository ppa:certbot/certbot

sudo apt-get install python-certbot-nginx

sudo certbot --nginx -d example.com

You might also want to read the following

2021-08-18 Accessibility

Skip navigation and jump straight to your main content

This site has a "skip to content" function, have you noticed? After loading a page the first tab press will focus on the anchor link and make it visible on the page. Follow this guide to make your own navigation skipper.

2021-08-14 Tropico 6

Traffic management

Do you ever experience traffic congestion? The chances are high that you have or will once your city starts expanding. But how should we combat this? This guide will help you with just that.

2021-08-12 Piranha

Edit block templates inside the manager with Scriban

Want to edit block templates without rebuilding your project? Let Scriban help you with that. Scriban parses your custom Html and objects and then gives you HTML ready to be used anywhere.

2021-08-10 Piranha

Manage your footer with a region

Footers are a basic component of almost all websites. Therefor we'll make it as easy as possible for us to manage ours by turning it into a region area on our site model. This guide uses Bootstrap 5

2021-08-03 Piranha

Display breadcrumbs with Bootstrap 5 on a Piranha site

Here's a quick and easy way to implement breadcrumbs on your Piranha site. The solution is based on Bootstrap 5 breadcrumb component