Nirav Kamani
Nirav Kamani

Reputation: 3272

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

I am learning .Net Core.

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

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?

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

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

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

Upvotes: 5

Views: 9964

Answers (3)

Peter
Peter

Reputation: 6035

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.

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.

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.

Hosting your API without Docker

  • 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. Setting up Nginx is covered in the article you referenced. As noted, not required just to start and test your 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.

Simple steps to get just you API running in a development setup

  • Ensure you are defining the appropriate runtime in you 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.
  • 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

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

  • 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.

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

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

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

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

Notes

  • 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.

Upvotes: 4

Frank
Frank

Reputation: 713

You can specify url/port like this (in your "Program.cs" file):

 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.

In your project directory just open a terminal/console window and run "dotnet run".

Make sure it says "Now listening on: 192.168.0.0:8080" (or the url/ip you put in).

The above example assumes you are using Startup for your startup class

Upvotes: 0

Pawel
Pawel

Reputation: 31610

Asp.NET Core application use a cross platform application web server called Kestrel. You can run your application with Kestrel directly (e.g. 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. You can find a sample nginx config here: https://github.com/aspnet/ServerTests/blob/dev/test/ServerComparison.FunctionalTests/nginx.conf

Upvotes: 1

Related Questions