ASP.NET Core RC2 as linux deamon

老子叫甜甜 提交于 2019-12-03 12:26:44

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!