1. Prerequisites

Ensure that you have IIS installed with CGI. You can do this on Windows Server 2008 by opening the Server Manager then clicking “Add roles” and selecting “Web Server (IIS)” in the “Server Roles” section of the wizard, followed by checking “CGI” under “Web server > Application Development” in the “Role Services” section of the wizard.

2. Install Python and Pip


The first thing to do is ensure that Python is installed together with the Pip package manager (to install packages ike Django). Pip should already be installed if you install the latest version of Python.

  1. Go to python.org
  2. Download the latest Python 2.7 for Windows
  3. Install it, remembering to set “Add python.exe to Path” to the “Will be installed on local hardrive” option in the “Customize Python 2.7.xx” section

Note that if an error message appears during installation, you may be able to fix it by setting the “pip” feature to “Entire feature will be unavailable” and installing only Python. With Python installed you can then run the installer again, selecting the “Change Pythom 2.7.xx” option and then setting pip to “Will be installed on local hardrive” in the “Customize Python 2.7.xx” section.

Also note that if you forget to set “Add python.exe to Path” then you can set the PATH by running the following command in Powershell: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User")

Expected Outcome

You should now be able to open the Command Prompt and run python to bring up the Python REPL (note that if you had it open before then you will need to close it an open it again for the correct path to the Python interpreter to be used).

You should also be able run pip in the Command Prompt to check that Pip is installed.

3. Install Virtualenv and create a Virtual Environment for the Project


Virtualenv enables you to keep the dependencies for your project in an isolated environment, allowing you to work on several Python projects on the same machine without worrying about their dependencies clashing.

  1. Run pip install virtualenv from the Command Prompt
  2. Open/create the directory that will contain your project and run virtualenv virtual_environment to create a new environment
  3. Run virtual_environment\Scripts\activate to activate the environment
  4. Run deactivate to exit out of the virtual environment

When the environment is activated you should see its name in parentheses on the left of the Command Prompt. Anything that you install with Pip when it’s activated will only be accessible from this environment.

C:\> mkdir C:\Django
C:\> mkdir C:\Django\DjangoOnIISProject
C:\> cd C:\Django\DjangoOnIISProject
C:\Django\DjangoOnIISProject> virtualenv virtual_environment
C:\Django\DjangoOnIISProject> virtual_environment\Scripts\activate
(virtual_environment) C:\Django\DjangoOnIISProject>
(virtual_environment) C:\Django\DjangoOnIISProject> deactivate

Expected Outcome

You can check the virtual environment is working by opening a command prompt and first running pip freeze without it activated, then activating it and running pip freeze again. The packages listed should be different (virtualenv should be listed when not activated and not listed when it is).

4. Install Django


  1. With the virtualenv deactivated, run pip install django==1.9 to install globally
  2. Run django-admin.py startproject django_on_iis to setup the files for the new Django project
  3. Ensure the virtualenv is activated and then run pip install django==1.9 to install to the virtualenv
  4. CD into the project directory and run python manage.py runserver
  5. Open a browser at http://localhost:8000 to check it’s working
  6. Press ctrl-c in the Command Prompt to stop the server

Note that installing Django the first time globally is simply to enable you to use the django-admin.py startproject command.

C:\> pip install django==1.9
C:\> cd C:\Django\DjangoOnIISProject
C:\Django\DjangoOnIISProject\> django-admin.py startproject django_on_iis
C:\Django\DjangoOnIISProject\> virtual_environment\Scripts\activate
(virtual_environment) C:\Django\DjangoOnIISProject\> cd django_on_iis
(virtual_environment) C:\Django\DjangoOnIISProject\django_on_iis\> python manage.py runserver

Expected Outcome

You should be able to activate the virtual environment, run python manage.py runserver from the project directory and see the starting page for Django at http:localhost:8000/ in your browser.

Alternatively you can also check by running path\to\virtualenv\Scripts\python.exe path\to\manage.py runserver and this should do exactly the same.

5. Serve Django from IIS with FastCGI


The final step is to set up IIS to serve the Django project.

  1. With the project’s virtualenv activated, run pip install wfastcgi
  2. Then run wfastcgi-enable
  3. Open Notepad and copy the “web.config” xml below into it, then save it in the project directory (alongside the manage.py file) as:

    • File Name: “web.config”
    • Save as Type: “All files”
  4. Open IIS and add a new site (right-click on ‘sites’ and click ‘Add Website’) with the following settings:

    • Site name: ‘DjangoOnIIS’
    • Physical Path: ‘C:\Django\DjangoOnIISProject\django_on_iis’
    • Port: 8001 (or another unused port)
  5. Open a browser at http://localhost:8001/ and you should see a 500.19 error saying the config is invalid
  6. Open ‘Windows\System32\inetsrv\config’ directory
  7. Open the ‘applicationHost.config’ file with Notepad
  8. Scroll to the bottom of the page and add the “Locations” snippet below before the </configuration> end tag and save the file
  9. Open “Application Pools” in IIS and select “DjangoOnIIS” then click “Recycle”
  10. Go to “Sites” in IIS and select DjangoOnIIS then click “Restart”
<!-- web.config -->
<add name="DjangoOnIISHandler"
requireAccess="Script" />
<!-- Required settings -->
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
<add key="PYTHONPATH" value="C:\Django\DjangoOnIISProject\django_on_iis" />
<add key="DJANGO_SETTINGS_MODULE" value="django_on_iis.settings" />
<!-- Optional - uncomment to get logging (but make sure "Logs" dir is already created) -->
<!-- <add key="WSGI_LOG" value="C:\Logs\DjangoOnIISProject.log" /> -->
<!-- Locations -->
<location path="DjangoOnIIS" overrideMode="Allow">

Why is the config invalid?

On the 500.19 error message, the Config Source shows the tag highlighed - this is because IIS requires the web.config works in a hierarchy from other parent config files and overriding needs to be explicitly set in a parent (learn more about how this works here..).

Expected Outcome

You should now be able to open a browser to http://localhost:8001/ and see the Django start page.