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-14 Tropico 6

Boost your economy early game

Starting a new game can sometimes be difficult, at least when you can't really get your money flowing. I'd like to share a few tips and tricks that always seem to work for me no matter which era I'm currently in

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-11 Piranha

Track users with Google Analytics on your Piranha site

Keep a track on your visitors and which pages are popular with Google Analytics. This post gives you a great example on how you can implement a dynamic way to add end edit your tracking codes

2021-08-07 Piranha

Youtube block for Piranha

Want to show Youtube videos on your Piranha site? This post will give you a quick example on how you could implement a Youtube block on your site, either for showing useful videos or Rick-Rolling your visitors

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