Setting up mod_wsgi for apache and Django on Windows

I just installed mod_wsgi for apache on windows and faced some problems. Since, I have to spent some time in sorting them out, I thought, I will write them down.

1. Goto modsgi Installation Windows page.

Download the file ‘mod_wsgi.so’ for your combination of python and apache versions. The site lists out various options like:

mod_wsgi-2.5 – Apache 2.2 / Python 2.6

mod_wsgi-2.3 – Apache 2.0 / Python 2.5

mod_wsgi-2.3 – Apache 2.2 / Python 2.5

mod_wsgi-2.3 – Apache 2.0 / Python 2.4

2. Save the file into your apache modules directory. For me, it was located in C:\Program Files\Apache Software Foundation\Apache2.2\modules.

We will first check if ‘wsgi’ installation is ok or not before heading towards Django/apache configuration. If you wish to omit these steps, no problem. This is because, I  wanted to make sure that wsgi is installed correctly before heading for my Django/Windows configuration.

3. Create a directory outside of your webroot that will host your mod_wsgi-application, for simplicity’s sake, I choose C:\wsgi_app directory.

4. Locate your httpd.conf-file and open it up in a text editor, it’s usually located in the conf-directory of your apache installation, for me it was: C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf

5. Search for “LoadModule” statements in httpd.conf. Put the following statement on a seperate line, let’s say, after all LoadModule statements:

LoadModule wsgi_module modules/mod_wsgi.so

6. Now, find the <Directory>  block in the httpd.conf for your web-root, for me it was “C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf” . Just below it, add this block:

WSGIScriptAlias /wsgi “C:/wsgi_app/wsgi_handler.py”

<Directory “C:/wsgi_app”>
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>

See, we are making of use of ‘/’ for specifying directories in windows !!

/wsgi part is the apache alias we want to use for our wsgi application. wsgi_handler.py acts as entry point to wsgi application.

7. Now, add this code to wsgi_handler.py and put it in C:\wsgi_app

def application(environ, start_response):

status = ‘200 OK’
output = ‘Hello World!’

response_headers = [(‘Content-type’, ‘text/plain’), (‘Content-Length’, str(len(output)))]
start_response(status, response_headers)

return [output]

8. Now, restart apache and go to http://localhost/wsgi. Hopefully, you will not encounter any errors!! The output will be ‘Hello World’ in the browser.

Now, let us turn over to apache/django configuration. I am assuming that you have already downloaded and installed ‘apache‘ and ‘django‘ and they are running successfully.

Please follow these web pages:
http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/#howto-deployment-modwsgi
http://code.djangoproject.com/wiki/django_apache_and_mod_wsgi
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

These pages give details of steps to be followed in configuring Django and mod_wsgi/apache combination.

In following these pages, I encountered some errors like:

mod_wsgi (pid=2200): Exception occurred processing WSGI script ‘E:/home/django/myproject/apache/django.wsgi’.
[Tue Jun 09 15:09:21 2009] [error] Traceback (most recent call last):
[Tue Jun 09 15:09:21 2009] [error] File “E:/home/django/myproject/apache/django.wsgi”, line 1, in
[Tue Jun 09 15:09:21 2009] [error] import os, sys
[Tue Jun 09 15:09:21 2009] [error] ImportError: No module named os

This error was generated as Django was not getting the correct path for PYTHON interpreter through PYTHONPATH environment variable.

In windows, this ‘PYTHONPATH’ key is stored in Windows registry under HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore.

Open ‘regedit’ windows utility through command line window and make sure that python interpreter path specified is correct or edit it to make python interpreter available in the path.

The next error occured was related to settings.py module in my django project.

pathImportError: Could not import settings ‘myproject.settings’ (Is it on sys.path? Does it have syntax errors?): No module named myproject.settings
[Tue Jun 09 17:20:56 2009] [error] [client 127.0.0.1] mod_wsgi (pid=4052): Exception occurred processing WSGI script

There was one extra ‘/’ in my sys.path line in django.wsgi file. So, you have to follow the instructions in the above pages carefully.

I encountered one more error related to models in my views.py file in my application. I am not able to trace the log to reproduce here…

But it was something related to ‘No Models.xxx could be found’ I just appended my project name before module name to get rid of it.

So, basically, you do

from myproject.myapp.models import *

