ASP.NET Core RC2作为linux deamon

Tom*_*ako 9 .net linux service daemon .net-core-rc2

我需要有关作为linux deamon托管网络核心控制台或asp.net应用程序的信息.Microsoft.Hosting.WindowsService已经支持将应用程序作为Windows服务托管,但我需要类似于linux deamons的东西.

Dav*_*ord 9

我正在运行RHEL,因此选择编写自己的systemd单元文件.这是我与PostgreSQL结合使用的一个例子(因此是环境变量).出于显而易见的原因,我已经删除了敏感信息.

[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  
Run Code Online (Sandbox Code Playgroud)

该文件进入/etc/systemd/system目录,并在其后命名为".service"的任何名称.例如,完整路径可能是/etc/systemd/system/aspnet-example.service.

然后,您可以使用systemctl start aspnet-example和启动和停止服务systemctl stop aspnet-example.

要将服务设置为在引导时启动: systemctl enable aspnet-example

配置文件中要指出的主要内容是:

  • 用户和组不应该是root用户.我建议创建一个运行应用程序的新用户.

  • 设置KillMode = control-group,以便将SIGTERM发送到运行服务的所有dotnet进程.

  • ReadWriteDirectory和WorkingDirectory指向Web应用程序的根目录.我用过/var/www/myapp一个例子.

  • ExecStart必须是dotnet二进制文件的绝对路径.Systemd不支持相对路径.

编辑:我忘记提到的一件事是我通常在我的应用程序前面运行nginx作为反向代理.我已经包含了一个链接,其中包含有关发布到Linux生产服务器的更多信息.

https://docs.asp.net/en/latest/publishing/linuxproduction.html