Tuesday, May 24, 2016

Setting up Ubuntu 14.04 for ASP.NET Core RC2 with PostgreSQL

Getting started building an ASP.NET Core RC2 with a PostgreSQL database hosted on Ubuntu Linux

Downloading, Installing, and Preparing Ubuntu 14.04 Linux server

Over the next two blog posts I will go over the steps in order to create a web app using ASP.NET Core RC2 with Entity Framework connected to a PostgreSQL database all hosted on an Ubuntu Linux server.

This first post will cover setting a virtual machine with Ubuntu 14.04 to be ready to host the web app.  The intended audience is for asp.net c# developers who are familiar with visual studio, c# & EF on Microsoft Windows operating systems and Microsoft SQL server, and would like to get started with the latest free and open source cross platform version of ASP.NET known as ASP.NET Core, and connect it to a free and open source database sql server known as PostgreSQL.   Instead of writing queries by hand I will use the Entity Framework as the ORM.

ASP.NET Core RC2 is pretty exciting because it allows the use of the awesome Microsoft tools such as Visual Studio and allows developers to deploy their apps to a completely free and open source tech stack!

Basically there are 3 basic steps to take to get our Linux server ready to run our app.
  1. Install Ubuntu 14.04 on virtual machine.
  2. Install ssh, PostgreSQL, ftp, nginx, & dotnet RC2
  3. Configure PostgreSQL, ftp, & nginx

Step 1. Install Ubuntu 14.04 on virtual machine.

Download and Install Virtual Box: https://www.virtualbox.org/wiki/Downloads
Install Ubuntu 14.04 on a new virtual machine with virtual box.  Inside of virtual box's settings for the virtual machine, be sure to set the network settings to use the 'Bridged Adapter' option.  This will provide the virtual machine with a network accessible IP address.  It took me about 8 minutes to create a virtual machine with virtual box and install Ubuntu.

Step 2. Install ssh, postgresql, ftp, nginx, & dotnet rc2

The following commands can be run to install all the required items:  Since I would prefer to copy and paste my commands instead of typing them, I will simply run the first two commands and then run 'ifconfig' to get the ip address of the linux VM.  I will then open up terminal, and ssh into the vm using the command: 'ssh username@ip-address'.  

sudo apt-get update

sudo apt-get install openssh-server

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

sudo apt-get update

sudo apt-get install postgresql postgresql-contrib

sudo apt-get install vsftpd

sudo apt-get install nginx 

sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'

sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
sudo apt-get update

sudo apt-get install dotnet-dev-1.0.0-preview1-002702

Step 3.  Configure PostgreSQL, ftp, & nginx

Now the servers need to be configured.  PostgreSQL is the most time consuming to configure, let's start with it first.  PostgreSQL will need a database user created, and set to allow for external connections.  Create a database user name by executing the following command.  Immediately after the command runs a prompt will request the password the user.  Enter a password, and remember it.  It will be needed for the connection string info.

sudo -u postgres createuser *webAppDbUserName* --createdb --pwprompt

sudo nano /etc/postgresql/9.5/main/pg_hba.conf

At the top of the pg_hba.conf file, add the following two lines, and save the file:
host     all     all     md5
host     all     all     ::0/0         md5

sudo nano /etc/postgresql/9.5/main/postgresql.conf

The postgresql.conf file has the following line:
#listen_addresses = 'localhost'  

For examples sake we will all the database server to be connected to by any IP address.  This is done by removing the '#' which comments out the line, and also replacing 'localhost' with '*'. Now the line should look like this:

Save the configuration file with the changes.  Now we can restart the PostgreSQL server and it will be accessible by external connections.  The command to restart PostgreSQL is:

sudo service postgresql restart

Now PostgreSQL is configured to allow remote connections, and it has a user with a password which will allow for creating databases.  Great, now we just need to configure the ftp server and nginx.  The ftp server will simply need to be switched to enable writing by removing a commenting '#' in the configuration file.  Type the following command to open the file:

sudo nano /etc/vsftpd.conf

Look for the line that says 


Remove the '#' and save the file.  Now restart the ftp server using the following command:

sudo service vsftpd restart

Now the ftp server is configured to allow the writing of files.  Finally the nginx server will need to be configured.  For simplicity sake and since this is a fresh install, I will delete all the configuration information for the nginx server and replace it (Don't delete this file if you already have a configured nginx server).  The following two commands will delete the existing file, and then open an editor to replace it:

sudo rm /etc/nginx/sites-enabled/default
sudo nano /etc/nginx/sites-enabled/default

Now type or copy and paste the following into the empty file, and save it:

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

Now we just need to restart the nginx server and it will be configured to point our external traffic on port 80 towards our locally hosted site on port 5000.  This is the default location for ASP.NET Core apps to be hosted.  Restart nginx server with the following command:

sudo service nginx restart

Our Linux server is now setup and configured to allow us to deploy our ASP.NET Core web app.  All we need to do now is get an ASP.NET Core web app and run it.  We will begin building and deploying our ASP.NET Core RC2 web app in the next blog post.  


  1. How would you configure nginx so you could host more Asp.Net Core websites. Wouldn't you mind to expand your post? Also, if web1.myweb.com and web2.myweb.com domains are pointing to your new Linux server IP how would you configure your server/nginx so you could server your 2 websites through those domains? Thanks :)

    1. I'll have to write a new post about how to do some nginx stuff with asp.net core. Thanks for the ideas.

  2. Is the project sample available on gituhub

    1. Yes! https://github.com/TotalTechware/unape

      I posted that in a different blog, and totally forgot about getting back to you. Please accept my apology. :-)