Deploying .NET Core, Angular and Postgres Web Application on Ubuntu (nginx)

Introduction

In this post, I will be deploying and running a .NET Core, Angular and Postgres application on an Ubuntu VM running in azure cloud.

This involves different tasks in different technologies to be done and I’ve previously written few posts about these different topics. In this post, I will combine all those learning so you can see how these technologies work together.

I will show you all these steps, however, for details of some steps, I will be pointing you to other articles for more information.

Environment Setup

For hosting the application, we will need the following infrastructure:

  • Ubuntu Server
  • PostgreSQL Server
  • NGINX

We will install postgres and nginx on single ubuntu server to keep things simple.

Here are few of the articles which can help you in setting up this infrastructure:

I have all this infrastructure setup and next we will talk about the application.

Web Application

I will be reusing an existing application and you can choose your own application as well. The application is built using .NET Core and Angular. It uses Entity Framework Core for database concerns. You can learn more about application on this link.

Setting Up Database

Using psql command, We can check that there is no database currently setup for our application:

So let’s create a database:

Next, we need to create database tables, relationships etc. For this purpose we can use Entity Framework to generate a SQL script for us which we can run for the accountingbook database.

Open up a powershell window on your dev machine and execute following command to generate script:

dotnet ef migrations script --project AccountingBook.Data --startup-project AccountingBook.Web --idempotent --output ./AccountingBookDB.sql

here is the generate script file:

Now, we can copy this script to our ubuntu machine using scp command:

scp AccountingBookDB.sql jawadhasan80@13.80.248.51:/var/www

on remote ubuntu machine, I can execute following command to run the sql script:

sudo -i -u postgres psql -U postgres -d accountingbook -a -f "/var/www/AccountingBookDB.sql"

and we can check that database tables are created as well:

Or you can connect to postgres server using using azure data studio and execute this script remotely:

Build the Angular Application

We can build the angular application as follows:

npm install
npm run build --prod -aot

Publish the .NET Core Web application

Use following command to publish the application:

dotnet publish "AccountingBook.Web.csproj" -c Release -r ubuntu.16.04-x64

Copy the Published Application to Ubuntu machine

We can copy published app to ubuntu using following command:

scp -r /c/workspace/accountingapp jawadhasan80@13.80.248.51:/var/www/

cd to app directory, Set execute permissions and run the application:

chmod 777 ./AccountingBook.Web
./AccountingBook.Web

NGINX Configurations

So, for location / we are giving location of wwwroot folder of .NET Core web application. This way the angular contents will be served by nginx directly.

Then we added /api/ endpoint settings as shown above and here nginx and kestrel will work together to give the results.

Now, if we visit the application as shown below:

also we can get data from the API endpoints as well:

You can visit the application on http://13.80.248.51/

Summary

Using an Ubuntu VM is one of the easiest route to expose your application to internet. Setting up postgres and ngnix are also very straight forward with Ubuntu. We saw that we can use EF to generate sql script based on EF migration workflow and then we can simply copy it to ubuntu machine and execute it for database work. If you have some questions or comments, feel free to ask. Till next time, Happy Coding.

My Recent Books