ASP.NET核心RC2作为linux deamon

我需要关于networking核心控制台或asp.net应用程序作为linux deamon的托pipe信息。 主机应用程序作为Windows服务已经由Microsoft.Hosting.WindowsService支持,但我需要类似的Linux deamons。

我在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 

该文件进入/etc/systemd/system目录,然后命名为“.service”后面的任何名称。 例如,完整路径可能是/etc/systemd/system/aspnet-example.service

然后,您可以使用systemctl start aspnet-examplesystemctl 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