We’ve all been here before: you’ve started writing a new Django site, and you’re ready to show it to the world. You’re familiar with the normal cloud hosting options (Rackspace and Amazon Web Services), but you’d like to save yourself the hassle of managing the mess that comes with them.
Heroku is a popular platform-as-a-service provider that’s been around since 2007, providing simplistic web application hosting for polyglot developers. Heroku supports a wide variety of programming languages, and just about every web framework you can think of.
What makes Heroku a perfect choice for hosting is that:
Furthermore–Heroku has a large collection of addons which makes using additional infrastructure components easy (you can even write your own).
Getting started with Heroku is simple. There are four major things you’ll need to do:
Through the rest of this site–you’ll be expected to have the above working and ready to go. I’ll walk you through steps 2 and 3, but steps 1 and 4 are up to you.
Before you go any further, sign up for an account on Heroku: https://api.heroku.com/signup.
The Heroku toolbelt is a set of software Heroku officially supports which provides:
The heroku CLI app is a great tool (maintained by Heroku) that allows you to fully manage your Heroku applications from the command line. You can use it to:
To get started, visit: https://toolbelt.heroku.com and follow their installation instructions for your specific system.
Don’t create any applications or anything else just yet–we’ll get to that in a bit.
We’re all programmers here, so I’m going to assume that you probably work on code both for fun, as well as for profit. Seeing as how this is most likely the case, this step will make your life much easier later on.
The heroku-accounts plugin allows you to easily switch between Heroku accounts on the command line. This will allow you to do things like:
To install heroku-accounts, simply run the following command:
$ heroku plugins:install git://github.com/ddollar/heroku-accounts.git heroku-accounts installed
Once you’ve got heroku-accounts installed, you should be able to run heroku help accounts to see the usage information:
$ heroku help accounts Usage: heroku accounts list all known accounts Additional commands, type "heroku help COMMAND" for more details: accounts:add # accounts:add accounts:default # accounts:default accounts:remove # accounts:remove accounts:set # accounts:set
As you can see, heroku-accounts provides several useful commands, add, default, remove, and set.
Let’s quickly add our personal account:
$ heroku accounts:add rdegges --auto Enter your Heroku credentials. Email: email@example.com Password (typing will be hidden): Generating new SSH key Generating public/private rsa key pair. Created directory '/home/rdegges/.ssh'. Your identification has been saved in /home/rdegges/.ssh/identity.heroku.rdegges. Your public key has been saved in /home/rdegges/.ssh/identity.heroku.rdegges.pub. The key fingerprint is: c3:b5:59:7f:4b:b5:c7:fb:59:eb:c6:c8:af:ac:7f:da rdegges@baal The key's randomart image is: +--[ RSA 2048]----+ | | | | | . . .| | . . + . .o| | S o .o+| | . ..+| | . oo.| | .o.+=| | .o=BE.| +-----------------+ Adding entry to ~/.ssh/config Adding public key to Heroku account: firstname.lastname@example.org
What happened here was that I created a new Heroku account (locally), and gave it the name rdegges. Since this is my personal account, naming it rdegges makes sense for me. If you’ve got multiple Heroku accounts, create them now.
When you add an account using heroku-accounts, heroku-accounts will automatically generate and upload a new SSH key to your Heroku account. This way, you’ll be able to push code to any of your Heroku applications.
Now that we’ve got our account(s) created, we can list them by running heroku accounts without any options:
$ heroku accounts rdegges telephonyresearch
If you want to set one account to by your system-wide default, you can do so via the heroku accounts:default command:
$ heroku accounts:default rdegges $ heroku accounts * rdegges telephonyresearch
As you can see, after an account is set as the default, it will have a * next to it. You can change the default account at any time.
If you’d like to remove an account, just use the heroku accounts:remove command:
$ heroku accounts:remove work Account removed: telephonyresearch $ heroku accounts * rdegges
The last thing we’re going to do here is specify which account we want our Django project to use. This way, the heroku command line tool will know which account to use when we’re pushing code, adding infrastructure components, etc.
Remember that Django project I talked about at the top of this section? The one that you’ve got version controlled with Git, and ready to be deployed? Go into the root of that project, and run the heroku accounts:set command:
$ cd ~/Code/rdegges/deploydjango $ heroku account:set rdegges $ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git://github.com/rdegges/deploydjango.com.git [branch "master"] remote = origin merge = refs/heads/master [heroku] account = rdegges
Whenever you use the acccounts:set command, heroku-accounts will add a new section to your project’s .git/config file, specifying which Heroku account to use whenever you’re working on that project. Pretty nifty, right?
Now that we’ve gotten all the basics covered–let’s create our first actual Heroku application!
$ cd ~/Code/rdegges/deploydjango $ heroku create --stack cedar deploydjango Creating deploydjango... done, stack is cedar http://deploydjango.herokuapp.com/ | email@example.com:deploydjango.git Git remote heroku added
Easy, right? And just in case you’re wondering–Heroku has multiple “stacks”, the most recent of which is cedar. You can read more about the Heroku cedar stack here.
Also, notice that when you created your app, Heroku automatically generated a project URL as well as a private Git repository.
Your project URL, (http://deploydjango.herokuapp.com/) is a publicly available URL that you can use to visit your site over the Internet.
The private Git repository (firstname.lastname@example.org:deploydjango.git) is a Git repository stored on Heroku’s servers that you’ll use to deploy new versions of your site. All deployment on Heroku is done through Git, which gives you a lot of power and data about your site as it grows.
This section contains some slightly more advanced topics that didn’t fit into the Setup section. Feel free to skip around to sections below that interest you.
At some point, you’ll most likely want to give another developer access to your project. Coding by yourself is great and all, but two heads are (often) better than one :)
To add a collaborator to your project, you can use the sharing:add command:
$ heroku sharing:add email@example.com firstname.lastname@example.org added as a collaborator on deploydjango.
Collaborators will be able to push new code to your application, but won’t be able to add new infrastructure components. For example: if I’m a collaborator on your application, I can push new features live, but I can’t add a new PostgreSQL database (only the account owner can do that).
If you need to remove a collaborator from your account (maybe you and your co-founder weren’t getting along), you can use the sharing:remove command to immediately revoke their access:
$ heroku sharing:remove email@example.com Collaborator removed.
If you haven’t worked on your application in a while, it’s probably a good idea to view your application’s status before doing anything. Heroku stores quite a bit of metadata about your application, so you can see stuff like:
To view this information, just run the info command inside your application directory:
$ heroku info === deploydjango Addons: PG Backups Auto - One Month Retention, Shared Database 5MB Database Size: (empty) Domain Name: www.deploydjango.com Git URL: firstname.lastname@example.org:deploydjango.git Owner: email@example.com Repo Size: 40M Slug Size: 6M Stack: cedar Web URL: http://deploydjango.herokuapp.com/
Sometimes you’ll want to get rid of an application that already exists. Sure, you can do this through Heroku’s web interface, but that’s no fun.
To destroy an application via the command line, you can run the apps:destroy command:
$ heroku apps:destroy deploydjango ! WARNING: Potentially Destructive Action ! This command will destroy deploydjango (including all add-ons). ! To proceed, type "deploydjango" or re-run this command with --confirm deploydjango > deploydjango Destroying deploydjango (including all add-ons)... done
While I’ve tried to give you a good overview of Heroku here, there is quite a bit more to learn. Depending on your current skill level, you may want to read through some of the resources listed below.
Since using Heroku revolves around the use of Git, I strongly recommend you become a Git wizard, as it will make your life much easier as you go on.
These are both excellent books on Git. If you’re a complete beginner with Git, you should read through both books, as the first is a lot more entry level, and the latter contains more complex topics and advanced usage.