Alex Herman
Alex Herman

Reputation: 2838

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?

Upvotes: 19

Views: 16292

Answers (1)

Groxan
Groxan

Reputation: 778

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.

Upvotes: 37

Related Questions