WEB APPS: How to deploy to the cloud with Heroku

Set up SSH keys spinning up an instance and deploy to the cloud with command-line Git


Set up SSH keys spinning up an instance and deploy to the cloud with command-line Git



We all enjoy making things every now and then. This can take any form – a linter, image converter or even a web service that sends you a picture of a cat whenever someone hits a button.

Often made for ourselves, sometimes, these tools pick up traction and other people start to use the thing you’ve made; you’ve solved a problem that other people have, congratulations! But this success has brought you a problem in itself, how are you going to keep your servers up with all of these new people using your shiny thing?

Shared web hosting is designed to be used by a couple of hundred, maybe even a few thousand people at the same time. It’s not going to be able to stand up to something going viral – at least – not for long and that’s assuming your web project is relatively small. If you’ve made something that’s, let’s say, 2MB in size, it doesn’t seem like too much for your provider to handle, does it? What if 10,000 people access your site in one day? That’s 20GB in bandwidth you’ve got to provide!

Shared hosting is not going to cut the mustard. You could provision a server and make it bigger as the site gets bigger, but that means you’ve got to select a provider, access the server, configure it for your app and, more often than not, scale it manually. This is not good enough.

This is where Heroku comes in. Heroku is a cloud platform that delivers your app for you, it scales whenever you like and it has Node, Ruby, PHP and Python built into it and ready to go! This tutorial is going to walk you through setting up a Heroku account, getting SSH keys all in order, configuring a Node.js app for deployment and then firing it off to Heroku to be run.

1. Create an account

Head on over to and then simply click the Sign Up button to create a new account. Check your inbox now for an email confirming that you want to sign up and create a strong password. If you’re worried about Heroku costing you something, fear not because there’s a free tier for you to experiment, explore and learn from. This free tier is the one that we’ll be using in this tutorial.

2. The dashboad

Once you’ve logged in to your dashboard, it’s here that we can create, manage, deploy and destory our apps with the help of the Heroku platform. If you click your email address in the top left of the dashboard, you’ll see a dropdown with the option Manage Account. Don’t click it, but take note of where it is located for now as we’ll be using it in a little while

3. The Heroku Toolbelt

In development, the Terminal is a powerful tool. Heroku has created a Toolbelt to help us get the most out of Heroku through the command line. Most of this tutorial will be done in the CLI (command-line interface), so head to, select the right Toolbelt for your operating system and simply follow the instructions to install it.

4. Log in with the Toolbelt

Once the Toolbelt has installed, you should now open your terminal (or the equivalent for your operating system) and simply enter:
heroku login

5. Heroku and SSH Keys

In the next few steps, we will now go over creating SSH keys on a Unix-based system (Linux or OS X). If you’re familiar with this process though, you can just skip over them and generate the SSH keys as you like, otherwise, you can read on and follow what they say. SSH keys are user credentials that don’t involve a password. Instead though, it uses public-key cryptography to generate a secure way of authenticating a user to a service, and we’re going to do just that with Heroku. This will spare you having to type in your password every single time that you want to do something.

6. Create an SSH Key

In terminal, enter the following with the email address you used to sign up to Heroku:

ssh-keygen -t rsa -b 4096 -C “[[YOUR@EMAILADDRESS.COM]]”

A new private/public key will be generated for you. You’ll be asked if you want to store the keys anywhere specific, just hit ‘enter’ and accept the defaults until you’re asked to enter a passphrase, choose something secure but memorable – it’s not easy to change this one!

7. Enable the SSH Keys

To get your system to actually use your new SSH keys, we will need to add them to the SSH agent. After that we can add them to Heroku. Enter the following command, you’ll be asked for the passphrase you used when creating the new keys. The SSH key will then be ready to be used by your computer and it will also have been copied to your clipboard.

eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa && pbcopy < ~/.ssh/

8. Add the SSH key

Remember that Manage Accounts button we told you to take note of earlier? It’s time to press it. Head back to the Heroku dashboard ( will redirect you there if you’re still logged in) and then click the Manage Account button. Now just scroll down until you see SSH Keys, click edit and then paste the SSH key into the input (the last command that we entered also copied it into our clipboard for us).

9. Alternative method

If all that GUI goodness is not your way of doing things, you can upload the SSH key to Heroku with the command-line interface. Simply head back to your terminal and enter:

heroku keys:add

You’ll be asked if you want to upload your key to Heroku. Hit ‘Y’ for yes.

10. Create a new project

On your Heroku dashboard there is a + sign in the top-right corner. Click it now to create a new Heroku app. You’ll then be given a form asking you to enter a name and whether you want your server to be in the US or Europe. If you don’t enter a name, one will be generated for you, so it’s up to you whether you want to give your app a name.

11. Prepare for deployment

If you have a project ready that you’d like to test deploying to Heroku, cd to the root folder of that project now. Otherwise, there is a demo project that you can grab from our FileSilo downloads. Once you’ve made your way to your chosen project in Heroku, enter the following commands:

git init
heroku git:remote -a name_of_your_heroku_app

When those commands have completed, we will have created a new Git repo with our Heroku app as a remote. With this, we can use SSH and Git to deploy our apps to Heroku with a Git commit.

12. Our demo app

Our app is a simple Node.js/Express server, delivering files in our ‘public’ folder to whomever browses to /. One thing to note is the port binding on line 7 of demoServer.js: ‘process.env.PORT || 8080’. Process.env.PORT lets Heroku choose the port it wants to make our app available on as it can change per deploy or instance.

13. Deploy to Heroku

We can also deploy with a Git commit. Now that we have Heroku set up as a remote Git repository, by pushing new code to it, Heroku’s build process will be triggered and will deploy our app for us. Let’s give it a go now:

git add .
git commit -m
git push heroku master
You’ll see Heroku building, testing and then finally deploying our app to its platform. If all has gone well, you’ll see something like
Launching... done, v1
remote: deployed to Heroku

14. Debugging errors

Except you probably won’t see that everything has gone right, because we’ve deliberately added an error. So how do we debug errors on our Heroku instances? If a Heroku build fails, it will refuse to deploy the broken code in favour of previously working code. But that only works if it’s a compile-time error. If the error is a run-time error, it won’t break until after it’s deployed. To find out where the error may lie, you can enter ‘heroku logs –tail’ and you’ll see the latest entries for your Heroku server.

15. Fix the error

If you look at our logs, the error that was deliberately made is simple, Our Node.js app is trying to listen on a port reserved by the system. Now just open demoServer.js and change line 7 so that it reads:

// Old
// port = 15 || process.env.PORT || 8080;

// New
port = process.env.PORT || 8080;

16. Deploy the fix

Now that we’ve fixed our app, we can try and redeploy it to Heroku. There’s no special procedure for this, simply commit the modified files for a Git push and then push to the Heroku origin.

git add .
git commit -m "Fixed server port issue"
git push heroku master

17. Scale your web service

By default, Heroku will start your apps running on the ‘web’ tier of its service. This is the least powerful of the tiers available, but it’s also free. Let’s say you have an overnight hit on your hands, how do you scale your app to handle all of these new people wanting to use the great thing you’ve made? It’s pretty simple, you can just enter:

heroku ps:scale web=2

.…in your command-line interface. That will spin up two web-scale instances of your Heroku app and will split the users betwee n them – thus, each server will handle