1

I currently set up a virtual host on Apache to try to host my Django project at django.localhost

My apache configurations are correct:

hobbes3@hobbes3:~/Sites/mysite$ apachectl configtest
Syntax OK

Here is my ~/Sites/mysite/apache/apache_django_wsgi.conf file, which gets included from /etc/apache2/httpd.conf

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName django.localhost
    ErrorLog "/var/log/apache2/django-error_log"

    <Directory /Users/hobbes3/Sites/mysite>
        AllowOverride All
        Options Indexes FollowSymLinks
        Order allow,deny
        Allow from all
    </Directory>

    WSGIDaemonProcess django.localhost processes=1 threads=1 maximum-requests=1
    WSGIProcessGroup django.localhost

    WSGIScriptAlias / "/Users/hobbes3/Sites/mysite/apache/django.wsgi"
</VirtualHost>

Here is my /User/hobbes3/Sites/mysite/apache/django.wsgi

import os
import sys

paths = [ '/Users/hobbes3/Sites/mysite',
          '/usr/local/lib/python2.7/site-packages',
]

for path in paths :
    if path not in sys.path :
        sys.path.append( path )

os.environ[ 'DJANGO_SETTINGS_MODULE' ] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I know at /usr/local/lib/python2.7/site-packages' I have both './django' and './MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg. I believe I installed them both using homebrew. And therefore /usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages points to the same location as well (I don't know how though).

When I run python manage.py runserver, I know MySQLdb is working, but I try to visit django.localhost, I get two similar errors about unable to find MySQLdb (I truncated the date and time for readability):

mod_wsgi (pid=76282): Exception occurred processing WSGI script '/Users/hobbes3/Sites/mysite/apache/django.wsgi'.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 59, in technical_500_response
    html = reporter.get_traceback_html()
  File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 151, in get_traceback_html
    return t.render(c)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 123, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 117, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))
  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 73, in render_node
    result = node.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 90, in render
    output = self.filter_expression.resolve(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 536, in resolve
    new_obj = func(obj, *arg_vals)
  File "/usr/local/lib/python2.7/site-packages/django/template/defaultfilters.py", line 695, in date
    return format(value, arg)
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 285, in format
    return df.format(format_string)
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
    pieces.append(force_unicode(getattr(self, piece)()))
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 191, in r
    return self.format('D, j M Y H:i:s O')
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
    pieces.append(force_unicode(getattr(self, piece)()))
  File "/usr/local/lib/python2.7/site-packages/django/utils/encoding.py", line 71, in force_unicode
    s = unicode(s)
  File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 206, in __unicode_cast
    return self.__func(*self.__args, **self.__kw)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 81, in ugettext
    return _trans.ugettext(message)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 286, in ugettext
    return do_translate(message, 'ugettext')
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 276, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 185, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 162, in _fetch
    app = import_module(appname)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 3, in <module>
    from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/helpers.py", line 3, in <module>
    from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/util.py", line 1, in <module>
    from django.db import models
  File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 78, in <module>
    connection = connections[DEFAULT_DB_ALIAS]
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 33, in load_backend
    return import_module('.base', backend_name)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
TemplateSyntaxError: Caught ImproperlyConfigured while rendering: Error loading MySQLdb module: No module named MySQLdb
mod_wsgi (pid=76283): Exception occurred processing WSGI script '/Users/hobbes3/Sites/mysite/apache/django.wsgi'.
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 169, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 59, in technical_500_response
    html = reporter.get_traceback_html()
  File "/usr/local/lib/python2.7/site-packages/django/views/debug.py", line 151, in get_traceback_html
    return t.render(c)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 123, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 117, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))
  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 73, in render_node
    result = node.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 90, in render
    output = self.filter_expression.resolve(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 536, in resolve
    new_obj = func(obj, *arg_vals)
  File "/usr/local/lib/python2.7/site-packages/django/template/defaultfilters.py", line 695, in date
    return format(value, arg)
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 285, in format
    return df.format(format_string)
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
    pieces.append(force_unicode(getattr(self, piece)()))
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 191, in r
    return self.format('D, j M Y H:i:s O')
  File "/usr/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 30, in format
    pieces.append(force_unicode(getattr(self, piece)()))
  File "/usr/local/lib/python2.7/site-packages/django/utils/encoding.py", line 71, in force_unicode
    s = unicode(s)
  File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 206, in __unicode_cast
    return self.__func(*self.__args, **self.__kw)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 81, in ugettext
    return _trans.ugettext(message)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 286, in ugettext
    return do_translate(message, 'ugettext')
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 276, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 185, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 162, in _fetch
    app = import_module(appname)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 3, in <module>
    from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/helpers.py", line 3, in <module>
    from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/util.py", line 1, in <module>
    from django.db import models
  File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 78, in <module>
    connection = connections[DEFAULT_DB_ALIAS]
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 33, in load_backend
    return import_module('.base', backend_name)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
TemplateSyntaxError: Caught ImproperlyConfigured while rendering: Error loading MySQLdb module: No module named MySQLdb

Sorry about the long error log, but I wanted to be complete. What am I doing wrong for this configuration?

2
  • 2
    Using maximum-requests=1 is a bad idea, even in development under mod_wsgi. Also don't define processes=1. It defaults to one process and using 'processes' option for any value technically flags it as a multi process system to WSGI, which isn't always what you want. Commented Feb 28, 2012 at 4:29
  • Thanks for the tip. But I still need to solve the above error. Commented Feb 28, 2012 at 5:11

1 Answer 1

2

Set PYTHON_EGG_CACHE in WSGI script to a location that user that Apache runs your code as can write to.

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User

The application can't unpack the Python egg file anywhere.

Sign up to request clarification or add additional context in comments.

2 Comments

I created a folder pylons under /usr/local/ then I created another folder python-eggs under pylon. Then I did chown -R hobbes3:staff /usr/local/pylons/python-eggs, because under my httpd.conf the Apache user is hobbes3 and group is staff. But that didn't seem to do anything. Am I missing something?
Are you saying you have overridden User/Group in your system Apache installation to be hobbes3 and staff? That would not be normal. If you wanted to run the Python web application process as that you would have just used options to WSGIDaemonProcess. Did you also set PYTHON_EGG_CACHE? Update your question with latest details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.