启动时在Linux下运行ASP.NET Core应用

Ale*_*man 8 linux apache ubuntu nginx asp.net-core

我想在Linux下运行ASP.NET Core解决方案,使其在启动时运行。

从Microsoft 文档来看,有两种方法:ApacheNginx

两种方法都涉及代理通行证,例如

阿帕奇:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ....
Run Code Online (Sandbox Code Playgroud)

Nginx:

server {
    listen        80;
    server_name   example.com *.example.com;
    location / {
        proxy_pass         http://localhost:5000;
        ...
Run Code Online (Sandbox Code Playgroud)

由于Apache或Nginx仅充当代理-我是否正确,必须手动启动dotnet应用程序

我在文档中看不到有什么地方可以触发dotnet run针对我的WebApi项目的命令。

显然,Apache或Nginx不会处理触发dotnet应用程序的问题-除非我错过了一些事情。

有没有一种方法可以在OS启动时自动启动应用程序

小智 12

文档中的此部分介绍了如何创建服务文件以自动启动Asp.Net Core应用。

创建服务定义文件:

sudo nano /etc/systemd/system/kestrel-hellomvc.service
Run Code Online (Sandbox Code Playgroud)

以下是该应用程序的示例服务文件:

[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/var/aspnetcore/hellomvc
ExecStart=/usr/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Development

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

保存文件并启用服务。

systemctl enable kestrel-hellomvc.service
Run Code Online (Sandbox Code Playgroud)

启动服务,并验证它正在运行。

systemctl start kestrel-hellomvc.service
systemctl status kestrel-hellomvc.service
Run Code Online (Sandbox Code Playgroud)

您需要设置WorkingDirectory-应用程序文件夹的路径和ExecStart-应用程序dll的路径。默认情况下,这就足够了。

从现在开始,您的应用将在操作系统启动自动启动,在崩溃后尝试重新启动