简体   繁体   English

如何在Ubuntu上托管/发布我的.Net Core WebAPI?

[英]How do I host/publish my .Net Core WebAPI on Ubuntu?

I am learning .Net Core. 我正在学习.Net Core。

I have developed WebApplication using ASP.Net MVC and as it can be installed and run on Local IIS. 我使用ASP.Net MVC开发了WebApplication,因为它可以在本地IIS上安装和运行。

What's the similar way to Host / Publish .Net Core WebApi in Ubuntu and Linux instead of running on specific port like 5000? 在Ubuntu和Linux中托管/发布.Net Core WebApi的方式类似,而不是像5000这样的特定端口运行?

Is docker helpful for that context? docker对这种情况有帮助吗? If yes then how can I use it? 如果是,那我该如何使用它?

Is it possible to host / publish without docker? 是否可以在没有docker的情况下托管/发布? How can i Host / Publish without Docker? 如何在没有Docker的情况下主持/发布?

I also read following link and implemented all steps. 我还阅读了以下链接并实施了所有步骤。 Publish to a Linux Production Environment 发布到Linux生产环境

In above link i am unable to identify what will be the url to access webapi? 在上面的链接中,我无法确定访问webapi的URL是什么?

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. 正如@Pawel所指出的,托管.NET Core Web API或ASP.NET Core应用程序的推荐方法是使用Kestrel,它是内置于dotnet核心工具中的Web服务器。 For development purposes you do not need another webserver to start and test your api. 出于开发目的,您不需要另一个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来托管您的Web应用程序/ API ,但应将其视为生产托管,因为它是一种干净,快速的自动化版本和隔离进程的方法。

With Docker the process structure is the same - Docker just hosts and manages the processes. 使用Docker,流程结构是相同的 - Docker只托管和管理流程。 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. 您可以让Kestrel在一个Docker容器中运行API,而Nginx(在另一个容器中或安装在基本操作系统上)将调用转发给它。

Hosting your API without Docker 在没有Docker的情况下托管您的API

  • On Ubuntu, use either Nginx (or Apache) to provide your public HTTPS, and configure it to forward requests to your Kestrel server, which typically runs on port 5000. If your server is running a firewall, do not expose port 5000, but open port 443 (HTTPS) on that machine. 在Ubuntu上,使用Nginx(或Apache)提供公共HTTPS,并将其配置为将请求转发到您的Kestrel服务器,该服务器通常在端口5000上运行。如果您的服务器运行防火墙,请不要公开端口5000,而是打开该计算机上的端口443(HTTPS)。 Setting up Nginx is covered in the article you referenced. 您引用的文章中介绍了如何设置Nginx。 As noted, not required just to start and test your Web API. 如上所述,不仅仅需要启动和测试Web API。
  • Kestrel is fast but very simple - eg. 红隼很快但很简单 - 例如。 it does not support HTTPS (which you should use for a public API, because you will need authentication, and you can't authenticate securely without HTTPS. There are many other reasons to use Nginx/Apache over Kestrel - security, load balancing, reverse proxy capabilies etc. 它不支持HTTPS(您应该将其用于公共API,因为您需要身份验证,并且无法在没有HTTPS的情况下安全地进行身份验证。使用Nginx / Apache而不是Kestrel还有许多其他原因 - 安全性,负载平衡,反向代理能力等

Simple steps to get just you API running in a development setup 只需简单的步骤即可在开发设置中运行API

  • Ensure you are defining the appropriate runtime in you project.json 确保在project.json中定义适当的运行时

     "runtimes": { "win7-x64": {}, "win81-x64": {}, "ubuntu.14.04-x64": {}, "debian.8-x64": {} } 
  • Ensure that your project.json defines "emitEntryPoint": true in the buildOptions section. 确保project.json在buildOptions部分中定义"emitEntryPoint": true
  • Build your project for the platform you will deploy to: dotnet build -r ubuntu.14.04-x64 --build-profile Release 为将部署到的平台构建项目: 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 发布您的平台项目: dotnet publish -r ubuntu.14.04-x64 --configuration Release -o ./bin/Release/Publish

Use the command line as I've shown to build and publish your app for Ubuntu - I have tried in VS 2015 Update 3 and had problems getting it to build for the right runtime 使用命令行,如我所示,为Ubuntu构建和发布您的应用程序 - 我已经在VS 2015 Update 3中尝试过,并且在为正确的运行时构建它时遇到了问题

  • 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 . 将Publish文件夹中的文件复制到Ubuntu VM或服务器,并添加应用程序需要运行的任何文件,例如appsettings.json
  • Ensure that the appropriate .NET Core framework is installed on your Linux machine. 确保Linux计算机上安装了适当的.NET Core框架。
  • 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. 打开一个终端窗口, sudo -i获取管理员权限, cd到您放置二进制文件的文件夹并使用以下dotnet MyWebApi.dll运行您的api: dotnet MyWebApi.dll其中MyWebApi.dll是您构建过程的主要输出。

At this point Kestrel should start with the usual message saying what port it is listening on (say, 5000). 此时,Kestrel应该从通常的消息开始,说明它正在侦听的端口(例如,5000)。 If it is a headless server, you should be able to call you Web API using curl: 如果它是无头服务器,您应该能够使用curl调用Web API:

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

If the Ubuntu box has a GUI (Gnome etc) you should be able to connect to your api with a browser. 如果Ubuntu框具有GUI(Gnome等),您应该能够使用浏览器连接到您的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: 如果您的Ubuntu服务器没有运行防火墙,您应该能够使用同一网络上另一台计算机上的浏览器连接到Web API:

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

You can get the IP address of your Ubuntu server by typing ip addr show in a terminal window. 您可以通过在终端窗口中键入ip addr show来获取Ubuntu服务器的IP地址。

Notes 笔记

  • Managing your firewall is dependent on your Linux distro. 管理防火墙取决于您的Linux发行版。 If the server is public, you really must run one and use it to shut down access to you Kestrel service. 如果服务器是公共的,那么您必须运行一个并使用它来关闭对Kestrel服务的访问。
  • Setting up Docker is more complicated, too much to add here. 设置Docker更复杂,在这里添加太多了。 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. 请注意,当您在Windows上的IIS下运行时,发生的情况完全相同:IIS将请求转发到端口5000上的Kestrel或您指定的任何内容。 Typically IIS is configured (via the web.config file generated by your publish) to start Kestrel when it is needed and keep it running. 通常,IIS(通过您的发布生成的web.config文件)配置为在需要时启动Kestrel并使其保持运行。 You could start your app manually on Windows with dotnet MyWebApi.dll and configure IIS to forward to it. 您可以使用dotnet MyWebApi.dll在Windows上手动启动应用程序,并将IIS配置为转发给它。
  • 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). 正如我所描述的那样运行在学习时很好,但是对于生产,你需要定义你的API作为Linux守护进程启动并让Linux重新启动它如果它崩溃(Docker也可以为你做这个)。 IIS generally takes care of this for you. IIS通常会为您处理此问题。

