Deploying Flask API on Microsoft IIS in Windows

Minhazul Hasan Sohan
5 min readAug 21, 2024

In a typical deployment, IIS serves as the web server, and WSGI handles the communication between IIS and your Flask application. This is usually achieved through a WSGI-compatible server like wfastcgi or mod_wsgi. These servers translate incoming HTTP requests from IIS into a format that Flask can process, and then return the response back to IIS, which delivers it to the client.

Deploying a Flask API on a Windows machine can be a bit tricky, but with the right steps, you can have your application up and running on the Microsoft Internet Information Services (IIS) server. This guide will walk you through the process of deploying a Flask application on an IIS server, ensuring a smooth and efficient deployment.

Prerequisites:

  • Windows machine with IIS server installed and configured.
  • A Flask web application (make sure it’s running correctly on the local development environment).

Step 1: Ensure IIS is Enabled on Your Windows Machine

First, you need to verify that IIS is active on your Windows machine. To do this, type IIS into the Windows search box.

If IIS is not found, you’ll need to enable it:

  1. Open the Control Panel.
  2. Go to Programs > Uninstall a program.
  3. On the left side, click Turn Windows features On or Off.
  4. Locate and check the Internet Information Services (IIS) option, then click OK to enable it.
Enable IIS from Turn Windows Feature On or Off

Step 2: Create a Flask API Project

  1. Create a new Flask API project in your preferred development environment.
  2. Set up a virtual environment using the command: python -m venv env.
  3. Activate the virtual environment using the command: .\env\Scripts\activate.
  4. Install the required dependencies using pip install flask.
  5. Create a simple Flask application in a file named main.py (or any other name you prefer).
  6. Run the Flask application using the command: python main.py.
  7. Verify that the application is working correctly by accessing http://127.0.0.1:5000/ in your web browser.
A simple Flask API on VS Code

Step 3: Configure File & Folder Permission

Before deploying the app, ensure that the IIS user has the necessary permissions to access your Flask project:

  1. Right-click your Flask project folder and select Properties.
  2. Go to the Security tab and click Edit.
  3. Click Add, and in the dialog, type IUSR and IIS_IUSRS.
  4. If the users are not found:
    4.1. Click Advanced > Find Now.
    4.2. Select IUSR and IIS_IUSRS from the list, then click OK.
  5. Assign the necessary permissions (usually Read & execute, List folder contents, and Read).
  6. Click Apply and OK.
Configure Permission for IUSER and IIS_USER

Step 4: Configure IIS for Your Flask App

Now that your project is ready and permissions are set, configure IIS to serve your Flask app:

  1. Open IIS by typing IIS in the Windows search box.
  2. In IIS Manager, right-click on Sites and select Add Website.
  • Site name: Enter a name for your site (e.g., Flask API on IIS or any name you prefer).
  • Physical path: Set this to the path of your Flask project (e.g., for me it’s C:\\Users\\BS1017\\Desktop\\MyFlaskApp).
  • Port: Assign a port (e.g., 7070).
  • Click OK.
Create a Site on IIS

Step 4.1: Configure Handler Mappings

  1. Double-click on your site in IIS Manager.
  2. Click Handler Mappings and select Add Module Mapping.
  • Request Path: *
  • Module: FastCgiModule
  • Executable Path: For get the executable path
    1. Open Command Prompt as Administrator.
    2. Navigate to your virtual environment’s Scripts directory.
    3. Run: `python.exe -m pip install wfastcgi`
    4. Then, enable FastCGI: `wfastcgi-enable.exe`
    5. Copy the executable path shown in the console and paste it in the Executable Path field.
  • Name: FlaskAPIHandler
  • Request Restrictions: In the Mapping tab, uncheck Invoke handler.
Handler Mapping
Get the Executable Path
Configure Handler Mapping

Step 4.1: Configure FastCGI Settings

  1. Go back to IIS Manager and click FastCGI Settings.
  2. Select your site and double-click on it.
  3. Under the General section, click on the Environment Variables button (three dots).
  • Add:
    1. Name:
    PYTHONPATH
    2. Value: The path of your application (For me its: C:\User\BS1017\Desktop\MyFlaskApp )
  • Add:
    1. Name:
    WSGI_HANDLER
    2. Value: main.app (replace main.app with the entry point of your Flask app).
FastCGI Settings
Find your created site
Add PYTHON path and WSGI_HANDLER

Final Step: Test Your Deployment

That’s it! Your Flask application should now be deployed and accessible through the IIS server. You can test it by navigating to http://localhost:7070/ (or the port number you specified) in your web browser.

Test Your API on Deployed Port

Remember to replace the paths and file names with the ones specific to your Flask application and development environment.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Minhazul Hasan Sohan
Minhazul Hasan Sohan

Written by Minhazul Hasan Sohan

চিত্ত যেথা ভয়শুন্য, উচ্চ যেথা শির

No responses yet

Write a response