Hosting Asp.net Core application on AWS Linux instance

How to Create Ubuntu instance on AWS and host Asp.net Core application on it


Category: ASP.NET Core Tags: ASP.NET Core 2, LINUX, AWS

Published files of project

    Most of .net developers are not much comfortable with Linux platform because developers had to code on windows and deploy their application on Windows Server but now things are changed and companies are moving towards Linux Servers. Hosting application on Linux may look little complex but it is not if you are familiar with basic commands. In this article, we will host application we created in our last article,  we will create a Linux instance on AWS and host our application on it.

Creating Ubuntu instance on AWS

    Ubuntu is Linux based operating system and to create instance on AWS we login to AWS management console and go to AWS Services -> All Services -> Compute -> EC2. Now click Button “Launch Instance” and you will see screen:

Selecting operating system for instance in AWS
Fig 1: Selecting operating system for instance in AWS

 

Once you search Ubuntu you will see different versions, select higher version.

Choose instance type in AWS
Fig 2: Choose instance type in AWS

 

You may select instances based on computing power and memory then click on Next.

Configure instance details in AWS
Fig 3: Configure instance details in AWS


In above screen, you may select any private network and subnet or leave it default and click Next.

Add Storage for instance in AWS
Fig 4: Add Storage for instance in AWS

 

I updated size of HDD storage to 32, you may change it as per you need and click Next.

Add Tags in AWS
Fig 5: Add Tags in AWS

 

I have given a name to my instance, you may add it or skip this. Now click Next.

Configure security group in AWS
Fig 6: Configure security group in AWS

 

By default only SSH will be enabled but we need to host our website so we need to allow HTTP port as well. RDP I just enable if I want to connect from Remote Device but in this tutorial, RDP is not required. Now click Next.

Review instance Details in AWS
Fig 7: Review instance Details in AWS

 

Review the details and click Launch.

Create key pair in AWS
Fig 8: Create key pair in AWS

 

Give key pair name and must download the key pair file, which will be named “myubuntu.pem” in my case. Launch the instance. Then in EC2 -> Instances tab you may see your instance as below:

Instance details tab in AWS
Fig 9: Instance details tab in AWS

 

Instance will be up and running with in few minutes, you could see public-ip, private-ip, domain name, availability zone etc. Once Instance state says running we can connect this instance through PUTTY.

Install PuTTY:

You may download and install PuTTY from https://www.putty.org/. Install and add environment variables If not added itself as shown in figure below.

Setting environment variables in Windows
Fig 10: Setting environment variables in Windows

 

Generating Private key from pem file

   After installing putty open PuTTYgen and load “myubuntu.pem” file (which was downloaded at the time of instance creation) and save the private key with name “myubuntu.ppk” somewhere.

PuTTY key Generator
Fig 11: PuTTY key Generator

 

Connect instance using PuTTY

    Open PuTTY and give hostname as “ubuntu@ip/dns”.

PuTTY configuration to make SSH connection
Fig 12: PuTTY configuration to make SSH connection

 

Expand SSH, click on Auth and load “myubuntu.ppk” file.

Loading ppk file to PuTTY configuration
Fig 13: Loading ppk file to PuTTY configuration


Now click Open and you will see command window connected to our Ubuntu instance.

SSH session using PuTTY
Fig 14: SSH session using PuTTY

 

Now we have access to our AWS instance and can perform any operations on it.

Install Nginx

    Once we are connected to our Ubuntu Server we have to install Nginx on it. Nginx is a web server like IIS which can host web applications. We will receive request through Nginx and pass it to Kestrel, means we will use Nginx as reverse proxy server. Kestrel actually process the request and sends response back to Nginx and Nginx sends it to client. We had enabled Kestrel in our project already. To install Nginx on Ubuntu we will run sequence of commands:

sudo apt-get update

sudo apt-get install nginx

sudo service nginx start

Once we run above commands, Nginx will be installed and stared, Now we just can copy public ip of our instance and hit it on browser. You will see Nginx startup page.

