Deploy .Net Core web API to Linux Ubuntu

1

Introduction

.Net Core is a free and open-source framework from Microsoft. The beauty of .Net Core is that it is cross-platform and you can run it on Windows, Linux, or MAC. So you can develop your web API on your favorite OS like Windows or MAC and host it on a Linux machine.
In this tutorial, I will show how to deploy your .Net Core web API on a clean Ubuntu virtual machine with Nginx.

Setting up Linux VM

You will need SSH access to the virtual machine and root permissions. First of all, let’s update the VM and install the necessary minimum software.

sudo apt update

The sudo apt-get update command is used to download package information from all configured sources.

sudo apt -y install vim bash-completion wget

This command will install vim, bash-completion and wget packages.

  • vim – is a text editor
  • bash-completion is a collection of command line command completions for the Bash shell, collection of helper functions to assist in creating new completions, and set of facilities for loading completions automatically on demand, as well as installing them.
  • wget – package for retrieving files using HTTP, HTTPS, FTP and FTPS, the most widely used Internet protocols.
sudo apt -y upgrade

upgrade is used to install the newest versions of all packages currently installed on the system

sudo reboot

After upgrade command is run we need to reboot a VM to make sure upgrade is completed.

Download and Install runtime

To run a .Net Core applications on any OS we need to install .Net Core Runtime. The current latest version of .Net Core is 3.1 and you can install it with the following command:

sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y aspnetcore-runtime-3.1

Detail instructions and troubleshooting guide is here.

Install Nginx

NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.

You can install it with a following command:

sudo apt-get install nginx

or the full instruction is located on an official web site.

Start Nginx with a command:

sudo service nginx start

Configure Nginx

To configure Nginx as a reverse proxy to forward requests to your ASP.NET Core app, modify /etc/nginx/sites-available/default. Open it in a text editor, and replace the contents with the following:

If you want to get more details on what is reverse proxy and why do we need to use it read this article.

nano /etc/nginx/sites-available/default

Add the following code:

server {
    listen        80;
    server_name   YOUR_DOMAIN;
    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;
    }
}

Save the file and verify syntaxis with the following command:

sudo nginx -t

If everything is OK rerun the Ngnix to apply new settings:

sudo nginx -s reload

Configure .Net Core web api

Firstly you will need to build a web api in release mode on your machine. To do this run the command in root location of your application:

dotnet publish --configuration Release

Create a folder in Ubuntu /var/www/your_app_name

Copy all files to that folder. You can use FTP (if it is available on your VM) or SSH File Transfer Protocol (I am using CyberDuck for that).

In terminal navigate to your application folder and run the application. This is just to verify that it works:

dotnet <app_assembly.dll>

If app started without any issues press CTRL + C to stop it.

We want our application to be run from services. In this way we can instruct Ubuntu to restart our application automatically if it crashes or after VM restart.

Create a service file:

sudo nano /etc/systemd/system/kestrel-<your_app>.service

Paste the follwing code:

[Unit]
Description=<App Name>

[Service]
WorkingDirectory=/var/www/<app_folder>
ExecStart=/usr/bin/dotnet /var/www/<app_folder>/<app_name>.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

Save the file, than enable and start the service:

sudo systemctl enable kestrel-<app_name>.service
sudo systemctl start kestrel-<app_name>.service 

You can check service status with the following command:

sudo systemctl status kestrel-alfalab.service

Nazar Martincenco

About

Nazar is a passionate full-stack and SDET with more than 12 years of experience. His main topics of interest are digital transformation, web APIs, cloud and Data Science. Nazar likes working with the most technically complicated projects and always delivers them with high standards. Nazar also works on his own start up project.

More on Nazar Martincenco.

Related Posts

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