Deploy a Python Flask Application in IIS Server and run on machine IP address

Pralay Das
6 min readMay 22, 2020

--

In this article, I’ll explain the procedure of deploying a Flask application in Windows IIS Server (2012 R2, 2016 and 2019) using FastCGI module and exposing the APIs on machine IP address.

I. Prerequisites

A. Installing Python

There are a set of prerequisites which are required to create and run a Flask Application. As Flask is a micro web framework designed in Python, we need to install python first into the system. The executable file can be downloaded from this URL: Python
Download the executable file and install Python in “C:\Python” folder and add “C:\Python\” and “C:\Python\Scripts” to the system environment variables. We can verify the installation by using the command prompt (CMD).

B. Installing Libraries

To run a flask application in IIS server, we need the “flask” and “wfastcgi” libraries. We can install the Python libraries by executing the “pip” command in CMD.

pip install flask
pip install wfastcgi

C. Installing IIS feature with CGI role

The Flask application connects with the FastCGI module of the server to expose the micro-service APIs. So, we have to install the IIS feature in the Windows server and enable the CGI role. The following steps will help to do it:

Open Server Manager and select Manage > Add Roles and Features.

Click “Next” and select “Role-based or feature-based installation” and select “Next”

Select the server (default: server available in server pool) and select “Next”

Select “Web Server (IIS)”, select “Add Features” and select “Next”

Accept the default configurations and continue till we see the “Role Services ” screen. Select “CGI” under “Application Development” and select “Next”

Accept the default configurations and select “Install”.

After the installation is complete, close the wizard.

Launch the IIS Manager by executing the following command in Windows Run (Win + r)

This will launch the IIS Manager in the system. If prompted with the following message then select “No”. Microsoft Web Platform helps to install the Python and FastCGI modules which we have already installed earlier.

D. Enable the FastCGI module

Go to this path “C:\Python\Scripts\” and execute the “wfastcgi-enable.exe” file. This will enable the FastCGI module and attach it with the IIS server.

This should create an entry for FastCGI pointing to the wfastcgi module in IIS Server. Verify the entry under FastCGI Settings in IIS Manager root server.

II. Create a Flask Application

Create a directory under “C:\inetpub\wwwroot\” naming “FlaskApp”. Inside FlaskApp folder, create the Flask Application file “app.py”.

"""
Deploy Flask App in IIS Server
"""
from flask import Flaskapp = Flask(__name__)
@app.route("/")
def home():
return "Hello, This is the Flask App in IIS Server."
if __name__ == "__main__":
app.run()

Now, copy the “wfastcgi.py” file from “C:\Python\Lib\site-packages\” directory and paste it to “C:\inetpub\wwwroot\FlaskApp\” directory.

Project Structure:

Now, we have to provide a few permissions to IIS so that it can access the FlaskApp directory. Open a CMD and change the directory to FlaskApp and execute the following commands:

icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"

Now, as the permissions are added, we are ready to deploy our Flask application in the IIS server.

III. Create a Website in IIS Manager

To host the Flask Application, we need to create a website using IIS Manager that can serve our application. Open IIS Manager > Sites > Default Web Site (Default Web Site must display the FlaskApp folder).

A. Change the Default Web Site folder mapping

Right click on Default Web Site > Manage Website > Advanced Settings… > Update the Physical Path value by “C:\inetpub\wwwroot\FlaskApp” and select “OK”. This should update the Physical Path of Default Web Site which should now point inside the “FlaskApp” directory. As there are only files but no sub-folders inside “FlaskApp” directory, the Default Web Site must display nothing now.

B. Add Handler Mappings

Select “Handler Mappings” in Default Web Site Home screen > Add Module Mapping… > Insert the values as below:

Request Path: *
Module: FastCgiModule (Select from the dropdown menu)
Executable(optional):
C:\Python\python.exe|C:\inetpub\wwwroot\FlaskApp\wfastcgi.py
Name: FlaskHandler

Click on “Request Restrictions” and uncheck “Invoke handler only if request is mapped to:” checkbox and select OK followed by another OK. Select “Yes” if prompted by this warning.

Now, verify the module mapping in FastCGI Settings in the root server. This should create an entry in FastCGI Settings.

B. Update the FastCGI Application

Double click the application entry in FastCGI settings to edit the application and select “Environment Variables”.

Click on “…” and open the “EnvironmentVariables Collection Editor”.

Add the below entries:

Name: PYTHONPATH
Value: C:\inetpub\wwwroot\FlaskApp

The PYTHONPATH must point to the directory containing the Flask Application.

Name: WSGI_HANDLER
Value: app.app

The WSGI_HANDLER must point to the application name followed by “.app”. In our case, the application name is “app.py” so WSGI_HANDLER is pointing to “app.app”.

IV. Access the Website

With the above configurations done, the website must be up and running. Open a browser and access “http://localhost/” (no need to mention any port as we have deployed the application in Default Web Site using port 80). We should get the following message:

V. Host the Application on machine IP address

Open IIS Manager > Sites > Default Web Site > Bindings… > Double click on “http” entry > Select the Machine IP Address under IP Address drop down > Click “OK” > Click “Close”.

Now Restart the website once. The “Browse Website” must be updated with <Machine IP Address>:80 now.

Open a browser and access “http://<Machine IP Address>/” (still no port is required).

Now, the APIs exposed by the Flask application can be accessed by other computers also in the network. We can deploy multiple Flask Applications in Web Sites assigning different ports.

So, that is all about deploying a Flask Application in IIS Server and run on a machine IP address.
Cheers and Keep your Dreams Alive !

--

--