How to Deploy Django Project to Heroku from Linux

Django python Heroku

The steps isn’t much different with other OS actually (Mac, Windows) it’s just that Linux is what I currently use. Also this article assuming that you install Django in virtualenv and you use Git for your version control. Ok, let’s do this.
The big picture on how to: You create and modify bunch of files required by Heroku (requirement.txt; Procfile; runtime.txt; yoursite/local_settings.py; yoursite/settings.py; yoursite/wsgi.py), install Heroku toolbelt, make Heroku account, pick a name for your application, then add your Git local repository to Heroku remote app, finally deploy to Heroku with Git. That’s about it, the details however is a bit another story.
Some details:
requirement.txt file
Go to your Django app virtualenv directory and install bunch of few required packages, then pip freeze it.

(your_env)$ pip install dj-database-url gunicorn whitenoise
(your_env)$ pip freeze > requirement.txt

These steps will create a file called requirement.txt with a list of your installed Python packages. Open requirement.txt file then add the following line.

psycopg2==2.5.4

That line needed for your app to work on Heroku. You might want adjust the version number to refer to whatever the latest.
Procfile requirement.txt file and add the following line and save it.

web: gunicorn yoursite.wsgi

Note: replace ‘yoursite’ with your Django project name (the one created by django-admin startproject command). That line means you’re going to be deploying a web application and you’ll do that by running command gunicorn yoursite.wsgi.
runtime.txt file
Create runtime.txt file in the same location with requirement.txt and add the following line and save it.

python-3.4.2

Adjust the number to reflect the Python version you use.

yoursite/local_settings.py
Create yoursite/local_settings.py to contain your DATABASE setup from your yoursite/settings.py.

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

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

DEBUG = True

Adjust the settings to reflect your setup.

yoursite/settings.py
Modify your website’s settings.py file by adding the following lines at the end of the file:

import dj_database_url
DATABASES['default'] = dj_database_url.config()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATIC_ROOT = 'staticfiles'

DEBUG = False

try:
		from .local_settings import *
except ImportError:
		pass

Then save it. Codes above will do the necessary configuration for Heroku and import all of your local settings if the file exists.

yoursite/wsgi.py
Add the following lines to yoursite/wsgi.py and save it.

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

Heroku account stuff

Big picture: install Heroku toolbelt, create your Heroku account and authenticate it.
Installing Heroku toolbelt
Just grab the install script using wget. If you use Linux distro other than Ubuntu/Debian (like Fedora, Arch, OpenSUSE etc etc), install Heroku toolbelt using the standalone installer. Make sure you install sudo package and configure your Linux system to use sudo (just in case your Linux distro don’t use sudo).
After installing Heroku toolbelt, it’s also useful to create bash alias for Heroku. Add the following to your bash alias.

alias heroku='/usr/local/heroku/bin/heroku'

Create your Heroku account
Sign up for your Heroku account here.
Authenticate your account

$ heroku login 

Deploy to Heroku

Big picture: pick a name for your Heroku application, add local repo to Heroku remote repo, push your local repo to Heroku repository, run database migration, tell Heroku to run your web.
Pick a name for your application

$ heroku create your_app_name_here

Add local repo to your Heroku remote repository

$ heroku git:remote -a your_heroku_app_name_here

Push your local repo to Heroku remote repo

$ git push heroku master 

Run database migration

$ heroku run python manage.py migrate

Tell Heroku to run your web app

$ heroku ps:scale web=1    # This tells Heroku to run just one instance of your web process. You might want to change this 
$ heroku open  # Visit your app in web browser 

That’s it. Hope it helps.

Sources:
Deploying with Git | Heroku Dev Center
Django Girls Tutorial [p.10] | Gitbook

Advertisements

Tags: , , , , , , , ,

About Ferry T.H.

I'm just your fellow human being.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: