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:
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”
Allow from all
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)))]
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.
Please follow these web pages:
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 *
myapp.models import *
That’s all. My Django project was up and running!