ASP.NET Core RC2作为Linux守护进程 [英] ASP.NET Core RC2 as linux deamon
问题描述
我需要有关作为Linux守护程序托管网络核心控制台或asp.net应用程序的信息。 Microsoft.Hosting.WindowsService已经支持将应用程序作为Windows Service托管,但是我需要Linux守护程序类似的东西。
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在RHEL上运行,因此选择编写自己的systemd单元文件。这是我与PostgreSQL结合使用的一个示例(因此环境变量)。我出于明显的原因删除了敏感信息。
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
文件位于 / etc / systemd / system
目录,并以其后的 .service命名服务名称。例如,完整路径可能是 /etc/systemd/system/aspnet-example.service
。
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
.
您然后可以使用 systemctl start aspnet-example
和 systemctl stop aspnet-example
启动和停止服务。
You could then start and stop the service with systemctl start aspnet-example
and systemctl stop aspnet-example
.
要将服务设置为在启动时启动: systemctl启用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:
-
用户和组不应是root用户。我建议您让一个新用户运行您的应用程序。
User and Group should NOT be root. I advise making a new user under which your applications run.
KillMode = control-group已设置为将SIGTERM发送到所有的dotnet进程,服务运行。
KillMode=control-group is set so that a SIGTERM is sent to all dotnet processes under which the service is run.
ReadWriteDirectory和WorkingDirectory指向Web应用程序的根目录。我以 / var / www / myapp
为例。
ReadWriteDirectory and WorkingDirectory point to the root of your web application. I've used /var/www/myapp
as an example.
ExecStart必须是绝对值dotnet二进制文件的路径。 Systemd不支持相对路径。
ExecStart must be an absolute path to the dotnet binary. Systemd doesn't support relative paths.
编辑:我忘了提到的一件事是,我通常以以下方式运行nginx:在我的应用程序前面的反向代理。我提供了一个链接,其中包含有关发布到Linux生产服务器的更多信息。
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/zh/latest/publishing/linuxproduction.html
这篇关于ASP.NET Core RC2作为Linux守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!