Netcore 2.1 Preview on Debian

Installing and testing .netcore 2.1.0 on Debian Strecth

Installation of netcore 2.1.0 preview on debian. Creation of a simple WebApi. Packing and installing the api as service using nginx.

Disk usage

Installing and running the sdk will use about 1.5Gb of disk space in /usr/share/dotnet.

Installation

Followin the steps from the offical guide.

Install system components

sudo apt-get update
sudo apt-get install curl libunwind8 gettext apt-transport-https

Register the trusted Microsoft Product key

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg

Register the Microsoft Product feed

sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list'

Install .NET SDK

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install dotnet-sdk-2.1.300-preview1-008174

First application

On the first run dotnet will initialize some stuff for nuget, telemetry and so on. It needs sudo privileges.

sudo dotnet new console -o myApp

From now on it is poosible to run without sudo (probably there is a shorter way of doing the initialization).

Remove the application (it has root permission) and recreate it

sudo rm -r myApp/
dotnet new console -o myApp
cd myApp
# download missing nuget packages
dotnet restore
# start the application
dotnet run

It should print a nice ‘Hello World!’

WebApi application

Let’s test a simple WebApi.

cd ~
dotnet new webapi -o myWebApi
cd myWebApi
dotnet run

Now with the browser navigate to https://localhost:5001/api/values and the response should be ["value1","value2"]

Or with the terminal

curl -i https://localhost:5001/api/values -k

Now we remove the requirement for https to simplify the nginx configuration. In the file startup.cs comment out the https redirection

// app.UseHttpsRedirection();

Try again without ssl

dotnet run
curl -i http://localhost:5000/api/values

WebApi pubblication on Nginx

Install Nginx

sudo apt install nginx
sudo service nginx start

Configure Nginx by editing /etc/nginx/sites-available/default

sudo atom /etc/nginx/sites-available/default

Add the following configuration to set nginx as a proxy to dotnet.

server {
    listen 80;
    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 $http_host;
        proxy_cache_bypass $http_upgrade;
    }
}

Test the configuration validity

sudo nginx -t

Now let’s try a run. At this point the application is still running from a terminal.

sudo service nginx restart
dotnet run
curl -i http://localhost:80/api/values

Packing and running the dotnet application

Executable folder

Create a folder to store the dotnet applications and a folder for the WebApi

sudo mkdir /var/aspnetcore
sudo mkdir /var/aspnetcore/myWebApi
sudo chown -R www-data:www-data /var/aspnetcore
sudo chown www-data:www-data /var/aspnetcore/myWebApi

Copy the files

cd ~/myWebApi/
dotnet publish -c Production
sudo cp -r ./bin/Production/netcoreapp2.1/publish/* /var/aspnetcore/myWebApi/
sudo chown -R www-data:www-data /var/aspnetcore/myWebApi/*

System service

Configure a new systemd service for kestrel

sudo atom /etc/systemd/system/kestrel-helloapi.service

Paste the foollowing:

[Unit]
Description=Example netcore 2.1.0 Web API App

[Service]
WorkingDirectory=/var/aspnetcore/myWebApi
ExecStart=/usr/bin/dotnet /var/aspnetcore/myWebApi/myWebApi.dll
Restart=always
# Restart service after 10 seconds if dotnet service crashes
RestartSec=10
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Save the file and enable the service.

systemctl enable kestrel-helloapi.service

Manage the status of the service

sudo service kestrel-helloapi status
sudo service kestrel-helloapi restart

GO

Test that all is ok

curl -i http://localhost:80/api/values