如何在Ubuntu上托管/发布.Net Core WebAPI? [英] How do I host/publish my .Net Core WebAPI on Ubuntu?

查看:273
本文介绍了如何在Ubuntu上托管/发布.Net Core WebAPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习.Net Core.

I am learning .Net Core.

我已经使用ASP.Net MVC开发了WebApplication,并且可以在本地IIS上安装和运行它.

I have developed WebApplication using ASP.Net MVC and as it can be installed and run on Local IIS.

与在Ubuntu和Linux中托管/发布.Net Core WebApi而不在5000这样的特定端口上运行类似的方法是什么?

What's the similar way to Host / Publish .Net Core WebApi in Ubuntu and Linux instead of running on specific port like 5000?

泊坞窗对这种情况有帮助吗?如果是,那我该怎么用?

Is docker helpful for that context? If yes then how can I use it?

是否可以在没有docker的情况下托管/发布?我如何在没有Docker的情况下托管/发布?

Is it possible to host / publish without docker? How can i Host / Publish without Docker?

我还阅读了以下链接并实现了所有步骤. 发布到Linux生产环境

I also read following link and implemented all steps. Publish to a Linux Production Environment

在上面的链接中,我无法确定访问webapi的网址是什么?

In above link i am unable to identify what will be the url to access webapi?

推荐答案

正如@Pawel所指出的那样,推荐的托管.NET Core Web API或ASP.NET Core应用程序的方法是使用内置的Web服务器Kestrel. dotnet核心工具.出于开发目的,您不需要其他Web服务器即可启动和测试您的api.

As @Pawel has noted, the recommended way to host your .NET Core Web API or ASP.NET Core application is using Kestrel, the webserver which is built into the dotnet core tooling. For development purposes you do not need another webserver to start and test your api.

您不需要Docker来托管Web应用程序/API ,但应考虑将其用于生产托管,因为这是一种自动化,发布和隔离进程的干净,快速的方法.

You do not need Docker to host your web application/API, but should consider it for production hosting because it's a clean, fast way to automate releases and isolate processes.

使用Docker的过程结构是相同的-Docker只是托管和管理过程.您将让Kestrel在一个Docker容器中运行API,而Nginx(在另一个容器中或安装在基本OS上)将调用转发给它.

