How to deploy ASP.NET Core 3.1 app to Digital Ocean Linux droplet

📅 Published 08/10/2020

I have a new blogpost on auto deployment an ASP.NET 6.0 application from GitHub to an Digital Ocean App.

You can find the post here

In this tutorial we will deploy and host a simple ASP.NET Core 3.1 demo app on Ubuntu 18.04 droplet in Digital Ocean.

You can download or clone the demo app from my GitHub, or you can deploy your own ASP.NET Core 3.1 application. Just keep in my mind that this demo app doesn't make use of a database. So in this tutorial we don't setup a database. Maybe I'll write another tutorial about implementing an MySql server.

We will break this up in different steps:

1. Create Digital Ocean Ubuntu droplet

2. Create a new user in Ubuntu

3. Install .NET SDK in Ubuntu

4. Deploy our ASP.NET Core 3.1 app to Ubuntu

5. Install and configure NGINX in Ubuntu

Create Digital Ocean Ubuntu droplet

Create Digital Ocean account

First of all, you'll need a Digital Ocean account. If you haven't an DO account than you can create an account with this referral link and you'll get $100 credits to spend in the next 60 days.

Create Digital Ocean project

Once you're logged in, create a new project by clicking on the + New Project link:

digital-ocean-create-project 01

In the next screen you'll need to enter an name, description and tell DO what want to do with the project.

digital ocean create project 02

When you're done, click on the Create Project button.

digital ocean create project 03

On the next screen click on the Skip for now link below.

Now it's time to create our droplet.

digital ocean create project 04

Click on the Get Started with a Droplet button

Create Digital Ocean droplet

Now it's time to create a droplet!
First we have to choose an image distribution, in our case we choose Ubuntu 18.04.3 (LTS) x64:

digital ocean create droplet 01

We choose the Basic plan for $5/mo (scroll to the left):

digital ocean create droplet 02
digital ocean create droplet 03

We don't need block storage, so we'll skip that.

Choose a data center region close to your hometown, in my case I'll choose Amsterdam:

digital ocean create droplet 04

For the authentication, choose SSH Keys and click on the New SSH Key button:

digital ocean create droplet 05

Then follow the steps described on the right:

digital ocean create droplet 06

Now it's time to finalize and create our droplet. In my case I added backups, but for this demo purposes you don't have to.

digital ocean create droplet 07

Now click on the Create Droplet button.

Now DO will create this droplet (give it some seconds). When everything is done you'll see the droplet and it's IP address. Something like this example:

digital ocean create droplet 07

If you enter the IP address in your browser you'll get an error. This is normal, because we only have a droplet with Ubuntu. But we have to configure our Linux VPS. Let's do this next.

Create a new user in Ubuntu

We need to login into our server. We'll do this through SSH. You can download PUTTY or download PowerShell (Scroll down to assets to download). In my case I'll use PowerShell.

Connect with SSH to the server

Open PowerShell and SSH into your droplet through this command to log in to your server as the root user:

The IP address from my droplet is 167.172.37.58. Change the IP address by your droplets IP address

Type yes if you get this message:

By typing yes the connection closes, so I'll need to login again:

Now you're asked to enter your passphrase. Enter your passphrase (or paste it with the right mouse button) and hit enter:

Now we're logged in to the server as the root user:

Create new user

First we'll create a new user on the server:

A little explanation about these commands:

adduser: create a new user with the name of USERNAME

USERNAME: the username for the new user. In my case I'll use dries as the username

Now enter following commands in PowerShell with your username:

The user dries is now created:

Now your asked to choose a password. Enter the password and confirm your password:

Now your asked about some information. You can fill this in or press ENTER for the default values. In my case I press ENTER and at the end I choose yes by typing y:

Give administrator/sudo privileges to our new user

Now our new user needs to get administrator/sudo privileges. So let set this up.
To do this we'll need following commands:

A little explanation about these commands:

usermod: calls the program

-a: to add the user to a group

-G: to specify the group

sudo: stands for 'super user do'. This will execute root commands as an non-root user.

Now enter following commands in PowerShell with your username:

Now I want the same ssh key for the root user and for the new created user (dries). We can copy this key by following commands (change dries by your username):

Now restart the SSH service by following command:

Now exit the server by typing the exit command:

Now it's time to login as the new created user:

You'll be asked for the passphrase. Because we copied over the ssh key from root user to dries we can now use the same passphrase from the root user. Enter the passphrase:

Now you should see this in PowerShell:

This means we're now logged in as user dries in the server.

Install .NET SDK in Ubuntu

