heroku, nginx, Python, Gunicorn, and Django

I just had to do this for a project at work and realized there wasn’t a lot of good direction out there for how to do it; here’s a minimal approach to getting you working with nginx in front of django requests via gunicorn. Most of the work is really done by ryandotsmith, this is just for Python instead of Ruby.

Buildpack directions

heroku config:set BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git
echo 'https://github.com/ryandotsmith/nginx-buildpack.git' >> .buildpacks
echo 'https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/python.tgz' >> .buildpacks
git add .buildpacks
git commit -m 'Added multi-buildpack nginx and python'

gunicorn.conf

import multiprocessing

def pre_fork(server, worker):
    f = '/tmp/app-initialized'
    open(f, 'w').close()

bind = 'unix:///tmp/nginx.socket'
workers = multiprocessing.cpu_count() * 2 + 1

Since New Relic has a free tier, and it’s so easy to use, I’m adding it here, too.

Procfile

web: bin/start-nginx newrelic-admin run-program gunicorn -c gunicorn.conf AppName.wsgi:application

Leave a Reply

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