Running ASP.NET Core Apps on DigitalOcean
Running ASP.NET Core Apps on DigitalOcean
By combining DigitalOcean's droplets (their codename for VMs) with .NET Core, you can get your apps up on a virtualized infrastructure, reaping the benefits therein.
Join the DZone community and get the full member experience.Join For Free
In this blog post, I am going to take a step-by-step lap around running ASP.NET Core web applications on DigitalOcean. So let’s get started.
What Is DigitalOcean?
According to official help documentation of DO (as it is popularly known as), here is the definition:
DigitalOcean is a cloud infrastructure provider focused on simplifying web infrastructure for software developers.
DO is like Azure or AWS or GCP but focuses on Infrastructure as a Service space of public cloud. You can know more about DO here.
What Is ASP.NET Core?
ASP.NET Core is the new web framework/technology from Microsoft which is cross platform in nature. That means you can develop an ASP.NET Core Web Application using C# as a language but can host it on Windows or LINUX or OSX. And ASP.NET Core is an Open Source Software. You can read more on ASP.NET Core here.
Getting Started With DigitalOcean
As with any other cloud provider, you will need to register first. Head over to DigitalOcean website here, and sign up for an account. If you want to follow along with this post, I suggest you get an account for yourself and login to your account.
Creating a Droplet
DO calls its VMs “Droplets.” I think it’s a cute name for the regular, otherwise boring name “VM” or “Virtual Machines.” Since DO is all about IaaS, we will need to first create a VM and run ASP.NET Core on it. ASP.NET Core can be run on 64bit versions of LINUX flavors. For this exercise, I will be using a UBUNTU 16.04 OS. Let’s create our droplet now.
After logging into your DO account, you will see a simple and clean interface like below:
DigitalOcean droplet surface
Yes, that is all you will get as you dashboard. I like that fact that it follows the KISS principle – Keep It Simple Silly. Notice, there are two buttons which say “Create Droplet”. You can click on any of those buttons to create a new VM or Droplet.
Selecting Droplet Images
The first step in creating a droplet is to pick the OS. They offer all flavors of LINUX distributions. But for this exercise, I want to use their One-Click apps feature where you will get images with some pre-installed Softwares. For running ASP.NET Core, I need a UBUNTU 16.04 and .NET Core installed on the machine. You will see an image with .NET Core + PowerShell on UBUNTU 16.04 – select that image.
Choose droplet image
Selecting Droplet Size
Next, we need to select the size of the Droplet. For this exercise, since this is a demo, I will be selecting a $5 machine which will have 1 CPU + 512MB RAM + 20GB SSD. That’s enough for a demo. But you have the option of choosing from many configuration choices. They also provide high memory machines.
Now is the time to decide where do we want to place the droplet i.e. what datacenter do we need the droplet to be created. I am from Bangalore and DO is probably the only cloud provider who has a data center in Bangalore. For this post, I will select my datacenter as Bangalore.
Adding SSH Keys
It is always a good practice to use SSH keys when working with VMs or in this case Droplets. DO has a fantastic tutorial on how to use SSH keys with DO droplets. I followed their tutorial here and have set up SSH key with my DO account.
Adding SSH keys
Finalizing Your Droplet
Now that we have picked all choices for our Droplet, the last step is to decide how many droplets we need and a name for the droplet. I will pick 1 droplet and will leave the default name for my droplet as is. Click the “Create” button after you make your changes.
Finalizing droplet creation
Once the droplet creation finishes, you will be navigated back to your dashboard. You can then click on your droplet to get more information. Here is how my droplet information page looks like:
Droplet information page
Next up, we will see how to connect to our newly created droplet.
We can connect to our UBUNTU droplet using SSH. As said before, refer to the tutorial here, which provides you step by step instruction on how to connect to your droplet. I have gone ahead and established an ssh connection using PuTTY. Below is my PuTTY session:
PuTTY session with a Droplet
Let’s check the DotNet version on the droplet. On the terminal, execute the following command
The output of the above command will be as below:
The droplet has been built with an older version of DotNet Core. It is preview3. As of now, the DotNet Core version is 1.1 and LTS version being 1.0. What I will do next is to install DotNet Core 1.1. on this droplet.
Remove All Versions of DotNet Core
In order to install latest and greatest version of DotNet Core, we will need to first remove the already installed version. Microsoft has created a cleanup script which can be found here. On the same terminal session just run the script and the preview version of DotNet Core will be uninstalled.
Add DotNet Apt-Get Feed
In order to install DotNet Core, we need to first set up the apt-get feed that hosts the DotNet Core package. On a terminal session, run the following commands:
sudo sh -c ‘echo “deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main” > /etc/apt/sources.list.d/dotnetdev.list’
sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv-keys 417A0893
sudo apt-get update
Install DotNet Core SDK
As said before, I will be installing DotNet Core 1.1 which is the latest version of the framework. We will use apt-get to install the 1.1 version. On a terminal session execute the following command:
sudo apt-get install dotnet-dev-1.0.0-preview2.1-003177
After the command is executed successfully, we will have 1.1 version on our droplet. if you execute command “dotnet –version” now, we will see the value “1.0.0-preview2-1-003177″.
Create ASP.NET Core Web App
Creating a new ASP.NET Core based web app is easy with the dotnet CLI. On the droplet, let’s create a folder named webapp1. Next, let’s change our working directory to webapp1. Now, execute the following command to create a new web app:
dotnet new -t web
What the command does is, it scaffolds a bare bone ASP.NET Core MVC web app with 3 routes – Home, About and Contact. ASP.NET Core Web Apps now rely on certain packages to function. So we need to now restore those packages. Execute the following command on a terminal:
Now, we are ready to run the web app.
Running ASP.NET Core Web App
In order to run the web app, open a terminal and execute the following command:
By default, when we don’t specify any explicit port, dotnet CLI makes the web app run on port 5000. So now we have our web app running inside our droplet at port 5000.
Expose Web App to the Outside
Now that our web app is up and running, we have just one thing to take care. How to make this web app available to outside world. We can’t reach this app from outside world i.e. it is not public facing yet. It is running on port 5000 which is internal to the droplet. For the sake of the demo, what I will do is to let the web server running the ASP.NET Core Web App (which is called Kestrel) to bind to all network adapters and listen for port 80. Open Program.cs which is available at the root of the project in a text editor and modify the code as below:
var host = new WebHostBuilder() .UseKestrel() .UseUrls("http://*:80") .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>() .Build();
We have used UseUrls() method to bind to all adapters and listen for port 80. Now if you open a browser in your machine and navigate to http://<IP OF YOUR DROPLET>, you will able to browse the ASP.NET Core Web App. Here is a screen shot of the web app on my browser:
ASP.NET Core Web App
That’s it, we have successfully hosted an ASP.NET Core Web App on a UBUNTU 16.04 64 bit droplet on DigitalOcean cloud. It can’t get any easier than this.
Through this blog, I just wanted to showcase how easy it is to run ASP.NET Core solutions on non-windows platforms. ASP.NET Core is truly cross platform and you can now deploy to any server OS you are familiar with. With DigitalOcean, if you are an IaaS guy – you are in control on your droplets. I hope this helps you in your endeavor to use ASP.NET Core.
Published at DZone with permission of Lohith Nagaraj , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.