How to Run a Flask Project in IIS: A Step-by-Step Guide

In my work, I encountered the challenge of deploying a Flask project on Windows, but I was unsure how to proceed. After searching online, I found that many articles were unhelpful. However, I eventually discovered the solution, and I would like to share how I successfully accomplished it.

Running a Flask application on Internet Information Services (IIS) is a great way to host Python web applications on a Windows server. IIS is a flexible, secure, and manageable web server for hosting anything on the web. In this article, we will walk through the process of setting up and running a Flask project in IIS with practical examples.

Prerequisites

Step 1: Install IIS and Configure CGI

  1. Install IIS: If IIS is not already installed, you can install it by opening the Control PanelPrograms and FeaturesTurn Windows features on or off. Check the box for Internet Information Services and select OK.

  2. Enable CGI: Under the same IIS features, ensure that CGI is installed. This allows IIS to execute Python scripts.

    • Go to Control PanelPrograms and FeaturesTurn Windows features on or off.
    • Expand Internet Information ServicesWeb Management Console
    • Expand Internet Information ServicesWorld Wide Web ServicesApplication Development Features.
    • Check CGI and select OK.

            

Step 2: Install Python and Set Up Your Flask Application

  1. Install Python: Download and install Python from the official website (https://www.python.org/downloads/). Make sure to add Python to the system PATH during installation.

  2. Install Flask: Open a command prompt and run the following command: 

        pip install flask

      3. Create a Simple Flask App: Create a new directory for your Flask app and navigate to it. Create a Python file (app.py) with the following                      content:

        

    4. Run the app.py file and navigate to http://127.0.0.1:5000/ in your browser.

      

 

Step 3: Set Up FastCGI for Python

  1. Install wfastcgi: wfastcgi is a Python package that enables FastCGI for IIS. Install it using pip:

    pip install wfastcgi
    
  2. Configure wfastcgi: After installing, run the following command to configure wfastcgi:

wfastcgi-enable

            

Step 4: Configure IIS for Your Flask Application

  1. Create a New Website in IIS:

    • Open IIS Manager.
    • In the Connections pane, right-click SitesAdd Website.
    • Enter a site name (e.g., "FlaskApp").
    • Set the Physical path to the directory where your Flask app resides.
    • Choose a Port (e.g.,5010 or another available port).
    • Click OK to create the website.
  2. Add a Handler Mapping:

    • In IIS Manager, select your new site.
    • Double-click Handler Mappings.
    • On the right, click Add Module Mapping....
    • Enter the following settings:
      • Request path: *
      • Module: FastCgiModule
      • Executable: "C:\Program Files\Python312\python.exe"|"C:\Program Files\Python312\Lib\site-packages\wfastcgi.py" (your executable from step 3)
      • Name: FastApiHandler
      • Click on Request Restrictions..
        • Uncheck "Invoke handler only if request is mappeed to"
    • Click OK.
    • Click Yes
  3. Configure FastCGI Settings:

    • Click on FastCGI Settings in IIS Manager (available in the Features view).
    • Find the entry for python.exe and select it.
    • Click Edit... on the right-hand side.
    • Add an environment variable with the following values:
      • Key: PYTHONPATH
      • Value: C:\Users\Alex\Documents\web_demo (yours path dir)
      • Key: WSGI_HANDLER
      • Value: app.app
    • Click OK to save.

Step 5: Test Your Flask Application

  1. Start Your IIS Website: Ensure that your newly created site is running in IIS Manager.

  2. Access Your Application: Open a web browser and navigate to http://127.0.0.1:5000/ or the IP address and port you configured. You should see your Flask app running with the

    {"email":"alex@mail.com","name":"Alex"}

    message.

Conclusion

You've successfully set up and run a Flask application in IIS using FastCGI. This setup is suitable for deploying Python web applications in a Windows environment where IIS is the preferred web server. With the power of IIS and the flexibility of Flask, you can now serve Python-based web applications in a production-ready environment.

Troubleshooting Tips

  • Permission Issues: Ensure that the IIS user has permission to access the Python executable and your Flask application files.
    • Navigate to the directory of your Flask app, right-click, and select "Properties." Then, go to the "Security" tab and click "Edit." In the dialog that appears, click "Add" and enter IIS_IUSRS.

Comments (0)

Leave a comment