Flask on IIS
Mar 9, 2015
2 minutes read

While Windows Server is usually not my preferred OS for deploying Python based web[sites|services] sometimes the situation dictates it. For situations where that is the case I’ve usually reached for Apache with mod_python until now. FastCGI is relatively straightfoward to setup on Windows Server 2012R2 and IIS using the Microsoft Web Platform Installer.

(NOTE: I’m using Flask, but any WSGI compatible Python app will work the same way)

Installing IIS

You’ll need to make sure that IIS is installed and configured with the CGI role service (this enables FastCGI as well).

Go to Server manager -> Manage -> Add Roles and Features:

Roles and Features

Choose Role-based or feature-based installation:

Role based

Select your server:

Server selection

Choose “Web Server (IIS)”:

Web Server (IIS)

Click “Add Features”:

Add Features

Accept the defaults until you reach the “Role Services” screen. At this point make sure “CGI” is selected:

Select CGI

Click next, accept defaults and click “Install”. You should see this screen:

IIS Installing

Launch the IIS Manager:

IIS Manager

If this message pops up, click “Yes” - otherwise you’ll need to download and install the Web Platform Installer if you haven’t already installed it:

Web Platform

Run the installer

Web Platform Installer

Search for WFastCGI

WFastCGI

Select the appropriate Python version (3.4 or 2.7.9). We’ll choose 3.4. Click “Add”, then click “Install”.
Read and accept the license (if you want to continue):

Add Install

Once you’re done - you should see this screen:

Install Complete

Setup your site - copy the wfastcgi.py from C:\Python34 (may be named C:\Python34_x86 if you had an existing Python34 directory) to your Flask application root:

wfastcgi.py in app root

Double click “Handler Mappings”

handler_mappings

Click “Add Module Mapping”

module mapping

Click “Request Restrictions”. Make sure “Invoke handler only if request is mapped to:” checkbox is unchecked:

request_restrictions

Click “Yes” here:

click_yes

Go to the root server settings and click “FastCGI Settings”:

fastcgi_settings

Double click it, then click the “…” for the Environment Variables collection to launch the EnvironmentVariables Collection Editor:

fastcgi_env_vars

Set the PYTHONPATH variable:

python_path

And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app - if yours is named site.py it would be site.app or similar):

wsgi_handler

Click OK and browse to your site:

flask_site

Flask code below:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello from FastCGI via IIS!"

if __name__ == "__main__":
    app.run()

Back to posts