Overview
Ok so I wanted to try serving Django from IIS 7.5 using Python 2.7. The advice available says to install PyISAPIe. Unfortunately there is not a compiled version of this available for Python 2.7 (x64 in my case). geographika has a great post on compiling one yourself. The following instructions are based on his guide, updated with newer versions. If you don’t want/can’t compile it, here is my compiled version: PyISAPIe-trunk-r201-Py2.7.2×64.zip
Install Python
I did this on Win2k8 R2 Standard with VS2010.
I used ActiveState’s Python from http://www.activestate.com/activepython/downloads (Windows 64bit 2.7.2.5)
Install Python
Compile PyISAPIe
(or you can skip this and unzip PyISAPIe-trunk-r201-Py2.7.2×64.zip into c:\PyISAPIe (remove the PyISAPIe-trunk-r201-Py2.7.2×64 folder)
cd \temp
# If you want to duplicate what I’m doing exactly add -r 201 to get the revision I got
svn co http://pyisapie.svn.sourceforge.net/svnroot/pyisapie/Trunk pyisapi-trunk
robocopy c:\python27 pyisapi-trunk\Python\x64\Python27 /mir
open pisapi-trunk\PyISAPI.sln in VS2010
Change target to Release|x64
Right click PyISAPIe project, Properties, Configuration Properties->C/C++
Change Preprocessor PYI_PY_VERSION=PY_27 to PYI_PY_VERSION=PY_27_2
Open PyISAPIe.h and insert the following after #define PY_27
#define PY_27_2 (PY_27 + 2)
Compile!
I got a couple of IntelliSense errors, but you can safely ignore those.
PyISAPIe.dll is produced under pyisapi-trunk\x64\Release
mkdir c:\PyISAPIe
copy pyisapi-trunk\x64\Release\PyISAPIe.dll c:\PyISAPIe
# Case may be important here
robocopy pyisapi-trunk\PyISAPIe\Python\Http c:\PyISAPIe\Http
Configure Website
Edit the properties -> security settings of c:\pyisapie\PyISAPIe.dll, add “Network Service” and tick “read” permission. (So IIS can use it).
# This is where we’ll put our python web app
mkdir c:\inetpub\PyApp
copy pyisapi-trunk\PyISAPIe\Python\Examples\Info.py c:\inetpub\PyApp
Open IIS Manager
Create new site called PyApp, set physical path to c:\inetpub\PyApp, set port to 8090 (or whatever is unused)
Select PyApp, Handler Mappings->Open Feature
Click Add Script Map” with the following setting
Request Path = *
Executable = C:\PyISAPIe\PyISAPIe.dll
Name = PyISAPIe
Ok now browse http://localhost:8090/info.py and it should work!
Install Django
Download Django 1.3.1 from https://www.djangoproject.com/download/
Extract and run python setup.py install
cd c:\inetpub\PyApp
# Create test Django project
django-admin.py startproject djangotest
Stick the following in djangotest\helloworld.py
from django.http import HttpResponse def index(request): return HttpResponse("Hello world!") |
Replace djangotest\urls.py with:
from django.conf.urls.defaults import * urlpatterns = patterns('', # Example: # (r'^newtest/', include('proj.apps.foo.urls.foo')), (r'^.*$', 'djangotest.helloworld.index'), # Uncomment this for admin: # (r'^admin/', include('django.contrib.admin.urls')), ) |
python djangotest\manage.py runserver
now browse to http://127.0.0.1:8000/ to make sure the internal Django server serves the site ok
Before Django will work in IIS, you need just a couple more steps.
copy /y c:\PyISAPe\Examples\Django\*.py c:\PyISAPIe\Http
Now edit C:\PyISAPIe\Http\Isapi.py after ‘Import DjangoMap’ add:
import os
# Probably a better way to do this, but this will get us going!
os.sys.path.append(‘c:\inetpub\PyApp’)
then jump down a bit and replace the following:
[“/1” , “mysite1.settings” ], # first Django app
with
[“/djangotest” , “djangotest.settings” ], # first Django app
Ok now try browsing http://localhost:8090/djangotest (or whatever port you configured the site on). You should see “Hello World!”. As geographika noted, you will need to recycle the PyApp app pool if you change any files on disk. I may try his modifications on DjangoMap.py’s Request method to get immediate feedback.
That seemed to have done it. I’ll try to post an update after I’ve been using it a bit.
Very Helpful Links
Setting up Python on IIS7 – http://geographika.co.uk/setting-up-python-on-iis7
https://code.djangoproject.com/wiki/DjangoOnWindowsWithIISAndSQLServer
Django Tutorial – https://docs.djangoproject.com/en/dev/intro/tutorial01/
My Compiled PyISAPIe for Python 2.7.2 x64 – PyISAPIe-trunk-r201-Py2.7.2×64.zip
I have problem my os Windows 2008 R2 x64
HTTP Error 404.17 – Not Found
The requested content appears to be script and will not be served by the static file handler.
I have been following your instructions here for running a django app under IIS. I have IIS 7.5 installed on a Windows 8 (64bit) machine with Django 1.5 and python 2.7 installed. When I follow your instructions above, at the point of adding the script handler I get “The specified module required by this handler is not in the modules list. If you are adding a script map handler mapping, the IsapiModule or the CgiModule must be in the modules list” Can’t go forward from this error, I am new to IIS, can you give me some advice? (I was using your compiled version of the PyISAPIe, not one I compiled).
Thanks, Gary.
@Gary Ridley
Hi Gary, I’ve been away from this setup for a while now. Unfortunately I can’t offer any advice on your issue. Sorry!
Solved the issue with ISAPI but now get a “404 Not found” error. Have you any idea why this might happen? All paths and such appear to be correct.
Gary.