Deploying Flask app on IIS

The motivation for this article came after going through a lengthy pursuit on various forums , git hub how-to-dos and learning some new things and finally hosting my project’s REST API written in Python with Mongo DB as back end in IIS successfully.

Hope this article covers those tiny bit of details that I found and is helpful to any one in pursuit.

Critical learnings I had doing this exercise:

  1. It is easy to host a flask script over IIS on your personal laptop. But with a webserver hosted within the confines of a strong security policy it isn't.
  2. pip install doesn't works all the time and in some cases when your target server is not connected to internet you need to figure out offline installation.
  3. User rights to set this up is of paramount importance . Mere confirmation from the server administrator that your ID has local machine privileges or is a part of the administrator group isn't sufficient .

My infrastructure for the setup:

Step 1: Installing wfastcgi package in your python environment.

I will demonstrate it the hard way than pip install as our constraint is that the server is not having internet connection and therefore we cannot use the web platform installer

Download the wfastcgi package from

The contents of the folder should look like this.

Install the package. I moved the contents to a folder under D drive for simplicity. But you can also install it from any path with the following command.

If you are doing this in a virtual environment then please activate the virtual environment before firing this command to ensure that the package installs in your virtual environment .

Critical things to check:

  1. After installation you should get message in the command prompt as follows

2. Don’t trust this. Check the following manually .

wfastcgi-enable and wfastcgi-disable exe present in the

python environment / scripts folder available in

python environment/lib/site-packages folder

With this we have successfully installed wfastcgi.

Step 2: Enabling wfastcgi and configuring Fast CGI Settings in IIS

The first step in enabling wfastcgi is to fire the wfastcgi-enable.exe placed in your python environment / scripts folder

Fire the following command and wait for response

Enabling wfastcgi with wfastcgi-enable command
This is what happens when your user ID has not got sufficient privileges .

Don’t panic . Close the command prompt and open it back in administrator mode as follows and fire the command wfastcgi-enable again.

Running command prompt on administrator mode

You should get the following message .

With this the python side of the table is over . Once again don’t trust this. Open IIS to see if the Fast CGI Settings are indeed fine.

Just in case if you are unable to find FastCGI , please add the feature . The following link has simple steps. In case if you are able to see the FastCGI settings proceed further.

Enabling FastCGI settings for IIS

Click on Fast CGI Settings. Note that the FastCGI settings are done at a server level . So look at your IIS server’s root.

This should show the following entry.

Take a look at the arguments . You might get something like your python environment site packages/wfastcgi-3.0.0-py3.6.egg-info/

When I wrote this blog I had trouble with my user ID and hence I had to manually get the into the site-packages folder .

Just perfect !.

With this we have successfully configured IIS to host python scripts. Let’s host a flask app on IIS now.

Step 3: Setting up the flask app on IIS.

a. First add a new application in IIS with the name “FlaskRedirect” . You can name it anything . But just for the purpose of identification I’m naming it FlaskRedirect .

Now we need to get the contents in virtual directory for this application.

Note the class PrefixMiddleware. It helps me do a basic validation of the URL calls and ensure that only a pre-defined pattern of URL calls are responded to.


Take a look at the parameters . Ensure that your web config file is updated to have the relevant entries in sync with your path .

scriptProcessor = Is the same as the entries that I had defined in the Fast CGI Settings

WSGI_HANDLER = ( where myapp comes from my flask script file name )

PYTHONPATH = Is the virtual directory where I’m keeping my flask script and config file.

d. Setting up the handler mappings for the application.

Now its time to fire up the application and test. Before that we have one last thing to check.

And that’s the handler mapping . Although the web.config file has the handler mapping it is a good idea to ensure that it is there in your site settings.

Look for a mapping “Python FastCGI” .

Remember the name variable that we added in the web.config ?

Just in case if the handler mappings are not visible , you should fire the following command to enable them .

%windir% with the exact location of your Windows Installation.

%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/handlers

Lets check the settings now by double clicking on the mapping. It should look something like this.

As a last step click on the Request Restrictions… button and ensure the check box is “un-checked”

Show time :

Firing up the URL on Chrome .

Observe the URL . Do you remember specifying the port 9010 in the flask script . That’s not in the URL shown here .

Why ?. Because the port forwarding happens automatically with FastCGI settings.

Firing another method

With that we have successfully hosted a flask app on IIS with Fast CGI .


I’m thankful to the following authors and their posts .

Medium post that provides a walk through of the setup with great screenshots

Stack Overflow answer on prefixing the url call. Though this is optional it reduced one worry for me.

Link to the code used in this article

Special Thanks

Pankaj Jha

My colleague and fellow nerd in adding the replace trick that sorted out the URL-redirect problem that I was facing .


Cloud Architect , Pythonista , Polyglot