With Docker the process structure is the same - Docker just hosts and manages the processes. You would have Kestrel running you API in one Docker container, and Nginx (in another container or installed on the base OS) forwarding calls to it.

  • 在Ubuntu上,使用Nginx(或Apache)提供公共HTTPS,并将其配置为将请求转发到通常在端口5000上运行的Kestrel服务器.如果您的服务器运行防火墙,则不要暴露端口5000. ,但在该计算机上打开端口443(HTTPS).您所引用的文章介绍了设置Nginx.如前所述,不仅仅需要启动和测试您的Web API.
  • 茶est非常快速但非常简单-例如.它不支持HTTPS(您应将其用于公共API,因为您将需要身份验证,并且没有HTTPS就无法安全地进行身份验证.在Kestrel上使用Nginx/Apache的原因有很多-安全性,负载平衡,反向代理功能等.

简单的步骤即可让您的API在开发设置中运行

  • 确保在project.json中定义适当的运行时

  • Ensure you are defining the appropriate runtime in you project.json

"runtimes": {
   "win7-x64": {},
   "win81-x64": {},
   "ubuntu.14.04-x64": {},
   "debian.8-x64": {}
}

  • 确保您的project.json在buildOptions部分中定义了"emitEntryPoint": true.
  • 为要部署到的平台构建项目:dotnet build -r ubuntu.14.04-x64 --build-profile Release
  • 为平台发布项目:dotnet publish -r ubuntu.14.04-x64 --configuration Release -o ./bin/Release/Publish
  • Ensure that your project.json defines "emitEntryPoint": true in the buildOptions section.
  • Build your project for the platform you will deploy to: dotnet build -r ubuntu.14.04-x64 --build-profile Release
  • Publish you project for the platform: dotnet publish -r ubuntu.14.04-x64 --configuration Release -o ./bin/Release/Publish
  • 使用所示的命令行为Ubuntu构建和发布您的应用-我在VS 2015 Update 3中尝试过,但在为正确的运行时构建它时遇到了问题

    • 将发布"文件夹中的文件复制到您的Ubuntu VM或服务器,并添加应用程序需要运行的所有文件,例如appsettings.json.
    • 确保在您的Linux计算机上安装了适当的.NET Core框架.
    • 打开一个终端窗口,sudo -i以获得管理员权限,cd进入您放置二进制文件并使用以下命令运行api的文件夹:dotnet MyWebApi.dll其中MyWebApi.dll是构建过程的主要输出./li>
    • Copy the files in the Publish folder to your Ubuntu VM or server, and add any files you app needs to run, such as appsettings.json.
    • Ensure that the appropriate .NET Core framework is installed on your Linux machine.
    • Open a terminal window, sudo -i to get admin rights, cd to the folder where you put your binaries and run your api using : dotnet MyWebApi.dll where MyWebApi.dll is the main output of your build process.

    这时,Kestrel应该从通常的消息开始,说它正在侦听哪个端口(例如5000).如果它是无头服务器,则应该可以使用curl来调用Web API:

    At this point Kestrel should start with the usual message saying what port it is listening on (say, 5000). If it is a headless server, you should be able to call you Web API using curl:

        curl http://localhost:5000/whatever/your/api/needs/here
    

    如果Ubuntu盒具有GUI(Gnome等),则应该可以使用浏览器连接到api.

    If the Ubuntu box has a GUI (Gnome etc) you should be able to connect to your api with a browser.

    如果您的Ubuntu服务器未运行防火墙,则应该能够使用来自同一网络中另一台计算机的浏览器连接到Web API:

    If your Ubuntu server is not running a firewall, you should be able to connect to the Web API with a browser from another machine on the same network:

        http://<linux-ip-address>:5000/whatever/your/api/needs/here
    

    您可以通过在终端窗口中键入ip addr show来获取Ubuntu服务器的IP地址.

    You can get the IP address of your Ubuntu server by typing ip addr show in a terminal window.

    • 管理防火墙取决于您的Linux发行版.如果服务器是公共服务器,则您实际上必须运行一个服务器并使用它来关闭对Kestrel服务的访问.
    • 设置Docker更为复杂,在此处添加太多内容.提出一个单独的问题,我将记录我所做的事情.
    • 请注意,当您在Windows上的IIS下运行时,会发生完全相同的事情:IIS将请求转发到端口5000或您指定的任何端口上的Kestrel.通常,将IIS配置为(通过发布生成的web.config文件)以在需要时启动Kestrel并保持其运行.您可以使用dotnet MyWebApi.dll在Windows上手动启动应用,然后将IIS配置为转发给它.
    • 学习时可以按照我描述的方式运行,但是对于生产而言,您需要定义API以作为Linux守护程序启动,并在崩溃时让Linux重新启动(Docker也可以为您执行此操作). IIS通常会为您解决这个问题.
    • Managing your firewall is dependent on your Linux distro. If the server is public, you really must run one and use it to shut down access to you Kestrel service.
    • Setting up Docker is more complicated, too much to add here. Ask a separate question and I will document what I have done.
    • Note that when you run under IIS on Windows, exactly the same thing is happening: IIS forwards the requests to Kestrel on port 5000 or whatever you specify. Typically IIS is configured (via the web.config file generated by your publish) to start Kestrel when it is needed and keep it running. You could start your app manually on Windows with dotnet MyWebApi.dll and configure IIS to forward to it.
    • Running as I've described is fine when learning, but for production you would need to define you API to start as a Linux daemon and have Linux restart it if it crashes (Docker can also do this for you). IIS generally takes care of this for you.

    这篇关于如何在Ubuntu上托管/发布.Net Core WebAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