Spinning up a NodeJS Server from Scratch on Digital Ocean

Digital Ocean takes a little more set up than Heroku, but once you get it going, it is blissfully easy to use. DO gives you access to a CLI on your server's file system where you can npm, grunt, and node to your heart's content. It's a refreshingly transparent system after the rather opaque world of Heroku's Dyno's and build packs.

What I'll walk through here is setting up a Digital Ocean droplet, their clever euphemism for a server instance. Once we have our droplet set up, we'll install the necessary NodeJS packages, install Git, and clone over a repository. Once we have that going, we'll briefly go through how to set up your DNS to point to your droplet and how to get the server to respond appropriately to traffic.

Set Up a Droplet

First up, head on over to Digital Ocean and sign yourself up.

Before you hit that enticing little 'Create Droplet' button, let's set up an SSH key to make our lives easier. An SSH key will allow you to directly access your droplet's file system from your terminal. After logging in, navigate to Settings > User > Security and click 'Add SSH Key'.

For more detailed instructions, follow Digital Ocean's own excellent tutorial on SSH keys here

Alternatively, TL;DR:

In your terminal:

ssh-keygen -t rsa  

Save the file. Passphrase optional. Then type out:

 cat ~/.ssh/id_rsa.pub

This will print out the public SSH key, which you can then paste into the text area in the Security page. Give the key a name, and save it.

At this point, go ahead and hit 'Create a Droplet'. Get it to run on Ubuntu, the default, and choose your size and location. Make sure to add the SSH key you just created in the 'Add Your SSH Keys' section!

Set up Your Environment

The droplet is up a running at this point, let's get it to do its job. In the droplet menu, copy the IP address of your new droplet. Then open up your terminal, and type the following.

ssh root@YOUR DROPLET IP  

Tell it 'yes', and you're in!

To start setting up the server environment, we'll use apt-get as a package manager.

First off run:

apt-get update  

Then install some essentials:

apt-get install nodejs npm git  

Then it's as simple as running git clone <your repo URL of choice> in your droplet. Once in there, an npm install will get all your packages going. If you need grunt or gulp, just install their cli tools through npm and build away.

Setting up an App Server

This is a summation of this excellent Digital Ocean Tutorial.

To run the node server on the droplet, we'll use PM2 as a process manager.

apt-get install pm2  

pm2 will be responsible for handling our app server on our droplet. Run pm2 start to get things going.

pm2 start <server file>  

At this point, if you visit your droplet's IP:< port #> in your browser, you should see your app up and running.

Setting Up a Web Server

Again, referencing Digital Ocean Tutorial.

Digital Ocean recommends setting up a Reverse Proxy with nginx as a web server.

Essentially, nginx will tell any incoming requests to our DNS which IP to go look at. First, we'll install it using apt-get, and then will set up a config file to route incoming requests to the proper IP.

apt-get install nginx  

Then to get at the config file we need:

vi /etc/nginx/sites-available/default  

And then, as per the Digital Ocean tutorial, replace the contents of that file with the following:

 {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Set up DNS

For reference, here's Digital Ocean's Guide.

Once you have that the app and web servers up and running on your droplet, its a matter of setting up your DNS to point to it. First head on over to the service that manages your domain name, goDaddy etc., and point the nameservers associated with your domain name to:

NS1.DIGITALOCEAN.COM  
NS2.DIGITALOCEAN.COM  
NS3.DIGITALOCEAN.COM  

Next, in your Digital Ocean Dashboard, navigate to the Networking > Domain menu. While there, in the appropriate fields, fill in your domain name and your droplet of choice and hit 'Create Record'.

Conclusion

That should get you where you need to be. Your app is live and hosted on a domain name of your choice! Digital Ocean takes a bit more set up than something like Heroku, but you also have a lot more control over how things are being run on your server!