woensdag 17 augustus 2022

asp.net in linux

in asp.net 

add systemd support

var builder = WebApplication.CreateBuilder(args);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
    builder.Host.UseSystemd(); // for linux...
}


    <PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="6.0.0" />

add forwarding for nginx

var app = builder.Build();

//for nginx
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

 

on ubuntu create a directory in:

/var/www/<appname>

build / publish asp.net project for linux

dotnet publish -c Release -r linux-x64 --no-self-contained

Check if .net runtime is installed if it does not work you can also use --self-contained in your build command

copy contents to directory

give permission to <appname> dir with command:

chmod 775 -R /var/www/<appname>

set execute rechten op exe file

chmod +x /var/www/<appname dir>/<appname> (without dll) in linux

Systemd

Create a service file in systemd

create an empty <appname>.service file (touch filename)

put in the following content (nano filename)

[Unit]

Description=app description

After=network.target


[Service]

WorkingDirectory=/var/www/uitgekookt-logistics-route-generation-api/current

ExecStart=/var/www/<appdir>/<linux generated file without extension / appname>

Restart=always

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

RestartSec=10

KillSignal=SIGINT

User=www-data

Environment=ASPNETCORE_ENVIRONMENT=Production

Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

Environment=AllowedHosts=*

Environment=ApplicationInsights__InstrumentationKey=

Environment=Connectionstrings__ABC=abc

Environment=ASPNETCORE_URLS=http://localhost:5040


Enable service:

sudo systemctl enable <appname>.service

Start

sudo systemctl start <appname>.service


NGINX

For nginx create an <appname> file in /etc/nginx/sites-available/ dir

with contents:

server {

    listen 50400; //external port

    listen [::]:50400; 

    location / {

        proxy_pass         http://127.0.0.1:5040; //portnumber of .net api

        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;

    }

}

create a systemlink from sites-available to sites-enabled:

ln -s /etc/nginx/sites-available/<appname> /etc/nginx/sites-enabled/<appname>

allow port 50400 in firewall (ufw enable)

reload nginx (nginx -s reload)

when there are issues with the application scan the log via:

sudo journalctl -fu <appname>.service


Meer info:

Host ASP.NET Core on Linux with Nginx | Microsoft Docs


Geen opmerkingen:

Een reactie posten