March 23, 2011

Python + Apache with Mod_wsgi in Windows

I intended to write this technical note for fun and possibly as my reference in the future. I have tested it on my Windows box as my consideration that there has been a lot of similar stuff written for Linux. I often use Windows for my previous small projects as the client required.

Installing python, django and any other stuffs that needed to make it work are beyond the scope of this note since I think that there's no big challenges to make them work perfectly, especially in development environment. Challenges will occur as developers run into production.


My resources:
Windows XP Pro SP3
Apache 2.2.17 (WAMP 2.1)
mod_wsgi
Python 2.7

My apache's document root is located at: c:/wamp/www. I created a directory named 'wsgi', so the full path is: c:/wamp/www/wsgi. I assumed that it would be accessed via http://localhost/wsgi/.

I added a file called dispatcher.py inside the wsgi directory. Here's a piece of code within 'dispatcher.py':


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]
The next step is to modify apache configuration (httpd.conf) in order to make it recognize python applications. The following lines are appended to httpd.conf and require that mod_wsgi has been installed in c:\wamp\bin\apache\Apache2.2.17\modules\mod_wsgi.so.

LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias /wsgi "c:/wamp/www/wsgi/dispatcher.py"
<Directory "c:/wamp/www/wsgi">
 Allow Override None
 Options None
 Order deny,allow
 Allow from all
</Directory>


Well, It's just a simple code to print 'Hello World' to make sure that mod_wsgi + apache is working seamlessly and we are ready for the next adventures.

7 komentar:

  1. Hi,

    Thanks for the post on this!

    I have Wampserver installed and it's working properly. I also have Python and Django both installed.

    I downloaded mod_wsgi-win32-ap22py27-3.3.so - Apache 2.2 / Python 2.7, renamed it to mod_wsgi.so and copied it to C:\wamp\bin\apache\Apache2.2.11\modules.

    I added dispatcher.py to my wsgi folder located at C:\wamp\www\wsgi, but when I add:

    LoadModule wsgi_module modules/mod_wsgi.so
    WSGIScriptAlias /wsgi "c:/wamp/www/wsgi/dispatcher.py"

    Allow Override None
    Options None
    Order deny,allow
    Allow from all


    to apache's httpd.conf file, my Wampserver tray menu goes yellow, and gives me an error message when I try to put it online.

    The error I get says:
    Aestan Tray Menu
    Could not execute menu item (internal error)
    [Exception] Could not perform service action: the service has not been started

    Wampserver 2.0
    Apache 2.2.11
    PHP 5.3.0
    MySQL 5.1.36

    Any ideas?

    ReplyDelete
  2. please check your apache's error.log first to get more detail information about the problems.

    ReplyDelete
  3. Thanks for this post.
    In my case I had to use the following code, just place it at the end of your httpd.conf:

    <VirtualHost *:80>

    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin admin@example.com

    DocumentRoot c:/wamp/www

    <Directory c:/wamp/www>
    Order allow,deny
    Allow from all
    </Directory>

    WSGIScriptAlias /wsgi c:/wamp/www/wsgi/dispatcher.py

    <Directory c:/wamp/www/wsgi>
    Order allow,deny
    Allow from all
    </Directory>

    </VirtualHost>

    ReplyDelete
  4. wowlo wolo ti :p

    ReplyDelete
  5. I am having the same issue as Matthew Edward. I am running slightly later version of WAMP (v2.2), but issue remains the same. Nothing gets written in error logs as apache fails to start completely unless I comment out #LoadModule wsgi_module modules/mod_wsgi.so

    Any clues?

    ReplyDelete
  6. I also get the same error as Matthew and Slavko. and nothing is in the error log..

    ReplyDelete

Leave comments.