Deploying to heroku

in five ten minutes

Brenton Cleeland
@sesh // brntn.me

Why Heroku?

  • Cheap during development and testing (reasonable after that)
  • Quick and easy to get started
  • Scales really well
  • git based workflow
  • Release management and rollback

Packages We'll Use

  • Heroku Toolkit
  • dj-database-url
  • whitenoise
  • gunicorn
  • Django
  • (virtualenv)
  • (gibo)

Heroku toolkit

> heroku --help

Usage: heroku COMMAND [--app APP] [command-specific-options]

Primary help topics, type "heroku help TOPIC" for more details:

  addons    #  manage addon resources
  apps      #  manage apps (create, destroy)
  auth      #  authentication (login, logout)
  config    #  manage app config vars
  domains   #  manage custom domains
  logs      #  display logs for an app
  ps        #  manage dynos (dynos, workers)
  releases  #  manage app releases
  run       #  run one-off commands (console, rake)
  sharing   #  manage collaborators on an app

https://toolbelt.heroku.com/

Configuring: dj-database-url

# settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

import dj_database_url
DATABASES = {
    'default': dj_database_url.config(default='sqlite:///{}'.format(
        DATABASES['default']['NAME'])
    )
}

https://github.com/kennethreitz/dj-database-url

Configuring: whitenoise

# wsgi.py

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = DjangoWhiteNoise(get_wsgi_application())
# settings.py

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

https://github.com/evansd/whitenoise

Configuring: gunicorn

# Procfile

web: gunicorn melbdjangoheroku.wsgi --log-file -

http://gunicorn.org

Configuring: django

# settings.py

DEBUG = os.uname()[0] == 'Darwin'
TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = ['.herokuapp.com']

Configuring: requirements.txt

# requirements.txt

Django==1.7.4
dj-database-url==0.3.0
gunicorn==19.2.1
whitenoise==1.0.6
psycopg2==2.6

http://slides.com/brntn/managing-your-django-requirements/#/

Actually deploying

  • Commit your configuration
  • run heroku create
  • git push heroku master
  • heroku looks after:
    • installing (and removing) requirements
    • collectstatic
  • heroku run python manage.py migrate
  • heroku run python manage.py createsuperuser

HANDY LINKS

  • Django deployment checklist: https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
     
  • Heroku Django template:
    https://github.com/heroku/heroku-django-template
Made with Slides.com