AWS  

How to Deploy a .NET Application on AWS EC2 Step by Step

Introduction

Deploying a .NET application on AWS EC2 is one of the most practical and widely used approaches for hosting scalable web applications. Amazon EC2 (Elastic Compute Cloud) allows you to run virtual servers in the cloud where you can host your ASP.NET Core applications.

In this step-by-step guide, you will learn how to deploy a .NET application on AWS EC2 using simple language. This article is perfect for beginners and developers who want to move their application from local development to a live production environment.

What is AWS EC2?

AWS EC2 (Elastic Compute Cloud) is a cloud service that provides virtual machines (instances) where you can install software and run applications.

Key benefits:

  • Full control over server

  • Scalable infrastructure

  • Pay-as-you-go pricing

  • Supports Linux and Windows

Prerequisites

Before starting, make sure you have:

  • AWS account

  • Basic knowledge of .NET / ASP.NET Core

  • .NET SDK installed locally

  • Your application ready for deployment

Step 1: Create an EC2 Instance

  1. Login to AWS Console

  2. Go to EC2 Dashboard

  3. Click "Launch Instance"

  4. Choose OS:

    • Amazon Linux (recommended)

    • Ubuntu

    • Windows Server

  5. Choose instance type:

    • t2.micro (Free tier eligible)

  6. Configure key pair (important for SSH login)

  7. Configure security group:

    • Allow SSH (Port 22)

    • Allow HTTP (Port 80)

    • Allow HTTPS (Port 443)

  8. Launch instance

Step 2: Connect to EC2 Instance

Using SSH (for Linux):

ssh -i your-key.pem ec2-user@your-ec2-public-ip

For Ubuntu:

ssh -i your-key.pem ubuntu@your-ec2-public-ip

Step 3: Install .NET Runtime on EC2

Update system:

sudo apt update
sudo apt upgrade -y

Install .NET SDK:

wget https://dot.net/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel LTS

Verify installation:

dotnet --version

Step 4: Publish .NET Application Locally

On your local machine, run:

dotnet publish -c Release -o ./publish

This creates optimized files for deployment.

Step 5: Upload Files to EC2

Use SCP to upload files:

scp -i your-key.pem -r ./publish ec2-user@your-ec2-ip:/home/ec2-user/

Step 6: Run Application on EC2

SSH into EC2 and run:

cd publish
dotnet YourApp.dll

Now your app is running, but only accessible internally.

Step 7: Configure Reverse Proxy (Nginx)

Install Nginx:

sudo apt install nginx -y

Start Nginx:

sudo systemctl start nginx

Configure Nginx:

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

Add configuration:

server {
    listen 80;

    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;
    }
}

Restart Nginx:

sudo systemctl restart nginx

Step 8: Run App as Background Service (Systemd)

Create service file:

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

Add:

[Unit]
Description=My .NET App

[Service]
WorkingDirectory=/home/ec2-user/publish
ExecStart=/usr/bin/dotnet /home/ec2-user/publish/YourApp.dll
Restart=always
User=ec2-user

[Install]
WantedBy=multi-user.target

Enable service:

sudo systemctl enable myapp
sudo systemctl start myapp

Step 9: Access Your Application

Open browser:

http://your-ec2-public-ip

Your .NET application should now be live.

Optional: Enable HTTPS (SSL)

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Run:

sudo certbot --nginx

Follow instructions to enable HTTPS.

Common Issues and Fixes

  • App not loading → Check port and Nginx config

  • Permission issues → Check file ownership

  • Service not starting → Check logs using:

journalctl -u myapp

Best Practices for Deployment

  • Use environment variables for secrets

  • Enable logging and monitoring

  • Use auto-scaling for production

  • Keep instance updated

  • Use load balancer for high traffic

Real-World Deployment Flow

  • Developer publishes app

  • Uploads to EC2

  • Configures server

  • Runs app using systemd

  • Uses Nginx for routing

  • Enables HTTPS

Summary

Deploying a .NET application on AWS EC2 involves creating a server, installing the .NET runtime, uploading your application, configuring a reverse proxy, and running the app as a background service. By following these steps, you can host your ASP.NET Core application securely and efficiently on AWS cloud infrastructure.