Deploying django 1.4 on openshift

Openshift is a PaaS solution by RedHat that allow developers to focus on their application rather than their infrastructure. If you have ever tried Heroku, you will feel comfortable since your first steps into it.

Depending on your app needs, you have a set of cartridges that you can use to build a service oriented infrastructure. For example, if you want to create a tipical website with django, you will probably need a python26 web cartridge and a mysql (or postgresql or mongodb) cartridge.

There are 2 ways to manage your applications on openshift: the rhc ruby gem and the web console. This choice is up to you, with both the methods you can create the needed cartridges for your python/django application. Follow the openshift django tutorial to create the basic python wsgi app.

The project directory you created with the openshift tutorial has the following layout:


Let’s start with the file: we need to put django in the requirements

from setuptools import setup

      description='OpenShift App',
      author='Your Name',

You will find by default a django>=1.3 commented, so uncomment it and change the minimum version.

Now it’s time to create our django project. The wsgi directory is the place where all your python code will be kept, so cd into it and execute

$ startproject openshiftdjango .

You can then see the following output from ls:


Open you application file and replace its content with:

#!/usr/bin/env python
# some original codes we need
import os
virtenv = os.environ['APPDIR'] + '/virtenv/'
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python2.6/site-packages')
virtualenv = os.path.join(virtenv, 'bin/')
    execfile(virtualenv, dict(__file__=virtualenv))
# new codes we adding for Django
import sys
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = os.environ['OPENSHIFT_APP_NAME']+'.settings'
sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi' ))
application = django.core.handlers.wsgi.WSGIHandler()

Please note that I adapted this code from the django example provided by redhat on their openshift github account. As you can see I just added to the python path the wsgi directory (instead of the django project directory) and used the OPENSHIFT_APP_NAME to locate the django settings module.

At this point you have already done most of the job. If you commit and push these changes back to the remote repo, you should be able to see the default django page pointing to you application url.

As a final step you can change your settings file to take in account the openshift environment variables:

  • OPENSHIFT_REPO_DIR –> True if running on openshift
  • OPENSHIFT_DATA_DIR –> Points to a remote directory (data) that persists between different builds. Useful to host a sqlite db or any file generated by your application (so you can point your MEDIA_ROOT here)
  • all the db-related variables

You can find a sample project on my bitbucket account

Do you have any suggestions about deplying django on openshift? Feel free to disqus it!

Published: November 15 2012

blog comments powered by Disqus