Python + Django + Passenger


After over 24 hours and not receiving a response from DreamHost support for the ticket I submitted, I cancelled the ticket and just started playing around trying to do it myself. I was able to setup my after a bad url was registered with my account. I am now unable to get Passenger running properly with my Python 2.7.5 & Django 1.5.1.

I keep getting a 500 internal error and logging doesn’t seem to be telling me why.

import sys, os
log = file('/home/username/', 'a')
print >>log, "Running %s" % (sys.executable)
cwd = os.getcwd()
sys.path.append(cwd + '/public/GearTracker')  #You must add your project here or 500
sys.path.append(cwd + '/public')
#Switch to new python
#You may try to replace $HOME with your actual path
if sys.version < "2.7.5":
	os.execl("/home/username/env/bin/python","python2.7.5", *sys.argv)
os.environ['DJANGO_SETTINGS_MODULE'] = "GearTracker.settings"
import django.core.handlers.wsgi
	application = django.core.handlers.wsgi.WSGIHandler()
except Exception, inst:
	print >>log, "Error: %s" % str(type(inst))
	print >>log, inst.args



[Wed May 22 09:13:30 2013] [error] [client IP] Premature end of script headers: [Wed May 22 09:13:30 2013] [error] [client IP] Premature end of script headers: internal_error.html

folder structure -username/ ------passenger_wsgi.log ------public/ --------GearTracker/ ----------etc... --------inventory/ --------accounts/ --------static/ --------wsgi/ --------templates/

This is my first django site setup on a server without using Heroku. How do I get my site to load? I use PyCharm and that IDE setup my folder structure like this.


Can you PM me (email button at the bottom of my post) with the URL of your site? I can take a look at some server logs to try and figure out what’s going on here but I’ll need to know where it is first.


Okay, got your message — here’s the error message you weren’t able to see before:

File "/dh/passenger/lib/phusion_passenger/wsgi/", line 59, in main_loop self.process_request(env, input_stream, client) File "/dh/passenger/lib/phusion_passenger/wsgi/", line 164, in process_request result =, start_response) File "/home/username/env/lib/python2.7/site-packages/django/core/handlers/", line 236, in __call__ self.load_middleware() File "/home/username/env/lib/python2.7/site-packages/django/core/handlers/", line 51, in load_middleware mod = import_module(mw_module) File "/home/username/env/lib/python2.7/site-packages/django/utils/", line 35, in import_module __import__(name) File "/home/username/env/lib/python2.7/site-packages/django/contrib/auth/", line 3, in <module> from django.contrib.auth.backends import RemoteUserBackend File "/home/username/env/lib/python2.7/site-packages/django/contrib/auth/", line 3, in <module> from django.contrib.auth.models import Permission File "/home/username/env/lib/python2.7/site-packages/django/contrib/auth/", line 8, in <module> from django.db import models File "/home/username/env/lib/python2.7/site-packages/django/db/", line 40, in <module> backend = load_backend(connection.settings_dict['ENGINE']) File "/home/username/env/lib/python2.7/site-packages/django/db/", line 34, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/home/username/env/lib/python2.7/site-packages/django/db/", line 93, in __getitem__ backend = load_backend(db['ENGINE']) File "/home/username/env/lib/python2.7/site-packages/django/db/", line 27, in load_backend return import_module('.base', backend_name) File "/home/username/env/lib/python2.7/site-packages/django/utils/", line 35, in import_module __import__(name) File "/home/username/env/lib/python2.7/site-packages/django/db/backends/mysql/", line 17, in <module> raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) <class 'django.core.exceptions.ImproperlyConfigured'>: Error loading MySQLdb module: No module named MySQLdb

It looks as though you don’t have MySQLdb set up in your virtualenv. You’ll need to install it.


doh~! thank you for helping and the fast reply! thought I had those installed!


The answer to this would have been useful to the entire forum…


Don’t know what you mean… He stated what the issue was. MySQLdb was not installed as a package…



[Wed May 22 09:13:30 2013] [error] [client IP] Premature end of script headers: [Wed May 22 09:13:30 2013] [error] [client IP] Premature end of script headers: internal_error.html

The error log is as useful as tits on a bull.


I have a very similar issue, but I do have MySQL installed and Django is working properly.

My issue is that Django isn’t using Python 2.7.5 which I have installed on my account. As soon as I add the following lines to my file, I get a 500 server error and log errors are the usual unhelpful “Premature end of script headers.”

INTERP = "/home/egeyer/opt/bin/python2.7"
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)

I know python is installed and working correctly in that location.

Any help is much appreciated!

  • Erin
    I have also tried “/home/egeyer/opt/bin/python” as the location and still the same 500 error.
    Ok! I just figured it out myself… I had not installed MySQL-python… I thought I had, but I guess I had only downloaded and unpacked it and never gone through with installation, so I did and now Django is running the correct python version.


I have exactly the same issue as the original poster. I get those cryptic “Premature end of script headers” messages in my error log but no more information about the real issues causing them.

How can I see the error logs that show the real issue? That is the question that was left unanswered.

(I have MySQLdb installed so that’s not the issue.)