Arul's Blog

Deploying Django application on Heroku

Sat 05 July 2014 By Arulmurugan Rajaraman comments

All the web developers would like to see their application hosted somewhere in the web where everybody can access it. When I tried to do the same for my django application, there was no free or cheap option to do that. And I didn't wish pay just for testing my application in a live environment. I tried many services like Alwaysdata, Google App Engine and Heroku. But all of them confused me in the beginning and have their own limitations.

Heroku

Later I worked on heroku in one my of projects and only then I understood how easy it is to deploy a django app in heroku. Here I'm giving step-by-step instructions to deploy a django app in heroku which will help lot more people like me.

I'm explaining the steps for deployment from using an Ubuntu system. But most of the methods will remain same irrespective of the operating system.

Versions Used

  • Django - 1.6.5
  • Git - 1.9.1
  • Ubuntu - 14.04

Step 1: Setup Heroku account

Go to Heroku site and create an account using your email address. This is free. No credit card or any other payment information required.

Step 2: Setup Heroku toolbelt

Install Heroku toolbelt in your system. The tool is available for all the most popular platforms. Login to your heroku account from the terminal using the command

heroku login

This will setup your heroku account in your system and will upload SSH key from your system to your heroku account.

Step 3: Setup Git

If you are not using git, download and install git from Git SCM. You can learn more about git from the presentation on Slideshare.

Step 4: Create and clone heroku app

Method 1: Login to your heroku account and create an application by giving a name of your choice. The URL of your heroku application will be yourappname.herokuapp.com. After creating the application, clone it to your system using the command

heroku git:clone -a yourappname

Method 2: You can also create heroku application using the command

heroku create yourappname

You can use any of these methods to create heroku application. If you don't specify your app name in command line, heroku will choose one for you.

Step 5: Configure your django project

Now we need to modify our django project to make it work in the heroku server. Before starting this make sure you are having pip installed in your system.

Step 5.1: Install django-toolbelt

Install django-toolbelt using the command

pip install django-toolbelt

This is a collection of the following packages: Django, psycopg2, gunicorn, dj-database-url, dj-static, static.

Step 5.2: Declare Procfile

Procfile will be used to decalre what command should be executed to start a web dyno. Create a text file in the root directory of your application with the following content

web: gunicorn yourappname.wsgi

This will be used to start your application during each deployment.

Step 5.3: Create requirements.txt

Create requirements.txt using the command

pip freeze > requirements.txt

in the root folder of your application, if you haven't done this already. This will be used to install the dependencies for your application.

Step 5.4: Configure settings.py

Configure your application's settings.py to suit the heroku environment. Include the following settings in your settings.py

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Make sure that you have all these settings defined only in one location and all dependencies installed.

Step 5.5: Configure wsgi.py

Open wsgi.py and replace the file content with the following content

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

This will be used to serve static files in production environment.

Step 6: Deploy to heroku

Copy your project content to the heroku app folder created in Step 4. Commit your changes using the command

git add .
git commit -am "Initial commit"

You can give a commit message of your choice. After committing, push the application to heroku using the command

git push heroku master

Sometimes you may have to use the command

git push origin master

Step 7: Run syncdb

You can run django management commands using the command heroku run python manage.py <command>. To sync your database, run the command

heroku run python manage.py syncdb

Step 8: Scale web dyno

Web dynos are processes that will run your application. You need to increase your app's web dyno to 1. Heroku offers only one web dyno for free. If you need more than one web dyno, you need to pay heroku. You can scale the web dyno using the following command

heroku ps:scale web=1

One web dyno is sufficient for testing an application or for personal use.

Step 9: Adding custom domain (optional)

If you like to use custom domain for your heroku application, you can do so by adding the domain to your herokuapp and configuring CNAME in your domain DNS editor. Add the domain to your heroku app using the following command.

heroku domains:add www.example.com

Configure CNAME in your domain DNS editor with host as www and Points to as yourappname.herokuapp.com.

Your django application should be live on heroku now. If you are facing any issues, you can check heroku logs using the command

heroku logs

If you are having more than one command in your heroku account, you can run a heroku command by specifying app using --app parameter as given below

heroku run python manage.py shell --app yourappname

Now we have successfully deployed a django application on heroku. Feel free to post your feedback and questions as comments.

comments powered by Disqus