Deploying Python web app (Flask) in Windows Server (IIS) using FastCGI

Python can also falling in love with windows :p

Though windows is not a perfect environment to install python, sometimes situation force you to do it. In this setup, I try to install flask up and running in Microsoft IIS (Windows Server 2012 R2). You can get flask running in pair with nginx or apache in linux either using gunicorn, uWSGI , or other wsgi server, but in my case, I was forced to install it on windows, thus I tried to install it on top of IIS , and it was pretty ~straight forward~ Tricky.

You can install and setup the IIS in your windows machine either from add or remove features or by using Microsoft Web Platform Installer (formerly web matrix) or you can say it is a Softaculous of Microsoft :p .

This time, I will give an example in installing flask in IIS using VirtualBox from scratch, so it will covers from installing python to make it run.

(Note : I use flask but any WSGI based python web framework should work using this tutorial)


Installing IIS (Along with installing FastCGI and Python)

After you finnish installing your virtualbox with Windows server installed, you can go directly to

Server Manager -> Manage -> Add Role and Features:

Then, choose Role-based or feature-based installation:

Select your server:

Choose “Web Server (IIS)”:

Click “Add Features”:

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

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

Launch the 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:

Run the installer

Search for WFastCGI, this will also install Python

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 (we all know, no one actually read that):

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

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:

Double click “Handler Mappings”

Click “Add Module Mapping”

its pipe character : C:\Python34\Python.exe|C:\inetpub\wwwroot\mysite\wfastcgi.py

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

Click “Yes” here:

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

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

Set the PYTHONPATH variable:

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):

Click OK and browse to your 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()

So, after sucessfuly runing your test site which only output Hello bla bla, then it is time to install your actual flask app ! .

Setting up windows environment for python

Firstly, make sure you have setting up your python binary and script in your system environment variable

Install Visual C++ compiler

Install corresponding visual c++ compiler and make sure it match with your python version

Install required software development kit

For now on, firstly you can try running your run.py using python. Just type

python run.py

and see the result, make sure by using the default werkzeug development wsgi server it works perfectly ! . If it runs error-free, it is a good sign that your code can totally fine running on the IIS.

Don’t forget to also update your pip

python -m pip install — upgrade pip

If else it shows error, like some file with .bat extension couldn’t found, then update the setuptools first, by running

pip install -U setuptools

If in case there is error and in the terminal shown that you have to install the required software development kit, just copy paste the url and install it.

Done doing that, go back to your terminal and re run, you should be good to go.

Setting up your server and Web Site

Actually, you have done this step, but however maybe you have one other process missing, which is installing the rewrite feature in the IIS , just go back to Windows Platform Installer and install rewrite feature.

And also don’t forget to create another Web Site, don’t use the default one

Setting up your pip

Make sure you have set up all of your requirements.txt (if you have one)

pip install -r requirements.txt

and make sure while in this process, your iis server has been stopped.

Clone or prepare your existing flask app

Install your flask app pip requirements

Setting up the rewrite URL

<rewrite>
 <rules>
 <rule name=”asset-url-rewrite” stopProcessing=”true”>
 <match url=”static” />
 <conditions>
 </conditions>
 <action type=”Rewrite” url=”flaskapp/{R:0}” />
 </rule>
 <rule name=”app-url-rewrite” stopProcessing=”true”>
 <match url=”[a-zA-Z]+” />
 <conditions>
 </conditions>
 <action type=”Rewrite” url=”flaskapp/” />
 </rule>
 </rules>
 </rewrite>

Note : the flaskapp is my main root directory of my flask apps, so you should rename it corresponds to your root directory of your flask apps.

Update

In case you have error 500 scriptProcessor not found , or 404 not found, make sure you follow every single detail of my tutorial above. Moreover try check your EventHandler in your pipe character :

C:\Python34\python.exe|C:\inetpub\wwwroot\mysite\wfastcgi.py

make sure between that pipe you remove the evil space , thanks to this post in stackoverflow, I literally spent 1 week to just realize that my pipe character has a space in it :( .

Also try to do in this post , which is set a execution permission in the python folder, because it may be the couse of the problem.

Done

ps : screenshots and steps for installing wcgi are made by and tribute to Aaron | אהרן