Deployment on Heroku

Script

Run these commands to deploy the project to Heroku:

  1. Create app on Heroku

    heroku create --buildpack heroku/python
    
    heroku addons:create heroku-postgresql:hobby-dev
    
  2. Database setup

    1. Postgres
      # Postgres
      # On Windows use double quotes for the time zone, e.g.
      # heroku pg:backups schedule --at "02:00 America/Los_Angeles" DATABASE_URL
      heroku pg:backups schedule --at '02:00 America/Los_Angeles' DATABASE_URL
      heroku pg:promote DATABASE_URL
      
    2. MySQL

      To add mysql to yout app when deploying it on heroku, you have to add it through some add-on that support MySQL. Check the full list of add-ons on Heroku Add-ons. Here JAWSDB Add-ons is being used.

      heroku addons:create jawsdb --app <name_of_your_app>
      # or
      heroku addons:create jawsdb --app <name_of_your_app> --version=<your_desired_mysql_version>
      
      # once database is delployed, you can run the following command to get the connection url,
      heroku config:get JAWSDB_URL
      >> mysql://username:password@hostname:port/default_schema
      
      # backups
      heroku addons:create jawsdb --bkpwindowstart 00:30 --bkpwindowend 01:00 --mntwindowstart Tue:23:30 --mntwindowend Wed:00:00 --app <name_of_your_app>
      
      # To find more about jawsdb backups
      # https://devcenter.heroku.com/articles/jawsdb#backup-import-data-from-jawsdb-or-another-mysql-database
      
  3. Redis connection setup

    heroku addons:create heroku-redis:hobby-dev
    
  4. Mailgun

    # Assuming you chose Mailgun as mail service (see below for others)
    heroku addons:create mailgun:starter
    
  5. Setting up environment variables

heroku config:set PYTHONHASHSEED=random

heroku config:set WEB_CONCURRENCY=4

heroku config:set DJANGO_DEBUG=False
heroku config:set DJANGO_SETTINGS_MODULE=config.settings.production
heroku config:set DJANGO_SECRET_KEY="$(openssl rand -base64 64)"

# Generating a 32 character-long random string without any of the visually similar characters "IOl01":
heroku config:set DJANGO_ADMIN_URL="$(openssl rand -base64 4096 | tr -dc 'A-HJ-NP-Za-km-z2-9' | head -c 32)/"

# Set this to your Heroku app url, e.g. 'bionic-beaver-28392.herokuapp.com'
heroku config:set DJANGO_ALLOWED_HOSTS=

# Assign with AWS_ACCESS_KEY_ID
heroku config:set DJANGO_AWS_ACCESS_KEY_ID=

# Assign with AWS_SECRET_ACCESS_KEY
heroku config:set DJANGO_AWS_SECRET_ACCESS_KEY=

# Assign with AWS_STORAGE_BUCKET_NAME
heroku config:set DJANGO_AWS_STORAGE_BUCKET_NAME=
  1. Deploying

    git push heroku master
    
    heroku run python manage.py createsuperuser
    
    heroku run python manage.py check --deploy
    
    heroku open
    

Notes

Email Service

The script above assumes that you’ve chose Mailgun as email service. If you want to use another one, check the documentation for django-anymail to know which environment variables to set. Heroku provides other add-ons for emails (e.g. Sendgrid) which can be configured with a similar one line command.

Warning

If your email server used to send email isn’t configured properly (Mailgun by default), attempting to send an email will cause an Internal Server Error.

By default, django-allauth is setup to have emails verifications mandatory, which means it’ll send a verification email when an unverified user tries to log-in or when someone tries to sign-up.

This may happen just after you’ve setup your Mailgun account, which is running in a sandbox subdomain by default. Either add your email to the list of authorized recipients or verify your domain.

Heroku & Docker

Although Heroku has some sort of Docker support, it’s not supported by cookiecutter-django. We invite you to follow Heroku documentation about it.

Optional actions

Celery

Celery requires a few extra environment variables to be ready operational. Also, the worker is created, it’s in the Procfile, but is turned off by default:

# Set the broker URL to Redis
heroku config:set CELERY_BROKER_URL=`heroku config:get REDIS_URL`
# Scale dyno to 1 instance
heroku ps:scale worker=1

Sentry

If you’re opted for Sentry error tracking, you can either install it through the Sentry add-on:

heroku addons:create sentry:f1

Or add the DSN for your account, if you already have one:

heroku config:set SENTRY_DSN=https://xxxx@sentry.io/12345

Gulp & Bootstrap compilation

If you’ve opted for Gulp, you’ll most likely need to setup your app to use multiple buildpacks: one for Python & one for Node.js:

heroku buildpacks:add --index 1 heroku/nodejs

At time of writing, this should do the trick: during deployment, the Heroku should run npm install and then npm build, which runs Gulp in cookiecutter-django.

If things don’t work, please refer to the Heroku docs.