Deploy ASP.NET Core Web App to Ubuntu Server Part 1 — The Basic

This is the first part of the three-part series that touches on deploying an ASP.NET Core Razor Pages web application to Ubuntu Server. In this series I will cover the various methods to make deploy and finally advancing to continuous integration and continuous deployment (CICD) practices.
DigitalOcean was used to create a virtual private server (droplet)

Recently I have created a new ASP.NET Core Razor Pages web application that uses SignalR to achieve real-time text-based chat. It was fun but merely testing it on localhost is kind of lonely. Therefore soon after creating the project, I was determined to quickly deploy it to a remote server so that I can let my friends playing it around together with me. In this post I will talk about deploying the newly created web application to a remote server in the most simple way.

So here are a few steps that I needed to carry out to have a running server that ended up serving the web application. The list of the steps goes as follows:

  1. Creating a new and blank server on DigitalOcean
  2. Setting up the proper environment and .NET Core runtime
  3. Installing and setting up NGINX for reverse proxy
  4. Installing PM2 to manage the process

Ubuntu Server

The first step towards getting a ready-to-work server is of course to create a brand new instance of a server. Fortunately I had a DigitalOcean (this is also a referral link of mine that gives you additional 100 dollar worth of credit upon signing up) account that allowed me to spin up a new server in just a few clicks. Ubuntu Server 18.04 was chosen and I included a couple of my SSH public keys during the process.

After waiting for roughly less than a minute, the server was ready with a designated IP address. Using the Terminal, I could access the server through SSH since the server would already have the corresponding public key:

ssh [email protected]<IP address>

Notice the root user which was created by default. This is the user that has the full control over the server and therefore is unsafe to be used — a new user should be created.

Using the command line interface in the server, I could run the several commands below to create this new user and its folder, a folder to store SSH authorised keys, and allow the newly created user to use sudo commands whenever necessary.

adduser <new user>
mkdir /home/<new user>/.ssh
cp .ssh/authorized_keys /home/<new user>/.ssh/authorized_keys
chmod 700 /home/<new user>/.ssh
chmod 600 /home/<new user>/.ssh/authorized_keys
chown <new user>:<new user> /home/<new user> -R
usermod -a -G sudo <new user>

Now I could switch to the newly created user to set up the firewall

sudo ufw allow 22 #do not forget about this port as SSH uses this port
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

These commands allow port 22, 80, and 443 to be available to communicate to outside world. After this, the ASP.NET Core runtime is needed for the web application to run. The detailed installation procedures are documented here.

Transfer Files to Server

I simply used the secured transfer protocol (SFTP) to copy the files that were needed to run the ASP.NET Core web application to the server.

sftp <new user>@<IP address>
put <path to directory that contains published files> <path to directory on server> -r

NGINX Reverse Proxy

NGINX is a powerful reverse proxy that is very popular. The configuration is easy enough to be done in a few minutes. Ubuntu package manager can be used to install NGINX

sudo apt install nginx

Next I needed to configure the NGINX to serve the web application by directing the request to the port 5000, which is the port used by the web application. This can be done by creating a custom configuration file (custom.conf) to avoid writing directly on the main configuration file.

touch /etc/nginx/conf.d/custom.conf

I named this configuration file custom.conf and opened it via editor to add the following instructions:

server {
  listen  80;
  server_name <ip address>;
	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;

I certainly did not forget the test the NGINX to ensure the configuration was working before restarting the NGINX service

sudo nginx -t
sudo systemctl restart nginx

At this step, I could run the web app by the command

dotnet <project name>.dll

And when I navigated to the IP address on web browser, I could see the app running properly.

Manage Process With PM2

One thing to note until this point was that the web application was running only as long as the Terminal window was open. The process would be stopped as soon as I exited from the SSH or closed the Terminal window. Time to bring in a very useful process manager — PM2.

PM2, although originally designed for node.js application, works well with ASP.NET Core as well. I installed it through node package manager and started it through simple commands:

sudo apt install npm
sudo npm install pm2 -g
pm2 start 'dotnet <project name>.dll' --name <project name>

Now I finally had an ASP.NET Core web app that ran continuously on Ubuntu Server.

What I Have Achieved so Far

At this point I had successfully created a Ubuntu Server, created a new user, set up the firewall, and installed NGINX. After installing .NET Core runtime and transferred the necessary files, I could see the web application running with the help of PM2. However, this process was rather tedious and manual, as I could see that in the subsequent deployments down the road, I would need to make sure I clear the directory before deploying new files to a avoid conflicts. In the next post, I will discuss how I used docker to achieve the same deployment.

One thought on “Deploy ASP.NET Core Web App to Ubuntu Server Part 1 — The Basic”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s