简体   繁体   English

如何在Nginx服务器中将ASP.NET Core Web API dll文件作为服务运行?

[英]How to run a ASP.NET Core Web API dll file as a service in Nginx server?

I have an AWS EC2 instance running on Ubuntu 16.04 server. 我有一个在Ubuntu 16.04服务器上运行的AWS EC2实例。 I am running an ASP.NET Core Web API server in this instance. 我在这种情况下正在运行ASP.NET Core Web API服务器。

I have followed this link to host ASP.NET Core on Linux with Nginx . 我已遵循此链接在Linux上使用Nginx托管ASP.NET Core。 I have followed the tutorial until the Monitoring the app section. 我已按照该教程进行操作,直到“ Monitoring the app部分为止。 Now, i have run the web api dll file using below command and it is listening at http://localhost:5000 现在,我已经使用以下命令运行了Web api dll文件,并且它正在http://localhost:5000监听

dotnet MyWebAPI.dll

I have run this above command by connecting with the EC2 instance using PuTTY . 我已经通过使用PuTTY与EC2实例连接来运行以上命令。 As i have set up the reverse proxy server, so i can hit my endpoint nicely using postman. 因为我已经设置了反向代理服务器,所以我可以使用邮递员很好地到达端点。

But, when i close the PuTTY session, the dll file is not running anymore. 但是,当我关闭PuTTY会话时,dll文件不再运行。 As a result, i could not hit the endpoint. 结果,我无法达到目标。

So, what should i do to keep the dll file running on localhost as a service so that it does not stop when i close the PuTTY session? 因此,我应该怎么做才能使dll文件作为服务在localhost上运行,以使其在我关闭PuTTY会话时不会停止?

I could run my application as a service inside the Nginx server. 我可以在Nginx服务器中将应用程序作为服务运行。 Now, if i close the PuTTY session , the service is still running. 现在,如果我关闭PuTTY session ,该服务仍在运行。 I have used systemd to create a service file to start and monitor the underlying web app. 我已经使用systemd创建了一个服务文件来启动和监视基础的Web应用程序。

systemd is an init system that provides many powerful features for starting, stopping, and managing processes. systemd是一个初始化系统,它提供了许多用于启动,停止和管理进程的强大功能。

At first, i have created a service file for my web application 首先,我为我的Web应用程序创建了一个服务文件

sudo nano /etc/systemd/system/my-web-api.service 须藤纳米/etc/systemd/system/my-web-api.service

Then, inside that my-web-api.service file, i have included below configurations: 然后,在该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

Then, i need to enable the service and run it. 然后,我需要启用该服务并运行它。

sudo systemctl enable my-web-api.service
sudo systemctl start my-web-api.service
sudo systemctl status my-web-api.service

Now, the status command should show the service as running if the configuration is correct. 现在,如果配置正确,则status命令应将服务显示为正在运行。 Now, my web application is running, kestrel by default listens on port 5000 , so my application is available on http://localhost:5000 . 现在,我的Web应用程序正在运行,默认情况下,kestrel监听端口5000 ,因此我的应用程序位于http:// localhost:5000上

Now, if i close the PuTTY session, my web api is still running. 现在,如果我关闭PuTTY会话,则我的Web API仍在运行。

Posting on behalf of @Brudex (this was edited into the question, instead of being posted as an answer - happy to delete this copy if original author posts this him/her-self correctly as an answer) 代表@Brudex发布(此内容已编辑到问题中,而不是作为答案发布-如果原始作者正确地将其自己发布为答案,则很乐意删除此副本)


You have to use a process manager like supervisor to keep the asp.net file running. 您必须使用进程管理器(如supervisor)才能使asp.net文件保持运行状态。 Use nginx as a reverse proxy to forward request to your asp.net core application. 使用nginx作为反向代理将请求转发到asp.net核心应用程序。

Follow to install and setup supervisor to manage ur asp.net apps 按照以下步骤安装和设置主管以管理ur asp.net应用

  1. sudo apt-get install supervisor sudo apt-get install supervisor
  2. create a supervisor config like follows 创建一个主管配置,如下所示

     [program:hellomvc] command=/usr/bin/dotnet /var/aspnetcore/HelloMVC/HelloMVC.dll directory=/var/aspnetcore/HelloMVC/ autostart=true autorestart=true stderr_logfile=/var/log/hellomvc.err.log stdout_logfile=/var/log/hellomvc.out.log environment=HOME=/var/www/,ASPNETCORE_ENVIRONMENT=Production user=www-data stopsignal=INT stopasgroup=true killasgroup=true 
  3. Start supervisor with 启动主管

     sudo service supervisor start 

You can find more information from: 您可以从以下位置找到更多信息:
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM