Hosting your ASP.NET application on Azure Linux Hosting

4 minute read

Now that ASP.NET 5.0 (vNext) runs on .NET 2015 – a cross platform version of .NET – it is worthwhile hosting your web applications on Linux. So let's get started

Procuring a Linux VM

I chose procuring a Linux VM using my Azure Subscription and I quickly spawned a Standard Ubuntu server.

Now what you get as a VM is basic version of VM and you need to install .NET runtime on it.

 

Installing .NET Runtime on Linux VM

If you do not have PuTTY client, I would recommend downloading it as we will use this for executing all our commands on Linux. Using PuTTY, you can login using your SSH certificate or credentials (based on your VM configuration). First you need to validate if you have appropriate version of Mono installed by executing following command

mono --version

You will get an output like,

The minimum required version of Mono for ASP.NET 5.0 is 3.4.1 or later. So in this case, we do need to install a later version of Mono. You can execute following commands one by one on the VM

## Prerequisites

sudo apt-get install make
 sudo apt-get install git autoconf libtool automake build-essential mono-devel gettext zip mono-complete unzip
 sudo apt-get install bash zsh curl

## Get Mono Code and Compile it

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 would require around 45-60 minutes of time and would download latest source code of Mono from GitHub, compile and install it for you on your VM. The source code is huge and its compilation (command: make) will take time to execute. You can browse through different versions of Mono at - http://download.mono-project.com/sources/mono/

The .NET Framework on Windows uses the Windows Certificates store to check whether to accept an SSL certificate from a remote site. In Mono, there is no Windows Certificate store, it has its own store. By default, it is empty and we need to manage the entries ourselves.

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/

This would ask you if you want to import certificates to CA store and AddressBook store. Say Y when prompted. In total, 9 certificates will be imported.

Some of the certificates may not be trusted, so to avoid trust issues you need to execute

mozroots --import –sync

 

Installing K-Version Manager

Installing KVM is simple and is single line command

curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh & source ~/.kre/kvm/kvm.sh

Post this you have to execute following command to start kvm

source /home/PuneetGhanshani/.kre/kvm/kvm.sh

Now you can trigger another command to upgrade kvm

kvm upgrade

This command will download the specified version of the K Runtime Environment (KRE), and put it on your user profile ready to use.

After all the above steps are completed, you can execute below command to check the version of Mono installed

mono --version

You should get some output like,

Verify K Runtime Environment (KRE)

 

The KVM upgrade command will download K Runtime Environment (KRE) and put it on your user profile to use. So next is to verify if K has been installed correctly

Checking nuget.config file

 

In the /home/<<username>>/.config/NuGet folder, you will find nuget.config.
The content of files should appear as



 
 
 
 
 


Hosting your ASP.NET 5.0 Website

 

So for illustration, I am using ASP.NET 5.0 Starter Application which can be quickly created using VS 2015 and its solution structure looks like,

I need to send this code to my Linux VM for compilation. I use FileZilla to connect to FTP to my sites. So I created another folder on my VM /home/PuneetGhanshani/Puneet/aspnet where I will copy my solution code. Once the code has been copied to the VM, you need to ensure that you have the correct Nuget Packages which can be verified and updated using

kpm restore –s www.myget.org/F/aspnetvnext/

Before you run this command, make sure your project.json has following elements

"dependencies": {
    "Kestrel": "1.0.0-beta1",
 },
 "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
    "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5000"
 },

The command kestrel will not be available in KRE unless you have defined the Kestrel in project.json. If you haven't mentioned Kestrel, you will have to manually install it using following commands,

sudo apt-get install npm
npm install -g grunt-cli
npm install kestrel-server

Now you can compile the code using command

kpm build

With the build being successful, now we can host the application,

k kerstel

For a website that ran perfectly through Visual Studio, you may get an error

System.NullReferenceException: Object reference not set to an instance of an object at Microsoft.AspNet.Server.Kestrel.Networking.Libuv.loop_size ()
 [0x00000] in <filename unknown>:0 at
 Microsoft.AspNet.Server.Kestrel.Networking.UvLoopHandle.Init (Microsoft.AspNet.Server.Kestrel.Networking.Libuv uv) [0x00000] in <filename unknown>:0 at Microsoft.AspNet.Server.Kestrel.KestrelThread.ThreadStart (System.Object parameter) [0x00000] in <filename unknown>:0

You can enable tracing to get more details by executing command export KRE_TRACE=1

For Web Applications, we need KestrelHttpServer which is built on libuv library. So let's build libuv and get going,

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

And we run the same command k kerstel

The last bit, exposing endpoint on Azure

 

As per the project.json we have hosted this website on the port 5000 using kestrel, so we need to expose this endpoint to the users. Assuming that this VM will host only one website, we can setup a HTTP/TCP endpoint as

You are now set to browse the ASP.NET 5.0 application hosted on Linux

The sample source code for project.json and packages.config are available on GitHub

Updated: