MS .NET Core Web API on Ubuntu Linux

By Style Sync
| Published on
eff76-screen-shot-2020-07-06-at-2.11.54-pm

 

If you are following Microsoft news, I’m sure you have already noticed the support Microsoft provides to the Linux OS. This focus is not just the deployment of Linux VMs on Azure, it  also enables the developers to use Linux to develop and host their applications on Linux systems.

When it comes to sizing and calculations, I found myself looking for ways to automate these kinds of tasks. If you are following my blog, I’m sure you are already aware of my Microsoft 365 backup calculator tool I built to help size the Microsoft 365 backup storage requirement, and the Azure backup infrastructure, include costing.

These days I’m working on my next calculator tool. It is going to be different in many ways; one of these differences is the way I intend to deliver the new web tool.

This time I decided to create a Linux appliance, where customers will import the new calculator tool and then find it easy to begin using it. To achieve this, I was looking at how I can run my DotNet application on Linux. After some research, trial and error, I have been able to deploy .Net on a Linux Ubuntu distribution version 20.04.

On this blog post, I will share the steps I took to deploy .Net Core on Ubuntu. So, let’s get started.

Deploying .Net Core:

To keep my appliance small, like many typical Linux apps, I deployed Ubuntu server version 20.04. The deployment of the Linux OS is very simple, and as it’s a guided setup, I’m sure I safely can skip those steps.

After the Linux OS deployment, we can start with the .Net Core deployment. Let’s start with downloading the Microsoft packages, and deploying them on our newly deployed Linux OS.

Begin with these commands:

wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb

After the MS packages are deployed, we can start with the deployment of the DotNet. Continue with these commands:

sudo apt-get update

sudo apt-get install apt-transport-https

sudo apt-get update

sudo apt-get install dotnet-sdk-3.1

Upon the completing the previous steps, the MS DotNet will be installed and ready on our Linux OS. As I mentioned at the beginning of this blog post, I also want users to access my application using Web-enabled devices; therefore, we will continue on and deploy the webserver, using Ngnix:

sudo apt updatebe

sudo apt install nginx

To check the status of the ngnix, we can run the following command:

sudo systemctl status nginx

To configure Nginx as a reverse proxy to forward requests to our ASP.NET Core app, we must modify /etc/nginx/sites-available/default. Let’s use Vi to modify the default page, with the following script lines:

server {

listen 80;

root /var/netcore;

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;

}

}

After that last modification, we must restart the nginx service:

sudo service nginx start

# and if the ngnix is already started we can use the below to restart the service

sudo service nginx restart

Running DotNet as a Service:

To keep our application running as a service, we must create a service and then run it. To accomplish this task, we create a service file for our web application, as follows:

Note: the web application will be called SONAROS

sudo vi /etc/systemd/system/sonaros.service

and add the following:

[Unit]

Description= SONAROS WebApp Service

[Service]

WorkingDirectory=/var/netcore

ExecStart=/usr/bin/dotnet /var/netcore/Sonaros.dll

Restart=always

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

RestartSec=10

KillSignal=SIGINT

SyslogIdentifier=dotnet-example

User=www-data

Environment=ASPNETCORE_ENVIRONMENT=Production

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]

WantedBy=multi-user.target

After the service file is created and saved, we can enable and run it using the following commands:

sudo systemctl enable sonaros.service

sudo systemctl start sonaros.service

and to check the service status we can use this command:

sudo systemctl status sonaros.service

Conclusion:

Following the steps described above will get you up and ready to host your MS DotNet application on the Linux OS. You will also have noticed my working directory, or path, to my application is under var/netcore/ Note: Sonaros.dll is the name of the application.

If you face any problems with the service and the DotNet application and you want to troubleshoot, use the following command to get started

sudo journalctl -fu sonaros.service

What do you think?

Leave a Reply

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

Subscribe to Our Newsletter

Table of Contents

Related Insights