24

I recently tried to export a Django project on OpenShift, but fruitlessly. The only solutions I found were "prebuilt" ones (such as https://github.com/openshift/django-example).

I spent some hours trying to adapt it to my project but I always got an Internal Server Error.

So, how to setup django on openshift?

1 Answer 1

30

I finally figured it out. The first thing to do is to start an openshift app and edit the setup.py file :

rhc app create -a APPNAME -t python-2.6
cd APPNAME
vim setup.py

You need to uncomment "install_requires=['Django>=1.3']"

Then you can commit to the server :

git commit -a -m "Initialization"
git push

By default, it installs django 1.4 but I think you can choose another version with the correct install requirement in setup.py. Anyway, you'll have to run the same django version on your computer and the server for the following.

Create your django project :

cd wsgi
django-admin.py startproject PROJECTNAME

Then you'll have to edit the file application, replace the whole content by :

#!/usr/bin/python
import os, sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'PROJECTNAME.settings'
sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi',
    'PROJECTNAME'))

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/activate_this.py')

try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass

#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
# 
from django.core.handlers import wsgi
application = wsgi.WSGIHandler()

Finally, you can commit the modifications :

cd ..
git add .
git commit -a -m "Project Creation"
git push

You should see the django welcome page. Now you can edit the settings and import your django apps without unwanted content

5
  • 4
    Great work. Thanks! Minor extension to the steps above. 1. In order to have your static files served, you need to put them in PROJECTNAME/wsgi/static. 2. In PROJECTNAME/wsgi/static you should also place the admin folder from site-packages/Django-xxx/django/contrib/admin/static/admin. Otherwise you will miss the static Django Admin files.
    – orschiro
    Jul 24, 2013 at 6:26
  • 5
    Unfortunately when I tried your solution, I got an 503 Service Temporarily Unavailable error. I edited PROJECTNAME as per my project name and edited the line with python2.7. There seems to be a huge lack of project documentation for OpenShift :(
    – Shailen
    Aug 7, 2013 at 21:32
  • i know you just edited this file but still thanks for the GREAT WORK :))))
    – suhailvs
    Oct 1, 2013 at 7:24
  • how about adding the requirements file, using virtualenvwrapper?
    – Ram Kumar
    Dec 24, 2013 at 2:19
  • I tried this. I don't know if its a common issue but the wsgi.py doesn't appear in a folder of its own and now is root. Trying these suggestions is causing the same issue as @shailenTJ where I get a 503 error. Has the wsgi format changed considerably?
    – disruptive
    Jul 3, 2014 at 11:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.