Apr 1, 2018

Runnning .NET Core app on Amazon EC2, part 1

Recently, Amazon announced a new AMIs with .NET Core, based on Amazon Linux 2 and Ubuntu. Those come pre-configured with .NET Core 2.0, PowerShell Core 6.0, and the AWS CLI.
I was curious, how hard it is to use them to host web applications built using .NET Core?
The thing with .NET Core web apps is that you need a reverse proxy to serve the app, which runs in Kestrel. In this part I will be using Nginx as a reverse proxy and will try to do that with both Amazon Linux 2 and Ubuntu.



The plan for each OS is:
  1. spin up EC2 instance (don't forget that security group should allow SSH and HTTP)
  2. install and configure Nginx
  3. create and run .NET Core web application

Install Nginx on Amazon Linux 2

It appeared that yum cannot install Nginx, since it cannot find it in repositories he has by default. After short googling, I came up with the decision to add the following repo:
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(rpm -E '%{rhel}').noarch.rpm
After that, it possible to use yum to install Nginx:
sudo yum install -y nginx
Add to boot sequence:
sudo systemctl enable nginx
Start Nginx service:
sudo systemctl start nginx
And now it is up and running (check out in browser).

Install Nginx on Ubuntu

Easy:
sudo apt-get update
sudo apt-get install -y nginx
Add to boot sequence:
sudo systemctl enable nginx
Start Nginx service:
sudo systemctl start nginx

Nginx configuration

You need to add reverse proxy configuration for your domain:
sudo nano /etc/nginx/conf.d/default.conf
I used this:
server {
    listen        80;
    server_name   yourdomain *.yourdomain;
    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 $http_host;
        proxy_cache_bypass $http_upgrade;
    }
}
and that required a small change in Nginx config to allow such a long host names, since 'yourdomain' would typically resemble something like 'ec2-34-244-132-120.eu-west-1.compute.amazonaws.com'. Open config:
sudo nano /etc/nginx/nginx.conf
and add\change the option in http section (not necessarily 1000 - use whatever works for you):
server_names_hash_bucket_size 1000;
Check Nginx configs:
sudo nginx -t
Restart Nginx:
sudo nginx -s reload

Create and run .NET app

Creating net core app is easy:
sudo mkdir /var/netcoreapp
cd /var/netcoreapp
sudo dotnet new mvc
Now let's damonize the web app. Publish it:
sudo dotnet publish --configuration Release --output bin
And create deamon config for it:
sudo nano /lib/systemd/system/netcoreapp.service
with the content as follows:
[Unit]
Description=.NET Core App
After=network.target

[Service]
WorkingDirectory=/var/netcoreapp
ExecStart=/usr/bin/dotnet /var/netcoreapp/bin/netcoreapp.dll
Restart=on-failure

SyslogIdentifier=netcoreapp
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target
Next, reload deamons:
sudo systemctl daemon-reload
Enable new deamon and start it:
sudo systemctl enable netcoreapp
sudo systemctl start netcoreapp
Checking status is easy:
systemctl status netcoreapp
If the deamon started successfully, open browser - your app is already running!

8 comments:

vcube software soltions said...

YOU HAVE SHARED BEST VALID INFO,KEEP POSTING.
<a href="https://www.vcubesoftsolutions.com/power-bi-training-in-hyderabad/>power Bi in kphb</a>

software course said...

Really awesome blog your blog is really usefull,keep posting.
python full stack in kphb

kosmik said...

I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me.devops training in hyderabadkeep sharing.

lokeshvcube said...
This comment has been removed by the author.
vcubelokesh said...


nice blog keep posting!
Your blog always provides a fresh perspective and inspires meaningful conversations
web Designing training near me

lokeshvcube said...

power bi institutes in hyderabad

kosmik said...

Really awesome blog your blog is really usefull,keep posting.
devops-training-in-hyderabad

Sugar Daddy New South Memphis said...

Veery nice blog you have here