Nginx startup page
Fig 15: Nginx startup page

 

It means we are able to hit our web server from anywhere.

Install Asp.net Core on Ubuntu

    We need to install Asp.net Core 2.2 to run our web application. To install Asp.Net Core we will run below commands.

wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

sudo dpkg -i packages-microsoft-prod.deb

sudo add-apt-repository universe

sudo apt-get install apt-transport-https

sudo apt-get update

sudo apt-get install dotnet-sdk-2.2

Sequence of above six commands will install Asp.net Core 2.2 SDK on Ubuntu server, if you come across any trouble please refer Microsoft’s guidelines: https://dotnet.microsoft.com/download/linux-package-manager/ubuntu18-04/sdk-2.2.203

Once dotnet sdk is installed you can verify it by running command “dotnet”.

Configure Nginx

    Now we are done with installation of required packages and now we have to configure Nginx to work as reverse proxy. From last article we know we will host our application on http://localhost:5000 locally. So all the requests coming to Nginx need to be forwarded to http://localhost:5000.

Here we need to understand the flow, Nginx listens on public-ip of instance and Kestrel listens on http://localhost:5000 so we pass the requests to Kestrel from Nginx. Execute below command to edit default configuration file of Nginx.

sudo nano /etc/nginx/sites-available/default

Now default file will be in edit mode, replace location section of file with below configuration.

location / {

proxy_pass http://localhost:5000;

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection keep-alive;

proxy_set_header Host $host;

proxy_cache_bypass $http_upgrade;

}

We can see proxy_pass shall pass the each request to http://localhost:5000. Now save the file and to check if file has no error, run command:

sudo nginx -t

Now we can reload the configuration file with command:

sudo nginx -s reload

If now we hit public-ip of instance, we do not get nginx startup page instead we get 502 bad getway response. It is because we are routing requests to http://localhost:5000 but localhost is not yet setup and running.

Publish code Files

    Till this step we have all set with configuration and installation, now we just need to publish the code to server and run it. Now just for making it simple I’m going to publish code in local folder named “publish” of my windows machine. You can download source code from previous article.

Publish dotnet core application to local folder
Fig 16: Publish dotnet core application to local folder

 

Now files from “publish” folder to be uploaded on Ubuntu, first execute commands to see folders in Ubuntu machine:

cd /

ls

above commands will show you files and folders in root directory of Ubuntu.

Ubuntu directories inside root
Fig 17: Ubuntu directories inside root

 

Now create a folder called “hosting” in tmp folder, using commands:

cd /tmp

mkdir hosting

Now we will upload our published code files to hosting folder, and to do that open windows command prompt (not PuTTY session prompt) and execute below command:

pscp -r -i C:\Users\H288088\Desktop\myubuntu.ppk C:\Users\H288088\Desktop\publish\* ubuntu@18.224.228.98:/tmp/hosting

Here “C:\Users\H288088\Desktop\myubuntu.ppk” is private key location, “C:\Users\H288088\Desktop\publish\*” is for all files from published code folder (local windows machine) and “tmp/hosting” is location where you want to publish the code(Ubuntu machine). Below is the output of command prompt:

Uploading files to Ubuntu from Windows using pscp
Fig 18: Uploading files to Ubuntu from Windows using pscp

 

Run the application

    Now we are all set, just need to run the code. Open PuTTY session prompt and execute below commands:

cd /tmp/hosting

dotnet CoreAppSample.dll

Above command will run the application and you will see output in console:

Running Asp.net Core application in Ubuntu
Fig 19: Running Asp.net Core application in Ubuntu


We can see application is listening on http://localhost:5000 inside the Ubuntu, If we hit public-ip of instance then we will see our website running:

Output of Asp.net Core application
Fig 22: Output of Asp.net Core application

 


Like 0 People
Last modified on 18 May 2019
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 133
Questions: 9
Given Best Solutions: 9 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x