Instead of

myapp.models import *

That’s all. My Django project was up and running!

About these ads

Tags: ,

16 Responses to “Setting up mod_wsgi for apache and Django on Windows”

  1. Graham Dumpleton Says:

    The error ‘No module named os’ suggests that Python was not installed for all users on the machine but only yourself. For things to work completely okay, needs to be installed for all users so that machine wide registry entries and not per user registry entries are setup. This is all necessary as Apache service would typically run as special user and not you. Therefore cannot access your user specific registry entries. That said, why you would still have some machine wide entries, albeit incorrect, I am not sure. Maybe you have installed Python many times over time and they were old entries from a time when you installed it differently or in a different location. Some times third party applications can carry their own Python installation and they may also setup registry entries, but they would be specific for their own Python installation and may not be correct for your main Python installation.

    • pradyumnajoshi Says:

      After reading your comments in one of the blogs ( You are very helpful for any wsgi related issues.. great!!), I had installed Python 2.5 for all the users on windows. But still, the error was cropping up. I don’t know the exact reason. May be, my registry was somehow corrupt. I will investigate further. After adding PYTHON interpreter path to PYTHONPATH, the error ‘No module named os’ disappeared.

  2. w4R Says:

    You have to add indentation to 7., otherwhise it won’t work.

  3. Madalin Says:

    Hello, great article
    I have a problem! I’ve got Apache2.2, Python 2.6, and mod_wsgi-2.5 – Apache 2.2 / Python 2.6. After copiyng the module into: \modules, I edit the conf file of apache, Load mod_wsgi module and restart apache.
    Apache does not start anymore. In event viewer it says that cannot find mod_wsgi module. The module is there, access is granted to ‘everyone’.
    I don’t know what to do else ! Please help.

  4. Madalin Says:

    Hello,
    My platform is a virtual machine with Win XP SP 2. If I comment that line with “LoadModule … modules/mod_wsgi.so” Apache server is starting, so that means mod_wsgi module is the one that creates this issue.

    Thank you.

  5. Picus Says:

    Madalin,

    When you download the .so file, it probably has a name like “mod_wsgi-apache22-python26-mod-wsgi2.5.so”

    You have two options, either use the exact name in the httpd.conf file or go into the modules directory and simply change the file name to mod_wsgi.so.

    The error is pretty basic, if there is a file name mismatch then technically, apache is looking for a file that doesn’t exist.

  6. Larry Henrie Says:

    Thanks for this blog. I am new at django and this is a big help.

  7. pradyumnajoshi Says:

    Perhaps, you can download from : http://modwsgi.googlecode.com/files/mod_wsgi-win32-ap22py26-2.4c1.so

    Then, either use the exact name in the httpd.conf file or go into the modules directory and simply change the file name to mod_wsgi.so for statement:
    LoadModule wsgi_module modules/mod_wsgi.so

    Cheers,
    Joshi

  8. yyzhang Says:

    I also enconter the same problem. I want to install django in apache in xp, I have tested the mod_wsgi,it actually works. But after when I replaced the test.wsgi with the configure python file, the error about import os happened. I checked my register file, and found I installed the python for current user but not for all user. So I checked out the item HKEY_CURRENT_USER\Software\Python into a .reg file, and opened it and replaced HKEY_CURRENT_USER by HKEY_LOCAL_MACHINE. Finally, I registered the .reg file, apache started to work.

  9. How to deploy a Django page on the Apache HTTP Server on Windows | andersguide Says:

    […] If you run into trouble, this link may contain some solutions: http://pradyumnajoshi.wordpress.com/2009/06/09/setting-up-mod_wsgi-for-apache-and-django-on-windows/ […]

  10. the blues Says:

    Hello,
    I have error when starting apache (via cmd) after edit httpd.conf (add WSGIScriptAlias…. until ). The apache feedback with notification like this : C:\webServerApache22\bin>httpd -k start
    httpd: Could not reliably determine the server’s fully qualified domain name, us
    ing 127.0.0.1 for ServerName
    (OS 10048)Only one usage of each socket address (protocol/network address/port)
    is normally permitted. : make_sock: could not bind to address 0.0.0.0:80
    no listening sockets available, shutting down
    Unable to open logs… My platform is win7, with apache ver 2.2. Appreciate for any help.

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: