Deploying a Node.js app to DigitalOcean

To set up a domain name, see How To Set Up A Host Name With Digital Ocean.

If you need to serve your site securely over HTTPs, see Setting up HTTPS in Node.js with Let’s Encrypt.

So you want to deploy that app you made with Node? Well, DigitalOcean is one way to do it. They provide cheap, fast SSD-based virtual servers, or ‘droplets’, that you can spin up from their website. Their product is geared towards developers – they assume you have some proficiency with the command line and are comfortable configuring your own server. Most of what follows is adaptable to AWS, Azure, Heroku, and any other provider of virtual private servers.

A word to the wise: upgrading a DigitalOcean droplet is easy. Downgrading a droplet is hard. If you aren’t sure how much space / memory / transfer you will need, start small and move up later.

I’ll keep this tutorial brief and assume you have a minimum of experience with servers. This setup works well for small apps with light traffic and storage requirements – essentially, the kind of apps you might build as proofs of concept while learning web development.

  1. Create an account with DigitalOcean.
  2. Log in and click Create Droplet.
  3. Select ‘Ubuntu’ at the current highest x64 version. Select the the size you’re comfortable with – remember, if in doubt, start small. Add block storage if you need it, and select a datacenter close to where your users are. Select additional options if you want – most often, backups are a good idea and worth the extra investment.
  4. If you don’t have an SSH key set up yet, it’s a good idea to set one up by clicking New SSH key, and following step 1-4 of the SSH key directions. This prevents you from receiving the root password via email, which is not very secure.
  5. Unless you have reasons to make duplicate droplets, leave the number set at 1. Give your droplet a hostname, and click Create.
  6. Wait while the droplet is created. When it’s ready, you’ll see your IP address – write this down for future use with SSH access in step 9.
  7. Follow the steps in these tutorials to configure your server: Initial Server Setup with Ubuntu 14.04, then Additional Recommended Steps for New Ubuntu 14.04 Servers. This will help you set up SSH access by setting up a non-root user, disabling root access for enhanced security, configuring SSH access, creating a basic firewall, and configuring the time zone. Don’t do the steps for Create a Swap File – you’ll see that this is not recommended for SSD-based servers like the one you just created. Do follow through with taking a snapshot of your current configuration so you can clone your server from this point next time you’re setting up an app. You may want to wait and do this after you follow the next step, so that your snapshot is ready to go with Node installed.
  8. To install Node.js v 6.x, enter this in the SSH terminal:
    curl -sL | sudo -E bash -
    sudo apt-get install -y nodejs
  9. Next, you’ll need to copy your app’s files from your local machine to your server. You have several options here:
    • Use SCP from the command line. You’ll want to copy files into your home directory – this directory should have the correct permissions set to allow you to remotely add files. Run this from your local terminal – the sudo password here is your local password, path-to-local-site-root is where your site folder lives on your local machine, your-ip-address is the IP from step 6, user is the username you created in step 7, and site is the name of your remote site folder.
      sudo scp -r path-to-local-site-root user@your-ip-address:/home/user/site
    • You can also use an SFTP browser like Cyberduck.
    • If you’re using Git, a very slick way to copy files is with automated Git deployment, as shown here: How To Set Up Automatic Deployment with Git with a VPS.
  10. Start up that Node server! Check out my post Keep Node running forever for details on this. Note that you will need to navigate to your server folder, which is somewhere within /home/user/site before you run Node. Once you start Node, you should be able to open your IP address in the browser and see your site.


Keep Node running forever

If you’ve ever started up a deployed Node application, you’ve probably had this problem: you start the server with node server.js, check on your site, and it’s up and running. Great! Some time later, your SSH session times out or you close your terminal. Then you go to your site, and it’s down. What happened?

You ran the server in the foreground, which means that when you log out of SSH, it will be stopped along with all the other running processes.

I’ve found two good solutions. Before using either of them, make sure you are starting the server with root privileges by running sudo -i. This will take you to the root of the server, so you will need to cd back to the folder where the server file is located.

Method 1: Run node in the background

In this method, you start Node with the & flag to run it in the background and the nohup flag to detach it from the console window.

To start the Node server:

sudo nohup node server.js &

To shut down the Node server, first find the running process ID with:

ps -aef | grep node
// root     21404     1  0 08:05 ?        00:00:00 node server.js
// alex     21530 21494  0 08:08 pts/0    00:00:00 grep --color=auto node

Then kill the process via the ID:

sudo kill 21404

More details are in these Stack Overflow posts: run node permanently and stop node.

Method 2: Use Forever to keep the server up

In this method, you install Forever and use it to run the server for you. This method is more robust, because it will restart your server automatically if it crashes.

Make sure to use forever start to run the server as a service.

sudo npm install -g forever
sudo forever start server.js

Now, when you close your terminal window, Node will continue to run. Here are a few helpful forever commands from the StackOverflow post:

To list all running processes:

forever list

Note the integer in the brackets and use it as following to stop a process:

sudo forever stop 0

Restarting a running process goes:

sudo forever restart 0

If you’re working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:

sudo forever -w server.js

More info on Forever is available on GitHub.

Meetup notes: Microsoft Bot Framework

Last night, I went to a meetup at Slack on the Microsoft Bot Framework. It was a good opportunity to get a grasp on what the Bot Framework is, and how developers are using it to create bots that deploy on several different channels, including (as of March 2016):

  • Text/SMS
  • Office 365 mail
  • Skype
  • Slack
  • GroupMe
  • Telegram
  • Facebook Messenger
  • Kik
  • Web (via the Bot Framework embeddable web chat control).

The general idea is that you can write a bot in the Microsoft Bot Framework, and it will be portable to all these channels, with some slight modifications depending on the specifics. Great! But what is a bot, anyway?

Generally, a bot embodies some kind of AI, or artificial intelligence. It’s an app that acts like a person, and it helps you accomplish work by using contextual meaning. If you’re an iPhone user, you’re familiar with Siri. Siri is essentially a bot – it asks you questions and accomplishes some kind of work by reasoning about your answers. Here’s a screenshot from the Bot Framework splash page:

Screen Shot 2016-06-23 at 1.28.01 PM

Mat Velloso gave the presentation, and one example he gave is called Murphy. You ask Murphy any kind of ‘what if’ question, and it goes out and finds images that match your scenario, then mashes them together. Basically, it’s face swap via bot. You can say ‘what if I were the president?’ and Murphy will probably give you an image of President Obama with your face. This is a rudimentary example, but you can see how bots would make lots of things faster, especially on a mobile device. You can give short, partial answers and depend on the natural language processor to figure out what you mean.

The SDK, available on GitHub, is written in C# and Node.js. You can fork the repo and start working from examples to build your own bots. Here are a few more resources that were covered during the presentation: – take a look at the docs – lets your app understand language – a whole suite of services like LUIS, including face and voice recognition, academic knowledge, image search, etc.

bot framework emulator – lets you play with your bot as your develop it. Includes a GUI for Windows and a command-line version for Mac / Linxu.


Looking forward to playing with this framework and maybe building a bot of my own in the near future!