RedNet
RedNet

Reputation: 52

Hosting Net Core app on Ubuntu

Hey guys I am having some troubles hosting my .net core on Ubuntu, I can host it fine using the following command:

dotnet /home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0/FrutaMargemCore.dll

This hosts the app just fine and I can access it via the VM's external IP, but it seems to lock it in some kind of session state where the terminal is just hosting it and I need to terminate the process to run any other commands.

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using'/home/bourdain/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Production
Content root path: /home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Closing the SSH connection terminates the process and the app is no longer hosted.

I've tried using

dotnet run /home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0/FrutaMargemCore.dll

but it randomly throws an exception

Using launch settings from /home/bourdain/website/FrutaMargemCore/Properties/launchSettings.json...

Unhandled Exception: System.FormatException: Value for switch '/home/bourdain/website/FrutaMargemCore/bin/Release/netcoreapp2.0/FrutaMargemCore.dll' is missing.
   at Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationProvider.Load()
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at FrutaMargemCore.Program.BuildWebHost(String[] args) in /home/bourdain/website/FrutaMargemCore/Program.cs:line 21
   at FrutaMargemCore.Program.Main(String[] args) in /home/bourdain/website/FrutaMargemCore/Program.cs:line 17

How can I host my netcore core on my ubuntu server without having it terminate whenever I close my SSH connection?

Upvotes: 1

Views: 607

Answers (1)

Luís Antunes
Luís Antunes

Reputation: 336

You should start by creating a service file like this:

sudo nano /etc/systemd/system/kestrel-yourApp.service

the service file will have this content:

[Unit]
Description=My ASPNET Core APP

[Service]
WorkingDirectory=/var/someFolder/yourApp
ExecStart=/usr/bin/dotnet /var/aspnetcore/yourApp/yourApp.dll
Restart=always
RestartSec=20
SyslogIdentifier=dotnet-example
User=yourUser
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Adjust the parameters for the context of your application (like your user, path, etc...). The user must have the right permissions.

This will start the service:

systemctl enable kestrel-yourApp.service

Do this just to check if its running:

systemctl status kestrel-yourApp.service

Upvotes: 2

Related Questions