CoderJony

Hosting an ASP.NET Core Application on Amazon Linux 2 EC2 instance

In this post, we will understand how can we deploy an ASP.NET Core application on an Amazon Linux 2 EC2 instance step by step.

1. Create an Amazon Linux 2 EC2 instance

In the first step, you have to create an Amazon Linux 2 EC2 instance. Though, in this post, we won't focus much on creating a Linux EC2 instance step by step. But there are certain things that you must know while provisioning the EC2 instance.

1.1. Make sure you select the right AMI - Amazon Linux 2

Select the Amazon Linux 2 AMI as shown in the below picture. For CPU architecture, you can choose either x86 or ARM. In this post, I have chosen x86 as ARM instances were not available in t2 or t3 instance family.

image

1.2. Make sure, the Security Group allows access on ports 22 & 80

  • Port 22 will be used for SSH
  • Port 80 will be used by the Web Server

image

2. Install ASP.NET Core Runtime on Linux EC2

There are a lot of Linux distributions such as:

  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • openSUSE
  • SLES
  • RHEL
  • etc.

Microsoft has published documentation on how to install .NET Core on each of these distributions.

2.1. Determine Linux Distribution

To install the .NET Core runtime, you first have to check the Linux Distribution used by Amazon Linux 2.

Run the below command using SSH to check the Linux distribution.

cat /etc/os-release

image

As you can see, the above command outputs ID_LIKE property with centos rhel fedora value. Considering this AMI similar to CentOS, we can proceed further installing .NET Core on Linux CentOS distribution.

To know more about ID_LIKE property, please refer this link.

2.2. Installing ASP.NET Core Runtime on CentOS

In this step, we will be following the documentation from Microsoft website to install the .NET Runtime on CentOS.

Before you install .NET Core Runtime, run the following command. This will add the Microsoft package signing key to the list of trusted keys and add the Microsoft package repository.

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

Next, run the below command to install ASP.NET Core 6 Runtime.

sudo yum install aspnetcore-runtime-6.0

You can see below, that ASP.NET Core 6 Runtime has been installed successfully.

image

3. Create and publish a new ASP.NET Core application

Let's create a new sample application. Here, I am creating an MVC application.

image

While publishing, make sure the deployment mode is Framework dependent, and Target Runtime is linux-64.

image

4. Create .zip from published content and upload it to S3

Create a Zip file of the published content as shown in the below picture.

image

Upload this Zip file to S3 bucket.

image

5. Unzip the application files on Linux EC2

SSH to the EC2 and run the below commands to create a directory for your web application.

// go to root
cd /  

// create wwwroot directory, you can choose anyname instead of wwwroot
sudo mkdir wwwroot

// move to newly created directory
cd wwwroot

// create another directory inside wwwroot
sudo mkdir my-mvc-app

Create a pre-signed URL by selecting the zip file from the console.

image

Before you proceed further, make sure your current working directory is /wwwroot/my-mvc-app, as you will be downloading the zip file and unzipping it there only.

Run the below command to download the zip file in the current working directory.

sudo wget -O "PublishedWebApp.zip" "https://{pre-signed-url}"

Run the below command to unzip the published web app.

sudo unzip PublishedWebApp.zip

Run the below command to view unzipped files of the MVC web application.

ls

See the below screenshot, all the files are now present inside /wwwroot/my-mvc-app directory.

image

6. Running the application (without Linux Service)

In this step, you will be running the application without creating a linux service. Though, it is not the recommended way to host a Web Application without creating a service, as your application process will be lost if the linux instance restarts due to any reason. Creating a Linux Service for the application guarantees that a Web App will be up and running when the system re-starts.

Hit the below command to run the ASP.NET Core application on port 80.

sudo dotnet WebApplication1.dll --urls http://0.0.0.0:80

See the below screenshot.

image

Visit the public IP of EC2, you can see the app is up and running.

image

6. Running the application (with Linux Systemd Service)

Running a web-app as a Linux service ensures that web-app will be up and running always as the Linux service restarts automatically after a reboot or crash using systemd.

Create the service definition file:

sudo nano /etc/systemd/system/mymvcapp.service

You can find running Linux service under path /etc/systemd/system.

Next, copy the below content in this service file.

[Unit]
Description=Example of ASP.NET Core MVC App running on Amazon Linux

[Service]
WorkingDirectory=wwwroot/my-mvc-app
ExecStart=/usr/bin/dotnet /wwwroot/my-mvc-app/WebApplication1.dll -urls "http://0.0.0.0:80"
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=my-mvc-app
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

Now, start the service.

sudo systemctl enable mymvcapp.service
sudo systemctl start mymvcapp.service
sudo systemctl status mymvcapp.service

The above command will start the service, and the service will start the ASP.NET Core app with the Kestrel server that is listening to the port 80. You can now try accessing the app from public IP.

See below screenshot of the running active service.

image

To redeploy your app with the changes, you need to stop the service, replace the DLLs, and start the service again. You can use the following commands to stop and start the service again.

sudo systemctl stop mymvcapp.service
sudo systemctl start mymvcapp.service
sudo systemctl status mymvcapp.service

Conclusion

In this post, we understood how easily we can deploy an ASP.NET Core application on an Amazon Linux 2 EC2 instance. Please let me know your thoughts and feedback in the comment section below.

Thank You ❤️

Buy Me A Coffee
Bootstrap