I need information about hosting of net core console or asp.net application as linux deamon. Hosting application as Windows Service is already supported by Microsoft.Hosting.WindowsService, but I need something similar for linux deamons.
I'm running on RHEL, and therefore have chosen to write my own systemd unit files. Here is an example of one I use in conjuction with PostgreSQL (hence the Environment variable). I've stripped out sensitive information for obvious reasons.
[Unit]
Description=My Sample Application
Documentation=
Wants=network.target
After=network.target
[Service]
User=dotnetuser
Group=dotnetuser
Nice=5
KillMode=control-group
SuccessExitStatus=0 1
Environment=MY_CONNSTRING=Server=localhost;Username=myUser;Password=myPass;Database=myDatabase
NoNewPrivileges=true
PrivateTmp=true
InaccessibleDirectories=/sys /srv -/opt /media -/lost+found
ReadWriteDirectories=/var/www/myapp
WorkingDirectory=/var/www/myapp
ExecStart=/opt/dotnet/dotnet run
[Install]
WantedBy=multi-user.target
The file goes in the /etc/systemd/system
directory and is named whatever you'd like to name the service with ".service" after it. For example, the full path might be /etc/systemd/system/aspnet-example.service
.
You could then start and stop the service with systemctl start aspnet-example
and systemctl stop aspnet-example
.
To setup the service to start at boot: systemctl enable aspnet-example
The main things to point out in the configuration file are:
User and Group should NOT be root. I advise making a new user under which your applications run.
KillMode=control-group is set so that a SIGTERM is sent to all dotnet processes under which the service is run.
ReadWriteDirectory and WorkingDirectory point to the root of your web application. I've used
/var/www/myapp
as an example.ExecStart must be an absolute path to the dotnet binary. Systemd doesn't support relative paths.
EDIT: One thing I forgot to mention is that I generally run nginx as a reverse proxy in front of my applications. I've included a link which has more information about publishing to a Linux production server.
https://docs.asp.net/en/latest/publishing/linuxproduction.html
© 2022 - 2024 — McMap. All rights reserved.