Now it's time to install .NET on Ubuntu. We can go to the Microsoft documentation website to see which commands we need to install this on our Ubuntu 18.04 server.

First we install the Microsoft package sources:

Then we install the .NET 3.1 SDK:

Next we check if it's installed by running following command:

You should see something similar like this:

Deploy our ASP.NET Core 3.1 app to Ubuntu

Now it's finally time to publish our ASP.NET Core app to the server. But first we need to make some arrangements. We'll going to be using the instructions from the Microsoft documentation to host our ASP.NET Core app on Linux to deploy our app.

For this tutorial I'll use a simple demo ASP.NET Core 3.1 web application. You can use your own application or else you can clone/download my demo application from my GitHub repository.

There are many ways to deploy the files to a server. In this case we will deploy our files through an FTP connection to our server. We can do this through the build in FTP client in Visual Studio or with an FTP client of choice (FileZilla, ...)

Create application folder on our server

We'll deploy our app to the /var/www/ directory from our server. But this doesn't exist yet, so we have to create this folder. We also need to give access to our user we've created and to the www-data user. This www-data user will be used to run the app.

Type in following commands in PowerShell and replace with the name u want to use for your app.

Install an FTP server on Ubuntu

We will install an FTP (File Transfer Protocol) server on Ubuntu so we can transfer our files to Ubuntu.

Make sure that our system packages are up to date by running following command:

Now it's time to install an FTP server. We will using the common open-source FTP utility vsftpd. To install vsftpd enter the following command:

Enter Y to continue

Now start vsftpd by entering following command:

To make sure that vsftpd is launched and enabled at startup entering following command:

Open your FTP client of choice (for me this is FileZilla) and make a connection by entering the IP address from your server and your credentials (in my case user dries). Now check if you have access to the following path: /var/www/.

Now we need to config vsftpd so we can upload files. At this moment we can only read files from the server but not upload any file. Open the vsftpd configuration file through PowerShell:

Find the entry write_enable=YES and uncomment this line. Don't forget to save this configuration.

Now restart the vsftpd service:

Publish our web application

Now open a new PowerShell window and navigate to your solution folder:

Type in the following command:

You should see something similar like this:

Now open your Windows Explorer and navigate to the following folder: \bin\Debug\netcoreapp3.1\publish. You'll see that this folder contains several files.

These files and folders need to be uploaded to our server. Select all the files and folders and move/drag them to the /var/www/ folder on the server through an FTP-client.

If everything worked correctly, our app is now ready to run.
But if you enter the IP address of your server in a web browser, you will see that this is not working (yet!).

We are almost there! We just need to install a web server on Ubuntu. Let's do that next.

Install and configure NGINX in Ubuntu

Now it's time to install a NGINX webserver and setup a reverse proxy. A reverse proxy will forward requests made to our server to our app. As a webserver we will use NGINX and for the configuration of our reverse proxy server we will follow the Microsoft documentation.

Install NGINX

We follow the NGINX documentation to install NGINX on our Ubuntu release.

We first need to add the repository to our source.list file:

At the bottom of the file add following lines:

Exit and save the file. Then enter following commands:

Run NGINX though following command:

Enter the IP address from your server in your browser. You should see the default NGINX welcome page:

Add an SSL certificate with Certbot (if you have a domain name!)

When you have a domain name that you can redirect to your IP address then you can secure your website with an SSL certificate from Let's Encrypt by using Certbot.

If you haven't a domain name, then skip this step.

Add the Certbot PPA and install with following commands:

Next run the following command and enter your e-mail address when asked. Agree with the Terms of Service by typing A and add your domain name.

Forward requests to our app

Now it's time to forward the requests to our app instead of the NGINX welcome page.

Enter following command to configure this:

Replace the content of that file with following text:

Now we need to enable the site by linking it to /etc/nginx/sites-enabled/default with following command:

We now need to ensure that the default site is included in the http section of the NGINX config file. Edit the file with the following command:

And below, where you'll find

Add the following line below:

Now restart NGINX:

Now start the app by the following command:

In my case this command will be:

Type in your browser your IP address now and you should see your website:

Create service

The next task is to set up our web application to run as a service, so you don't have to be logged in to your server for your application to run. Ubuntu uses systemd by default for managing services. So first we need to create a service file (remember the name of the file as this is what you will reference to control it) by entering following commands:

In my case:

Enter the service information:

In my case:

Give read, write and execute rights to the folder:

In my case:

Then start the service with following command:

Or in my case:

Check if it's running:

Or in my case:

That's it! Now your web server will keep running your ASP.NET Core app, even if you exit the server.

An unhandled error has occurred. Reload 🗙