[英]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(在另一个容器中或安装在基本操作系统上)将调用转发给它。
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": {} }
"emitEntryPoint": true
in the buildOptions
section. 确保project.json在buildOptions
部分中定义"emitEntryPoint": true
。 dotnet build -r ubuntu.14.04-x64 --build-profile Release
为将部署到的平台构建项目: dotnet build -r ubuntu.14.04-x64 --build-profile Release
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中尝试过,并且在为正确的运行时构建它时遇到了问题
appsettings.json
. 将Publish文件夹中的文件复制到Ubuntu VM或服务器,并添加应用程序需要运行的任何文件,例如appsettings.json
。 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地址。
dotnet MyWebApi.dll
and configure IIS to forward to it. 您可以使用dotnet MyWebApi.dll
在Windows上手动启动应用程序,并将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.