【问题标题】:run ASP.NET Core app under Linux on startup启动时在 Linux 下运行 ASP.NET Core 应用程序
【发布时间】:2018-12-23 14:10:35
【问题描述】:

我想在 linux 下运行我的 ASP.NET Core 解决方案,结果它在启动时运行。

来自 Microsoft docs,有两种方法:ApacheNginx

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

阿帕奇:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ....

Nginx:

server {
    listen        80;
    server_name   example.com *.example.com;
    location / {
        proxy_pass         http://localhost:5000;
        ...

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

我在文档中看不到某些内容可能会针对我的 WebApi 项目触发 dotnet run 命令。

显然,Apache 或 Nginx 不会处理触发 dotnet 应用程序 - 除非我错过了什么。

有没有办法在操作系统启动时自动启动应用程序

【问题讨论】:

    标签: linux apache ubuntu nginx asp.net-core


    【解决方案1】:

    文档中的This section 描述了如何创建服务文件以自动启动您的 Asp.Net Core 应用程序。

    创建服务定义文件:

    sudo nano /etc/systemd/system/kestrel-hellomvc.service
    

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

    [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
    

    保存文件并启用服务。

    systemctl enable kestrel-hellomvc.service
    

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

    systemctl start kestrel-hellomvc.service
    systemctl status kestrel-hellomvc.service
    

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

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

    【讨论】:

    • 对于遇到我遇到的问题的其他人;如果您希望您的应用程序绑定在端口 上,则用户必须设置为 root
    • 如果您不启用该服务,它将一直工作到系统下一次“重新启动”。
    猜你喜欢
    • 2018-06-02
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2011-11-05
    • 1970-01-01
    • 2014-05-07
    • 2017-12-24
    相关资源
    最近更新 更多