Hosting .Net Core on Linux(Ubuntu) Amazon AWS Lightsail with MySQL - A Complete Guide

As .Net Core is an open-source, cross-platform framework we don't need to rely on windows (IIS) hosting platform. Now we have the option to host it on Linux using either Nginx or Apache as a webserver. This post is a complete guide to hosting .net core web application on Amazon AWS Lightsail Linux (Ubuntu) instance with managed MYSQL as a backend database.
There are many popular cloud hosting platforms that provide Linux(Ubuntu) hosting from as cheap as $5. I'm going to use Amazon AWS Lightsail ubuntu with managed MYSQL as a backend for this guide.
.Net Core + Linux(Ubuntu) + Nginx + Amazon AWS Lightsail + MySQL
Hosting .Net Core on Linux(Ubuntu) Amazon AWS Lightsail with MySQL - A Complete Guide
1) Create Linux(Ubuntu) Instance in Amazon AWS Lightsail

- Signup/Login to Lightsail then click on Create Instance.
- You can set up a free trial instance for 30 days. or select an instance of your choice. Give a name to your instance. The screen also has an option to upload your own SSH key. I'm going to leave for default one which is generated by AWS.
- Click on Create Instance. Your Lightsail ubuntu instance will be ready in a minute or two.
- Create Static IP: Lightsail instance comes with free static/public IP address. Attaching the server with static IP helps in DNS configuration. Also, using that IP adress the website can be directly accessed from anywhere.
- Go to the Network tab and click on Create Static IP
- Select the location same as Linux box
- Attach the instance created above
- Give a name to the static IP
- Click Create
2) Create MySQL Managed Instance

- Navigate to the Database tab then click on Create Database
- Select the AWS region same as one selected while creating the Ubuntu box.
- Select the My SQL Version
- Provide username and password for DB instance or leave it to the default username dbmaster and lightsail auto generates a strong password for you.
- Give a name to MySqlL instance then click Create Database.
3) Setup .Net Core Project with MySql
As our AWS instance is ready, lets setup .net core project to use MySQL database
- First, open MySql workbench in your local system.
If you don't have MySql, download and install it from here - In MySql, workbench window click on create a new connection (+ icon), then give lightsail MySql database instance credentials
- Once the connection is created, open your database instance and create a new schema (database)
- Open .net core project either in Visual Studio or VS Code and install MySql NuGet package.
From visual studio package manager console, runInstall-Package MySql.Data.EntityFrameworkCore
Install-Package MySql.Data.EntityFrameworkCore.Design
If you are using vs code run below command from the .Net CLIdotnet add package MySql.Data.EntityFrameworkCore
dotnet add package MySql.Data.EntityFrameworkCore.Design
- Open appsettings.json file and enter the connection string
"ConnectionStrings": { "dbconnection": "server=<aws_server_name>;port=3306;database=<database>;User=<db_user>;Password=<my_sql_password>" }
- Open Startup.cs, in the configure section, enter below code
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseMySQL(Configuration.GetConnectionString("dbconnection"))); }
- With the above setup in place, the project is now connected to MySQL. Let's run the migration commands to migrate user tables.
- Delete the default migration folder which comes with the template, as it is created for MS SQL. Create a new migration and update the database.
//from visual studio Add-Migration [migration-name] Update-Database
//from .net cli dotnet ef migrations add [migration-name] dotnet ef database update
- Create a publishing profile with the following options and publish code to the local directory
- Target Machine: Linux-x64
- Deployment Mode: Self-Contained
4) Hosting .Net Core in Nginx
As we have published the code to the local folder in the previous section. Next step is to set up the Nginx and then transfer the code to Ubuntu Nginx hosting directory.
- Login to the Lightsail website from browser and open Ubuntu terminal
- Install .Net Core runtime using this link. Step to install for deferent Linux distros are also given the same link.
-
Install Nginx -
sudo apt update sudo apt install nginx
-
Start webserver
sudo service nginx start
-
Verify that Nginx running by browsing the public IP address created earlier from your local system.
You should see the default Nginx screen saying "Welcome to Nginx" - Create folder for project in Nginx directory /var/www/html/ directory
-sudo mkdir /var/www/html/<project_name>
-
Give write permission to non-root user - Ubuntu
This is needed to access your file using FTP clientscd /var/www/html/<project_name> sudo chown ubuntu <project_name>
- Copy the published project file to Nginx folder using any FTP client like WinSCP
- Nginx Configuration: Nginx works as a reverse proxy server with Kestrel.
Edit the Nginx config file
sudo nano /etc/nginx/sites-available/default
server { listen 80 default_server; return 444; } server { listen 80; #Once site starts working with IP, replace it with your domain name.
server_name <Static_IP>; 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; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
sudo nginx -t
- Our Nginx is ready, Configure Kestrel server to run as service.
Create a service definition file using below commandsudo nano /etc/systemd/system/<kestrel_project_name>.service
- Put below code in the service definition file. Change project_name and assembly_name.
[Unit] Description=Example .NET Core Web App running on Ubuntu [Service] WorkingDirectory=/var/www/<project-name> ExecStart=/usr/bin/dotnet /var/www/<project_name>/<app_assembly>.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target
- Save the file and Enable the service
sudo systemctl enable <kestrel_project_name>.service
- Restart the service
sudo systemctl restart <kestrel_project_name>.service
Next step is to -
- Remove IP address from Nginx config file and replace with your domain name.
- Configure DSN recording using the public IP
- Generate and Add SSL certificate