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.
- Go to python.org
- Download the latest Python 2.7 for Windows
- 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:
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
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.
pip install virtualenvfrom the Command Prompt
- Open/create the directory that will contain your project and
virtualenv virtual_environmentto create a new environment
virtual_environment\Scripts\activateto activate the environment
deactivateto 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.
You can check the virtual environment is working by opening a command prompt and
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
- With the virtualenv deactivated, run
pip install django==1.9to install globally
django-admin.py startproject django_on_iisto setup the files for the new Django project
- Ensure the virtualenv is activated and then run
pip install django==1.9to install to the virtualenv
- CD into the project directory and run
python manage.py runserver
- Open a browser at http://localhost:8000 to check it’s working
- 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
django-admin.py startproject command.
You should be able to activate the virtual environment, run
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.
- With the project’s virtualenv activated, run
pip install wfastcgi
- Then run
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”
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)
- Open a browser at http://localhost:8001/ and you should see a 500.19 error saying the config is invalid
- Open ‘Windows\System32\inetsrv\config’ directory
- Open the ‘applicationHost.config’ file with Notepad
- Scroll to the bottom of the page and add the “Locations” snippet below before the </configuration> end tag and save the file
- Open “Application Pools” in IIS and select “DjangoOnIIS” then click “Recycle”
- Go to “Sites” in IIS and select DjangoOnIIS then click “Restart”
Why is the config invalid?
On the 500.19 error message, the Config Source shows the
You should now be able to open a browser to http://localhost:8001/ and see the Django start page.