我有一个在 Ubuntu 16.04 服务器上运行的 AWS EC2 实例。我在此实例中运行 ASP.NET Core Web API 服务器。
我已经关注了这个link使用 Nginx
在 Linux 上托管 ASP.NET Core。我一直按照教程进行操作,直到监视应用程序
部分。现在,我已使用以下命令运行 Web api dll 文件,并且它正在监听 http://localhost:5000
dotnet MyWebAPI.dll
我已使用 PuTTY 连接 EC2 实例来运行上述命令。由于我已经设置了反向代理服务器,因此我可以使用 postman 很好地到达我的端点。
但是,当我关闭 PuTTY session 时,dll 文件不再运行。结果,我无法到达终点。
那么,我应该怎样做才能使 dll 文件作为服务在本地主机上运行,以便在关闭 PuTTY session 时它不会停止?
最佳答案
我可以将我的应用程序作为 Nginx
服务器内的服务运行。现在,如果我关闭 PuTTY session ,该服务仍在运行。我使用 systemd 创建一个服务文件来启动和监控底层 Web 应用程序。
systemd is an init system that provides many powerful features for starting, stopping, and managing processes.
首先,我为我的网络应用程序创建了一个服务文件
sudo nano /etc/systemd/system/my-web-api.service
然后,在 my-web-api.service
文件中,我包含了以下配置:
[Unit]
Description=My first .NET Core application on Ubuntu
[Service]
WorkingDirectory=/home/ubuntu/MyWebAPI
ExecStart=/usr/bin/dotnet /home/ubuntu/MyWebAPI/MyWebAPI.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=offershare-web-app
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target
然后,我需要启用该服务并运行它。
sudo systemctl enable my-web-api.service
sudo systemctl start my-web-api.service
sudo systemctl status my-web-api.service
现在,如果配置正确,状态命令应显示服务正在运行。现在,我的 Web 应用程序正在运行,kestrel 默认情况下监听端口 5000,因此我的应用程序可在 http://localhost:5000 上使用。 。
现在,如果我关闭 PuTTY session ,我的 Web api 仍在运行。
关于.net - 如何在 Nginx 服务器中将 ASP.NET Core Web API dll 文件作为服务运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51477473/