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:

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

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

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

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

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

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 the details and click Launch.

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 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.

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.

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

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

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

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.

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.

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.

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:

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:

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:

Comments: