Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run ASP.NET Core app under Linux on startup

I would like to run my ASP.NET Core solution under linux with the result it runs on startup.

From Microsoft docs, there are 2 ways: Apache and Nginx.

Both approaches involve proxy pass, e.g.

Apache:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ....

Nginx:

server {
    listen        80;
    server_name   example.com *.example.com;
    location / {
        proxy_pass         http://localhost:5000;
        ...

Since Apache or Nginx only acts as proxy - do I get it right that I have to manually start the dotnet app?

I can't see the bit in the documentation where something could trigger dotnet run command against my WebApi project.

Obviously, Apache or Nginx wouldn't handle triggering dotnet app - unless I've missed something.

Is there a way to automatically start the app on OS startup?

like image 985
Alex Herman Avatar asked Jul 16 '18 05:07

Alex Herman


People also ask

Can ASP.NET Core run on Linux?

With ASP.NET Core, you can build and run . NET applications not only on Windows but also macOS and Linux.

Can you run .NET apps on Linux?

Now there's an alternative that's maturing and gaining popularity--you can run . NET applications on Linux, using the open source Mono runtime. And that's it--Mono will run your . NET binaries without requiring any conversion.


1 Answers

This section in docs describes, how to create a service file to automatically start your Asp.Net Core app.

Create the service definition file:

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

The following is an example service file for the app:

[Unit]
Description=Example .NET Web API App running on Ubuntu

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

[Install]
WantedBy=multi-user.target

Save the file and enable the service.

systemctl enable kestrel-hellomvc.service

Start the service and verify that it's running.

systemctl start kestrel-hellomvc.service
systemctl status kestrel-hellomvc.service

You need to set WorkingDirectory - path to folder with your app and ExecStart - with path to your app dll. By default this is enough.

From now, your app will automatically start on OS startup and will try to restart after crashes.

like image 161
Groxan Avatar answered Oct 24 '22 02:10

Groxan