In most cases, I would recommend using a 'unix' OS and nginx for running your Django application. However, there are times that it's necessary for you to use Windows and IIS.
This is a compilation of a few tutorials and Stack Overflow answers. I've referenced them at the section in the bottom.
I assume the following has already been setup:
- IIS 7+ with the CGI module is installed on your machine
- Python 2.7+/3.4+ with pip and django are already installed
- You already have an example Django application and know how to edit it
If you don't have IIS installed:
- How do I install IIS on Windows 8 or Windows 10?
- How do I install IIS on Windows Server 2012?
- How do I install the CGI Module?
If you don't have a Django application created and don't know how, then I suggest reading through Django's documentation which is not only very documented, but also has quite a few useful tutorials. Note: I've linked to the 'dev' branch of documentation to make this tutorial a little more relevant over time. However, it's important you use the documentation of the actual version of Django you have installed.
Note: This guide works for native and environment based Python installation. I personally used an Anaconda Environment without any issues. It's assumed from the rest of this guide, that you're working in this environment.
Installation and Setup
There are two major steps to take when installing and setting up Django to be used on IIS.
- Installing and enabling wfastcgi
- Configuring IIS for Django
Installing and enabling
pip install wfastcgi
First off, we need to install
wfastcgi using pip (or you preference of installation method):
wfastcgi was created by the Python Tools for Visual Studio people, but PTVS isn't required. If you find any issues with
wfastcgi you can file a report on their Github repo.
After installing you need to enable
wfastcgi you need to enable it:
You may need to use the full file path:
Configuring IIS for Django
- Add Handler Mapping
- Set Root folder for IIS Site
A file called
web.config should be created in root folder of you Django Site (same location as your
manage.py file). Here's a template:
WSGI_HANDLER: this configuration is dependent on the version of Django you're running:
- Django 1.7+:
Here are a few issues that I can into while setting this up.
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
If you get this error, then that means you're probably using the wrong
WSGI_HANDLER option for your version of Django.
Exception: WSGI_HANDLER env var must be set
This means that your
web.config doesn't have the correct app keys configured and is missing the
WSGI_HANDLER environment variable.
Within the folder that you store your
static files you'll need to add this