mercredi 20 avril 2016

ASP.NET Core on a Linux environment




I will explain  some things related to use of ASP.NET Core Linux,  for the development of a project (environment setup) and for the deployment of an application. This article is based on the version  RC1 of ASP.NET Core tools,  given that APIs, including utilities in command lines can change in the last releases.  We will use Ubuntu 14.04 which is Linux distribution  supported by ASP.NET Core.

Basic configuration of the environment

Installing DNVM

To start configuring and running a first application ASP.NET Core Linux, the first step is to install the runtimes Manager, which will enable us later to install a specific version of the runtime, and finally execute our applications.
On Windows, this runtimes manager is in the simple form of a powershell script. In Linux, there is a dnvm.sh script you can just download. This installation is done through another script (dnvminstall.sh). Nevertheless, the use of that script requires prerequisites: utility unzip (used by the dnvminstall.sh script) and curl (used in the following command and the dnvminstall.sh script).
Run both commands below to get the functional dnvm order.
sudo apt-get install unzip curl
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
Now the DNVM command is usable. Make the test, simply type dnvm in the command prompt and the following result is displayed. However, note that with the previous command, the dnvm.sh script is only installed for the current user.

Installing DNX

sudo apt-get install libunwind8 gettext libssl-dev libcurl4-openssl-dev zlib1g libicu-dev uuid-dev
Once installed these dependencies, it is possible to use the DNVM command to install a version of the runtime. For example, the -r CoreCLR dnvm upgrade instruction will automatically get the latest version of the runtime suitable for .NET Core. Nevertheless, I recommend using the alternative below that allows you to explicitly specify the version of the runtime that you want to install.
dnvm install 1.0.0-rc1-update1 -r coreclr
The switch -r CoreCLR is necessary to clarify that the .NET runtime Core must be installed. Without this switch, the Mono runtime have to be installed. But Mono must also be installed on the server. In the development of your projects, support for .NET Core rather than Mono is more interesting, since it is developed and supported by Microsoft. CoreCLR the runtime for Linux is currently available in 64-bit. Using the -a switch x86 will throw automatically an error asking you to prioritize the use of 64-bit.

Installing libuv

The libuv is used by the HTTP Kestrel server. It is currently the only HTTP server used in an ASP.NET Core Linux. The following instructions will download the sources from the library, compile it and then place it in the charger dynamic system libraries (dlopen).
Note that installing libuv is a mandatory step to run an ASP.NET Core Linux. Without that, it will be impossible to start the application.

Starting a first project

For further, you must have an ASP.NET Core on your Linux machine. You can use either Yeoman (which can be installed with NPM) to generate a new project, and then transfer the sources of an existing application and developed in another environment (eg. Your Windows machine with Visual Studio).

Deployment and hosting solution

 Why use a reverse proxy?

Your ASP.NET Application Core carries its own HTTP server (Kestrel). Nevertheless, it is unusual to expose a single HTTP server to the public. In terms of production architecture, we prefer rather put a reverse proxy in front of the HTTP server and reverse proxy expose this to the public.
The interest of the reverse proxy is then to support anything that is not related to the performance of a piece of code. For example, serving static files, compress HTTP responses, ensuring support for SSL, etc.
Going to production ==> Need full featured web server,reverse proxy just like in IIS with HttpPlatformHandler so install Nginx.

Install Nginx

sudo apt-get install nginx
Once installed, Nginx is present in the form of a service. You then have the option to either restart the machine, either manually start the service.
sudo service nginx start

Nginx configuration

The configuration of the sites exposed by Nginx is done by editing the file in the /etc/nginx/sites-available / default. In my example, I have chosen to redirect site traffic on port 80 to port 5000. I added the crossing different headers to the HTTP request to take forward a Kestrel. Note that the Connection header must absolutely be present, otherwise the replies will not be returned by Kestrel.
Unix sockets are another form of inter-process communication. The operation is very simple, the HTTP server is not configured over a TCP port but compared to a path to a file. In application startup, the HTTP server will create this file, then the monitor. Nginx  will just write to that file every time an incoming request must be sent to the HTTP server.
In terms of configuration, the changes required are very simple. Just tell Nginx the file path to use as socket (via proxy_pass property).
Then, we must also change the commands Node project.json file of Core ASP.NET application. Indeed, until now, I was resting on the default port used by Kestrel (5000). However, I wish now that Kestrel uses the same socket unix as configured with Nginx. The server.urls option to easily override the configuration of Kestrel.

Application Management 

The supervisor application manages the instantiation and possibly automatic instantiation of our application in case of crash.
She moved through the command below.
sudo apt-get install supervisor
Then, the following content is to be placed in the file /etc/supervisor/conf.d/nerddinner.conf.

2 commentaires: