Wednesday, 17 December 2014

Hosting Asp.net vNext application on Linux server on Azure

Note: This post is outdated and not applied to current version of Asp.net DNX version. 
In this tutorial we’ll see how to host Asp.net vNext application on Linux server image running on Azure. In order to follow the steps firstly you need to have Microsoft Azure account. The supported Linux version is Ubuntu snappy.
Login to the http://portal.azure.com/ to access your Azure account. In this article I’m using the newly launched azure portal which is having blades to support multi-view in one window. We’ll start with creating a new Linux VM on Azure and then configuring it for Asp.net vNext hosting.
clip_image002
Step 1: Click on New and select Ubuntu
clip_image003
Step 2: Specify the required details for the new Linux server details.
clip_image004
If all details are correct and filled the Create button will get enabled. Click on Create the VM will start initializing, allocating respective resource for the Linux VM. This process will take 10-15 minutes to finish.
image
You’ll get something similar view of created Linux machine. If you click on Settings then Properties you can get the DomainName with other details. This domain name will be used as machine name to connect via remote client.
Step 3: Once the VM is created, Next step would be to connect with the Linux server. If you’re on windows you can use the Putty tool to connect to server console. Here are few quick steps to connect to the Linux VM.
i) Launch the Putty tool and enter the domain name of the created machine.
image
ii) Once you click open, if the domain name is valid a command window will open asking for Username and password. Enter the details and Linux machine will be connected and ready to receive the commands.
clip_image009
Step 4: Now you’re connected with the Linux server. To get the server ready now we need to install components to have the .Net core installed on the server. For Linux machine the .Net core is supplied with in MONO project from Xamarin and contributors. To check if the Mono is installed run following command.
 

Mono --Version 

clip_image011
Ofcourse the Mono is not installed on the server so we need to install it. Run following commands to install the Mono.
Note:- You can try downloading the mono-runtime with suggested command. But I’m not sure if that will give the fully featured Mono on the Linux. I haven’t tested it.

Downloading required tools:



 

sudo apt-get install make 

sudo apt-get install git autoconf libtool automake build-essential mono-devel 

gettext zip mono-complete unzip 


Downloading Mono and compile it. Run following command in order to get the latest Mono version ready to Install.

 

PREFIX=/usr/local 

PATH=$PREFIX/bin:$PATH 

VERSION=3.10.0 

wget http://download.mono-project.com/sources/mono/mono-$VERSION.tar.bz2 

tar -xjvf mono-$VERSION.tar.bz2 

cd mono-$VERSION 

./autogen.sh --prefix=$PREFIX 

make 

make install 

These steps will take more than 30 minutes to downloading, compile and Install the Mono.
Now we have to install few more components like NPM, Git, KVM, and K Runtime with Kestrel server. Before download and installing these components few ssl certificated would be required to add to the certificate store of the machine. With this step the downloading of required Nugets will not take place and will give missing certificate error.

Installing certificates:



 

sudo certmgr -ssl -m https://go.microsoft.com 

sudo certmgr -ssl -m https://nugetgallery.blob.core.windows.net 

sudo certmgr -ssl -m https://nuget.org 

sudo certmgr -ssl -m https://www.myget.org/F/aspnetvnext/ 


To avoid few certificated which falls under untrusted certificates run following command:

 

mozroots --import –sync 

Installing KVM on server 


Run following commands in order to download and install the Kvm.

 

curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh 

| sh & source ~/.kre/kvm/kvm.sh 

source /home/punitganshani/.kre/kvm/kvm.sh 

kvm upgrade 


Now check the Mono version again:

 

Mono –Version 

clip_image013
Note: If you don’t see the version 3.10.0 of Mono then repeat the above steps for installing the Kvm again. Because only this version is supported for Kestrel server that hosts the Asp.net vNext application.
Now to verify if the K Runtime is installed Run “k”.
clip_image015
Installing Kestrel server
To install the Kestrel server run following commands:

 

sudo apt-get install npm 

npm install -g grunt-cli 

npm install kestrel-server 


Now we need a sample application so that we can test the installed components. We’ll be using the Asp.net starter application from github.
Run command:

 

git clone http://www.github.com/aspnet/home.git 

clip_image016
Navigate to the following directory hierarchy:
Home -> samples -> HelloWeb
After navigating run following command:

 

kpm restore 

 

kpm build 


If there’s no error in restoring nuget packages and building the application then application is ready to Host.
Run following command to host the application in Kestrel server:

 

k kestrel 

You might get following error:
System.NullReferenceException: Object reference not set to an instance of an 
object at 
Microsoft.AspNet.Server.Kestrel.Networking.<strong>Libuv</strong>.loop_size 
() 
[0x00000] in &lt;filename unknown&gt;:0 at 
Microsoft.AspNet.Server.Kestrel.Networking.UvLoopHandle.Init 
(Microsoft.AspNet.Server.Kestrel.Networking.Libuv uv) [0x00000] in 
&lt;filename unknown&gt;:0 at 
Microsoft.AspNet.Server.Kestrel.KestrelThread.ThreadStart (System.Object 
parameter) [0x00000] in &lt;filename unknown&gt;:0 



For Web Applications, we need KestrelHttpServer which is built on libuv library.

 

wget http://dist.libuv.org/dist/v1.0.0-rc1/libuv-v1.0.0-rc1.tar.gz 

tar -xvf libuv-v1.0.0-rc1.tar.gz 

cd libuv-v1.0.0-rc1/ 

./gyp_uv.py -f make -Duv_library=shared_library 

make -C out 

sudo cp out/Debug/lib.target/libuv.so /usr/lib/libuv.so.1.0.0-rc1 

sudo ln -s libuv.so.1.0.0-rc1 /usr/lib/libuv.so.1 





Now runt the command again:

 

k kestrel 

It should say “Started” at this time.
clip_image017
The application is hosted at localhost:5004 (default port for Kestrel server). It can be changed from project.config file.

 


Exposing hosted website:


Now to expose the hosted site to external world last thing is to add an Endpoint on your Linux machine via Azure portal. To add the Endpoint go back to the Azure portal select the Linux VM -> Click Settings -> Endpoints -> Add.
clip_image019
Fill the details. If all the End points details are valid, click Ok. Now your machine is ready to show the hosted Asp.net vNext starter application to external world.
Now open browser on local machine. Goto http://<your domain name>
e.g. http://testlinux-12345c.cloudapp.net/
… And you’ll your hosted started application.
image



























































No comments:

Post a Comment