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
[Unit]
Description=This is a sample application for my tutorial
[Service]
WorkingDirectory=/websites/mywebsite/ #CHANGE this to your application location
ExecStart=/usr/bin/dotnet /websites/mywebsite/MyWeb.dll #CHANGE this to your application location
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-myapp
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
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-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-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-08 Piranha

Carousel block for Piranha with Bootstrap

A good image slider or carousel can complete your website. This post will give you a quick example on how you can add your own carousel to your Piranha site

2021-08-05 Piranha

Accordion block with Bootstrap 5

Sometimes you have way to much text to fit on a page. A simple way to structure and group important content is by using an accordion. This post will give you an idea on how you can implement your own accordion on a Piranha site

2021-08-03 Piranha

Piranha block for social media links

Most businesses and even personal sites have the need to share their social media links. Here's an example on how you can turn your social media links into a reusable block

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