Issue the following commands to create a new directory where we will create the ASP.NET 5 application. The first command (
mkdir
- make directory) is for creating a directory in Linux and the second command (
cd
- change directory) is for going inside the folder. And the last command is the command line to create a .NET Core application:
mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new
The following screen will be displayed:
This will create the .NET Core application, which has a couple of files—Program.cs
and project.json
. It's a bare minimum application that does not have even Startup
file.
We need to add the Kestrel HTTP Server
package as a dependency in project.json
. You can edit the file by issuing the command
vi project.json
. By default, the vi editor will open the file in read-only mode. You need to press Esc + I in order to make it to the edit mode. Add the line "Microsoft.AspNetCore.Server.Kestrel": "1.0.0" as shown in the following screenshot:
Press the Escape key and ":" and type wq
to write into and quit the vi
editor.
As we have added the dependency, we need to restore the packages by executing the following command:
dotnet restore
Once you enter this command, all the packages will be restored as shown in the following screenshot:
Create a new file, Startup.cs
, with the following content. You can create a new file by issuing the command
vi Startup.cs
. As usual, we need to press Esc + I to make the file in write and read mode. Paste the following content (you can paste it by right-clicking on the mouse after copying it from here):
using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; namespace aspnetcoreapp { public class Startup { public void Configure(IApplicationBuilder app) { app.Run(context => { return context.Response.WriteAsync("This is ASP.NET Core application running in Linux!"); }); } } }
Press Esc + :
and type wq
, to save the file. Update the Program.cs
file with the following content:
namespace aspnetcoreapp { public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseStartup<Startup>() .Build(); host.Run(); } } }
You'll see the following screen:
We have created the ASP.NET Core web application. Now we need to install Nginx, a reverse proxy server, which enables you to offload work such as serving static content, caching, and compressing requests. You can configure Nginx to listen on a particular port (we'll discuss the details later in this chapter). You can install Nginx by issuing the following command:
sudo apt-get install nginx
Once it is installed, you can issue the following command to start the service:
sudo service nginx start
When you'll run the command, you'll see the following screen: