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.


0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *