Running Django in IIS7/IIS8

So I’ve been working hard on Python and Django training materials lately. There’s a client that was in desperate need of somebody to provide this type of training, and I was fortunate enough to have an experienced Python/Ruby programmer on staff that fit the bill. He and I worked together to come up with plenty of examples and concepts to cover for a 5-day compressed course on Beginning Python and Django!

The most exciting part of the course was the final 2 days, where the students began to apply some of their newly acquired Python skills toward Django manage.py, settings.py, urls.py, and other files. During this course, I took it upon myself to think through the best way for a primarily Microsoft-centric server shop to be able to leverage the full capabilities of Python/Django in a way that didn’t feel limiting, wasn’t overly batch-script based, and didn’t have too many complex steps.

I found many articles on hosting Django within IIS… that were very difficult to follow. I eventually came across this article… which I spent quite a bit of time deciphering and understanding. The “batch script” nature of the article is what confused me the most. However, I distilled it down a bit into some IIS user-interface driven steps that my students had an easier time following along with. These are those steps on a Windows 7 machine, and should be nearly identical for Windows Server 2008 and above: (**note** assumes you’ve already installed Python version 2.7 and Django 1.4+ in C:\Python27 )

1. Make sure CGI is checked on box.

  • A. Control Panel
  • B. Programs and Features
  • C. Turn Windows features on or off
  • D. IIS
  • E. World Wide Web Services
  • F. CGI (Check)
  • G. Click OK

2. Go to IIS Manager
3. Select the web server
4. Double-Click Web Platform Installer a.k.a. WPI (you may not have it, so skip to step 7 if that’s the case)
5. If it asks to update, do so
6. If update fails, you need to download to install
7. http://www.microsoft.com/web/downloads/platform.aspx
8. When you can launch WPI, search for FastCGI
9. Select and install WFastCGI
10. If install fails, add a key under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node
This key’s path is Python\PythonCore\2.7\InstallPath
It’s value is C:\Python27\
11. Confirm that wfastcgi.py is in C:\Python27\Scripts\

  • Copy the wfastcgi.py file from C:\Python27\Scripts\ to your Django root site folder possibly at – C:\inetpub\wwwroot\[project site]

12. Go to web server, and ensure that IIS has icons for both CGI and FastCGI
This is at the root server level
13. Move your django project to wwwroot (assumes you knew how to create a Django project with django-admin.py startproject
)
14. Create a site in IIS. Give it some name. Point it to the path of your project. Give it a

port that won’t have conflicts, such as 8003
15. Double click Handler Mappings
16. Upper right-hand side, click Add Module Mapping
17.

  • Request path of *,
  • Module, select FastCgiModule,
  • Executable: “C:\Python27\python.exe|C:\inetpub\wwwroot\[project site]\wfastcgi.py”,
  • Name: “Django Handler”

18. Click Request Restrictions
19. On Mapping tab insure that Invoke handler only if request is mapped to, is NOT checked.
20. Click OK and Click OK
21. You will be asked if you want to add an entry to the FastCGI collection in IIS. Answer YES
22. Select the root server.
23. Double-Click FastCGI settings
24. You should have the Full Path to your Python.exe with an argument to your wfastcgi.py
25. Double-click it.
26. Click on Collection in Environment Variables property box and click the … button
27. Add 3 Environment variables:

  • DJANGO_SETTINGS_MODULE: [project name].settings,
  • PYTHONPATH:C:\inetpub\wwwroot\[project name] ,
  • WSGI_HANDLER: django.core.handlers.wsgi.WSGIHandler()

28. Click OK. then Click OK
29. navigate to http://localhost:8003

*** YOU’VE MADE IT TO THE END ***

Unfortunately, there’s more. There are some challenges getting Django to cough up “static files” which come in the form of *.css, *.jpg, *.js, and many… many… many more. As a result, you may need to create an “Application” within your newly created “Website” (used to be known as “Virtual Directory”). The trick here, is that once your Application is created… you need to navigate to the “Handler Mappings” module for that Application and “Remove” the Python-specific Http Handler that was created automatically. This will not affect the parent Web-Sites Handler-Mapping settings.

Here’s the complete video! (Quality is pretty bad… if you really want this video, let me know. I will give you a better quality link to it.)
http://youtu.be/kXbfHtAvubc

Happy configuration! Feel free to contact us if you’ve got questions!

One Response to Running Django in IIS7/IIS8

  1. Pingback: Debugging Django with Aptana | CodeSmart, Inc.

Our Capabilities Include:


Custom Software Development
Enterprise Architecture
Project Management
Systems Analysis
Performance Testing

AND THE LIST GOES ON...

These methods are vital to our work:


Agile Methodology
PMBOK
Test-Driven Development

LEARN WHY...

About CodeSmart, Inc.


CodeSmart has been locally owned and operated in the Olympia, WA area since 2002. We direct, design, develop and deliver full end-to-end information systems using leading edge Microsoft .Net technologies and recommended best practices.

LEARN MORE...