Asp.NET Core application use a cross platform application web server called Kestrel. Asp.NET Core应用程序使用名为Kestrel的跨平台应用程序Web服务器。 You can run your application with Kestrel directly (eg using dotnet run - very useful during devlepment) however it's not recommended expose Kestrel directly to the outside world, so in a production environment you would put IIS in front of your application when running on Windows or nginx when running on Linux. 您可以直接使用Kestrel运行您的应用程序(例如使用dotnet run - 在开发期间非常有用)但是不建议将Kestrel直接暴露给外部世界,因此在生产环境中,当您在Windows上运行时,您会将IIS放在应用程序前面或nginx在Linux上运行时。 You can find a sample nginx config here: https://github.com/aspnet/ServerTests/blob/dev/test/ServerComparison.FunctionalTests/nginx.conf 你可以在这里找到一个示例nginx配置: https//github.com/aspnet/ServerTests/blob/dev/test/ServerComparison.FunctionalTests/nginx.conf

You can specify url/port like this (in your "Program.cs" file): 您可以像这样指定url / port(在“Program.cs”文件中):

 public static void Main(string[] args) {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseUrls("http://192.168.0.0:8080")
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }

Replace "192.168.0.0" with the actual ip or url in the UseUrls() method. 将“192.168.0.0”替换为UseUrls()方法中的实际ip或url。

In your project directory just open a terminal/console window and run "dotnet run". 在项目目录中,只需打开一个终端/控制台窗口并运行“dotnet run”。

Make sure it says "Now listening on: 192.168.0.0:8080" (or the url/ip you put in). 确保它显示“正在收听:192.168.0.0:8080”(或您输入的url / ip)。

The above example assumes you are using Startup for your startup class 上面的示例假设您正在为启动类使用Startup

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

相关问题 如何明确确保我的还原,构建和发布设置在.Net Core中是否一致? - How do I explicitly make sure my restore, build and publish settings are consistent in .Net Core? 如何在Azure上托管和部署ASP.Net Core 2.0 WebAPI? - How to Host and Deploy ASP.Net core 2.0 webapi on azure? 如何在 .NET Core 3.1 WebAPI 中托管 Angular 应用程序? - How to host an Angular app inside .NET Core 3.1 WebAPI? .NET Core WebAPI VueJS 模板发布问题 - .NET Core WebAPI VueJS template publish issue 我是否需要在ASP.NET Core WebAPI中调用TopicClient.CloseAsync()? - Do I need to call TopicClient.CloseAsync() in my ASP.NET Core WebAPI? 为什么我有 2 个类用于这个 .net 核心 webapi 端点? - Why do I have 2 classes for this .net core webapi endpoint? 如何在 .NET Core 5.0 WebAPI 解决方案中启用完整的 OData 功能? - How do I enable full OData functionality in .NET Core 5.0 WebAPI solution? 如何为将字符串数组作为参数的 .Net Core 3.1 webapi 服务调用定义路由? - How do I define the Route for a .Net Core 3.1 webapi service call which takes an array of strings as a parameter? 我如何将我的asp.net项目发布到我的本地iis? - how do i publish my asp.net project to my local iis? 如何在我的MVC和WebAPI中使用ASP.NET Identity? - How do I use ASP.NET Identity across my MVC and WebAPI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM