Introduction:
This concise step-by-step guide is designed for developers who need to quickly deploy an ASP.NET Core application on an Ubuntu AWS EC2 instance, leveraging MySQL for data persistence and Nginx as a reverse proxy.
Step 1: Preparing the AWS EC2 instance
- Login to your AWS Management Console.
- Navigate to the EC2 Dashboard, click on "Launch Instance".
- Choose "Ubuntu Server" as your Amazon Machine Image (AMI).
- Select an instance type that meets your requirements and click "Next".
- Configure instance details, add storage, and add tags as needed. Click "Next".
- Configure your security group to allow HTTP, HTTPS, and SSH traffic. Click "Review and Launch".
- Review your instance configurations, then click "Launch".
- Choose an existing key pair or create a new one, check the acknowledgement box, then click "Launch Instances".
Step 2: Assigning an Elastic IP and Configuring Security Groups
An Elastic IP is a static IPv4 address that ensures a reliable connection to your instance. AWS charges for Elastic IPs if they're unassigned, encouraging efficient use. Assigning an Elastic IP to your instance allows it to be accessible over the internet, even after restarts.
To assign an Elastic IP and allow ports 80 (HTTP) and 443 (HTTPS), follow these steps:
- Navigate to the VPC Dashboard in the AWS Management Console.
- Click on "Elastic IPs" in the left pane.
- Click "Allocate new address", then "Allocate".
- Select the new Elastic IP, click "Actions", and then "Associate IP address".
- Select your instance from the dropdown list and associate it.
Next, open ports 80 and 443 in the security group for your instance:
- In the AWS Management Console, navigate to EC2 Dashboard and select 'Security Groups' under 'Network & Security' in the left-hand menu.
- Select the security group associated with your instance.
- Click on the 'Inbound rules' tab, then click 'Edit inbound rules'.
- Click 'Add rule', select 'HTTP' for port 80, choose 'Anywhere' under 'source' and click 'Save rules'.
- Repeat step 9 but select 'HTTPS' for port 443.
Your instance now has a static public IPv4 address and is accessible via HTTP and HTTPS.
Step 3: Installing ASP.NET Core
- SSH into your EC2 instance.
- Update your package lists:
sudo apt-get update
- Install the necessary .NET packages:
sudo apt-get install -y apt-transport-https
- Download the Microsoft package feed:
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
- Install the package:
sudo dpkg -i packages-microsoft-prod.deb
- Update package lists again:
sudo apt-get update
- Install .NET SDK:
sudo apt-get install -y dotnet-sdk-5.0
Step 4: Installing MySQL
- Update your package list:
sudo apt-get update
- Install MySQL:
sudo apt-get install mysql-server
- Secure your MySQL installation:
sudo mysql_secure_installation
- Follow the prompts to configure MySQL.
Step 5: Setting up Nginx
Nginx will be our reverse proxy to forward requests to our ASP.NET Core application. This will also allow us to serve our application on port 80, which is the standard port for HTTP.
Follow these steps to install and configure Nginx:
Install Nginx with the following command:
sudo apt-get install nginx
Open the server block configuration file for editing:
sudo nano /etc/nginx/sites-available/default
Delete any existing content in the file and replace it with the following configuration. This configures Nginx to forward HTTP traffic on port 80 to the Kestrel server that runs the ASP.NET Core application:
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;
}
}
Test the Nginx configuration:
sudo nginx -t
If the configuration test is successful, restart Nginx to load the new configuration:
sudo systemctl restart nginx
Step 6: Creating and Setting up a Simple ASP.NET Core App with Database
In this step, we'll create a simple ASP.NET Core application that takes note input from a user and stores it in the MySQL database. This application will have a simple form to input a note's title and description.
Navigate to your desired directory and create a new ASP.NET Core MVC project:
cd /desired/directory/
dotnet new mvc -n NotesApp
cd NotesApp
Open the project file (NotesApp.csproj
) and add the following package references for Entity Framework Core and MySQL:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0" />
</ItemGroup>
Then, install the new packages:
dotnet restore
Create a new folder named "Models". Within that folder, create a new file named "Note.cs". Here's a basic model:
namespace NotesApp.Models
{
public class Note
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
Set up Entity Framework Core. Create a new file in the Models folder named "NotesContext.cs" and add the following:
using Microsoft.EntityFrameworkCore;
namespace NotesApp.Models
{
public class NotesContext : DbContext
{
public NotesContext(DbContextOptions<NotesContext> options) : base(options)
{
}
public DbSet<Note> Notes { get; set; }
}
}
Open the "Startup.cs" file and modify the ConfigureServices
method to add the database context:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<NotesContext>(options =>
options.UseMySql(Configuration.GetConnectionString("NotesContext"), new MySqlServerVersion(new Version(8, 0, 21))));
}
Add your connection string in "appsettings.json":
{
"ConnectionStrings": {
"NotesContext": "server=localhost;userid=root;password=your_password;database=NotesDB;"
},
// Rest of the file
}
Apply the database migration:
dotnet ef migrations add InitialCreate
dotnet ef database update
Finally, create a "Notes" controller and views:
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet tool install --global dotnet-aspnet-codegenerator
dotnet aspnet-codegenerator controller -name NotesController -m Note -dc NotesContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries
You can now run the application with:
dotnet run
Then, navigate to http://localhost:5000/Notes
to create and view notes. This is a basic implementation, and you may need to modify it as per your requirement. Also, replace placeholders like "your_password" with your actual data. The application runs on localhost:5000
, but in a real scenario, you'd need to configure the ASP.NET Core app to use the Nginx reverse proxy.
Conclusion
You now have a simple "Hello World" ASP.NET Core application running on an Ubuntu server on AWS EC2, backed by MySQL and proxied through Nginx. The Elastic IP ensures your instance is reachable at a fixed address. This guide assumes a basic understanding of AWS, Ubuntu, ASP.NET, MySQL, and Nginx.