Introduction
ASP.NET Core is an open-source and cross-platform framework for developing web applications and mobile backend. ASP.NET Core being cross-platform framework it provides flexibility to develop and run applications on Windows, Linux and Mac.
There are multiple ways to deploy application on AWS EC2 Linux Instance. I will post about AWS code pipeline which can be used to automate deployment process and install application on EC2 Instance some other day, for now we will focus on deploying ASP.NET core web application on Amazon Linux instance.
Step 1 - Build ASP.NET Core Application
Develop your own ASP .NET core Application from scratch. Alternatively, you can also use sample application on GitHub for this demo.
Step 2 - Provision EC2 Instance using Amazon Linux AMI
Provision a t2.micro instance using Amazon Linux AMI as it is eligible for Free Tier Usage for 12 months.
- Logon to AWS console and launch Amazon Linux EC 2 Instance
- Follow through steps on Launch Instance wizard and configure inbound rules to allow HTTP and SSH Protocol. Ensure while deploying real time production application you lock down security on SSH protocol to allow access from desired source only. Leaving source as 0.0.0.0/0 gives access to everyone.
- Create and download new Key Pair. In case you have already created key pair, make sure you have access to .pem file. You will need .pem in below steps to generate ppk file and logon to EC2 instance.
- Launch EC2 instance and note Public IP showed on EC2 Dashboard. You will need Public IP to SSH in to EC2 Instance.
Step 3 - Configure PuTTY to SSH in Linux EC2 Instance from Windows
- Download Putty and PuttyKeyGen from putty download page and configure PuTTY.
- Load downloaded pem file and click on save private key to generate private key (.ppk) file without passphrase.
- Specify generated ppk file path in PuTTY Configuration for Authenticating.
- Specify username and public IP to SSH EC2 instance User name will be ec2-user@<Public IP Address>.
Step 4 - Install Git, Apache and .NET Core on Linux EC2 Instance
- Elevate permission for installation using sudo su command:
- Run below commands to install Appache and Git:
yum install httpd
yum install git
- Execute below commands on terminal in sequence to download and Install .NET Core:
sudo yum install libunwind libicu
curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=835019
sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
sudo ln -s /opt/dotnet/dotnet /usr/local/bin
Refer to https://www.microsoft.com/net/core#linuxcentos steps described for installing .NET Core on CentOS 7.1 also work for Amazon Linux Instance.
- Exit from sudo mode and run dotnet command to verify the installation:
Step 5 - Configure Reverse Proxy so that All Incoming Requests are Passed to kestrel Server
- Elevate permissions to edit httpd.conf file and make below changes:
<VirtualHost *:80>
ProxyPass / https://127.0.0.1:5000/
ProxyPassReverse / https://127.0.0.1:5000/
</VirtualHost>
- Restart
httpd
service:
sudo service httpd restart
For more information on why we need reverse proxy, refer to https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction.
Step 6 - Compile and Launch .NET Core Application on Linux EC2 Instance
Execute the below command to compile and Launch application:
dotnet restore
dotnet run
Browse deployed application through public DNS name displayed on EC2 Dashboard. Sample Application display's OS, Frame and Environment variables set on EC2 Instance.
References