How to run .Net Core website on DigitalOcean Ubuntu 20.04

This tutorial is not about how to publish your .Net Core website to the DigitalOcean Ubuntu server, but on how to setup your Ubuntu server to run .NET Core website after you've published and uploaded your site to the server.

 

There are 3 prerequisites before you can run your .Net Core website on Ubuntu, please follow them below before you continue with the tutorial:

1. Set up a Production-Ready Droplet

2. How to Install Nginx on Ubuntu

3. How to secure Nginx with Let's Encrypt

 

Now we can move on to the important stuff.

 

1. Install OpenSSH

Get started with OpenSSH

 

2. Open your command Prompt and ssh into your root

ssh root@<your digitalocean ip address>

 

3. Install .net on the Ubuntu Server

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

 

4. Install .net sdk

sudo apt-get update; \

sudo apt-get install -y apt-transport-https && \

sudo apt-get update && \

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

 

5. Install .net core runtime

sudo apt-get update; \

sudo apt-get install -y apt-transport-https && \

sudo apt-get update && \

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

6. Create a new service for .net core

sudo nano /etc/systemd/system/<name of your project>.service

 

[Unit]

Description=<name of your project> running on Ubuntu



[Service]

WorkingDirectory=/var/www/<name of your project>.com/html

ExecStart=/usr/bin/dotnet /var/www/<name of your project>.com/html/<name of your project>.Web.dll

Restart=always

# Restart service after 10 seconds if the dotnet service crashes:

RestartSec=10

KillSignal=SIGINT

SyslogIdentifier=<name of your project>

User=www-data

Environment=ASPNETCORE_ENVIRONMENT=Production

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false



[Install]

WantedBy=multi-user.target

 

7. Enable the service

sudo systemctl enable <name of your project>.service

 

8. Start service

sudo systemctl start <name of your project>.service

 

9. Check service is running

sudo systemctl status <name of your project>.service

 

10. Create server blocks

sudo nano /etc/nginx/sites-available/<name of your project>.com

 

server {

...

...

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;

}

}

 

11. Enable the file by creating a link from it to the sites-enabled

sudo ln -s /etc/nginx/sites-available/<name of your project>.com /etc/nginx/sites-enabled/

 

12. Avoid a possible hash bucket memory problem

sudo nano /etc/nginx/nginx.conf

 

...

http {

...

server_names_hash_bucket_size 64;

...

}

...

 

13. Test to make sure that there are no syntax errors in any of your Nginx files

sudo nginx -t

 

14. Restart nginx server

sudo systemctl restart nginx

 

Now your .Net Core website should be up and running, Enjoy your new site!

 

15. Optional, After uploading a newer version of your .Net Core website, you will need to restart service for the changes to take effect

sudo systemctl restart <name of your project>.service

Leave a comment