Update 21.10.2016:

Meanwhile Microsoft provided the Python package wfastcgi. Instead of using the Microsoft Webplatform Installer you can install the package easily using pip which is more convenient. The configuration of the IIS is well documented on that Python package web page.

Python/Django Windows hosting with IIS 7+

We are perfectionists with deadlines so we use the Python/Django Web Framework for our web applications.

Since Microsoft established a Python development center on their cloud plattform Azure, the Windows plattform became an option for hosting Python/Django web applications. Also Microsofts SDK Visual Studio was overhauled and now supports Python development.

This enticed us to try a Windows Deployment of a Python/Django web application. For this task we will use the Microsoft webserver IIS (Internet Information Services) und provide step by step installation intructions.

We choose to use a dinosaur: Windows Server 2008 with an IIS 7 installation. We were curios if the new features are downwards compatiible and will work on old Windows plattforms.

I am sorry for the screenshot in German language. At that time I was writing that article, there was only a German Windows server installation available for me.

 

Software installation

IIS, Python and Django are the minimal requirements for the software installation. When deploying a more complex web application usually you want to install a database of your chioce (i. e. PostgrSQL, MySQL). We will focus for the sake of simplicity on the deployment of simple "Hello World!" sample project.

 

1. Python/Django

We install Python 2.7.9 with the Windows installer. Afterwards we install Django via the command line:

C:\inetpub> pip install django

Then we generate a new Django Projekt inside a directory (i. e. C:\inetpub):

C:\inetpub> django-admin.py startnewprojekt helloworld

The script generates the new project in C:\inetpub\helloworld. It's good practice not to serve that directory for security reasons. We will go into detail when we cover the IIS website cnfiguration.

You can run that project via the commandline now:

C:\inetpub\helloworld\>python manage.py runserver

 

2. IIS

You need to activate the IIS using the web server role inside the server manager. CGI module needs to be activated also.

In addition ff you want to host a Django web application you need install a gateway for FastCGI – PythonWSGI. You can do that either via the Microsoft Webplattform Installer (WPI) or a seperate installer. Old systems does't come with WPI, you need to install it. When starting the WPI search for WFastCGI. You need to install that option.

WPI installs the wfastcgi.py file in the Python standard directory C:\Python27\Scripts\. Python must be installed first otherwise WPI throws an exeption.

Copy the wfastcgi.py file into C:\inetpub\helloworld. Now create a new website "Helloword" using the IIS Server Manager. Use C:\inetpub\wwwroot as base directory.

Click on that website and open the handler bindings. Add a module binding. You need to provide following settings:

  • Request path: *
  • Module: FastCgiModule
  • Executable: C:\Python27\python.exe|C:\inetpub\helloworld\wfastcgi.py
  • Name: Helloworld
  • Restrictions: the checkbox “Invoke handler only if request is mapped to” is not checked (causes otherwise problems with URL Routing/Mapping)

When finished hit OK. A dialog shows up asking for adding a FastCGI application entry. Add that entry. All URL requests (*) will now be handled by the Django web application.(internal URL routing).

Select the main directory in the IIS Server Manger and click on the FastCGI settings icon. A list shows of where you can find the entry you just added. Click on that entry. A dialog shows up where you need to set the environment variables for our web application:

  • DJANGO_SETTINGS_MODULE: helloworld.settings

  • PYTHONPATH:C:\inetpub\helloworld

  • WSGI_HANDLER: django.core.wsgi.get_wsgi_application()

It is important to use the new WSGI handler. Since Django 1.7 the old handler django.core.handlers.wsgi.WSGIHandler() doesn't work any more for the IIS. For further information see the article on the pytools website.

Website launch

Now you can start the „Helloworld“ website. IIS uses Port 80 as standard port and complains if you have more than one website running on that port. So you might want to use port 8000 which is also used by the Django development server. Then you can reach the website ubder http:\\127.0.0.1:8000.

Static and media files

For the simple Helloworld sample projekt these settings are sufficient. But running a proction site you need to server static and media files.

Because every URL request is now handled by the Django web application, including requests for files in the /media and /static directories. We need further settings to serve these files with IIS. To achieve this task we need to add virtual directories in the Helloworld site configuration in IIS Server Manager.

Important is to create the correct file permission for these directories ( C:\inetpub\helloworld\helloworld\static, C:\inetpub\helloworld\helloworld\media) in the Windows file system. IIS need read access for /static and read/write access for /media.

When you create the virtual directories it's a standard behaviour of IIS that directories „inherit“ all the handler settings of their parent directory. So you need to open the handler settings of these directories after you created them. In the handler settings you need to delete the Helloworld-Handler for all incoming requests (*). These handler prevents handling the requests for the /static and /media directories.

When you create the virtual directories it's a standard behaviour of IIS that directories „inherit“ all the handler settings of their parent directory. So you need to open the handler settings of these directories after you created them. In the handler settings you need to delete the Helloworld-Handler for all incoming requests (*). These handler prevents handling the requests for the /static and /media directories.

With these settings you can run a production site. You have some scaling option in the FastCGI settings dialog of the IIS Server Manger. You have some parameters to manage the server resources:

  • Max instances
  • Instance MaxRequests

"Monitor changes" to file is also interesting. When you update the files of an web application these changes get loaded automatically.

Comments, additions or critics are always welcome. Please use our contact form.

Author: Max Pfeiffer - Alp Phone GmbH