How to Deploy a Python Flask Web App on Windows Server for High Traffic and Best Performance

How to Deploy a Python Flask Web App on Windows Server for High Traffic and Best Performance

Published: 2025-01-26 08:06:32 Updated: 2025-02-11 10:51:30.932604+00:00 By: MOSHIUR SHOHEL
If you're looking to deploy your Python Flask web app on a Windows Server with a focus on handling high traffic and supporting many concurrent users, you're in the right place. In this guide, we'll walk you through the process of setting up the Flask application on Windows, configuring IIS as a reverse proxy, and ensuring your site performs well under heavy load.
 

What You Need:

Γªü Windows Server (with IIS installed). 
Γªü Python installed (preferably Python 3.9 or later). 
Γªü Flask web app ready to deploy. 
Γªü Waitress (a production-grade WSGI server for Python). 
⦁ URL Rewrite module and Application Request Routing (ARR) for IIS.
⦁ Installed NSSM (download from ⦁ nssm.cc).
 

Step-by-Step Deployment

Step 1: Install Python and Necessary Libraries

Before deploying your Flask app, make sure that Python and all necessary libraries are installed on your server.
Γªü Install Python: 
Γªü Download and install the latest Python version from Γªü python.org. 
⦁ During installation, make sure to check ⦁ Add Python to PATH.
Γªü Install Flask: 
Γªü Open Command Prompt and write following command to install Flask: 
Γªü Install Waitress (WSGI server for Python): 
⦁ Waitress is a production-ready WSGI server that will handle requests efficiently.
Γªü Install Waitress by running: 
 

Step 2: Prepare Your Flask App for Production

Ensure your Flask app is ready to run in a production environment with Waitress. Here’s a basic Flask app example (app.py):
⦁ The serve() method will start the Waitress server on 127.0.0.1:5000.
⦁ The threads=10 option helps increase concurrency by handling more requests simultaneously. You can adjust the number of threads based on your server's capacity.
 

Step 3: Install IIS (Internet Information Services) and Required Features

You need IIS to handle web requests, manage SSL certificates, and route traffic efficiently.
Γªü Install IIS: 
Γªü Open Server Manager. 
Γªü Go to Add Roles and Features > Role-based or feature-based installation. 
Γªü Select Web Server (IIS). 
Γªü Click Next through the installation wizard. 
⦁ Enable CGI and URL Rewrite:
Γªü In IIS Manager, enable CGI and URL Rewrite (for creating a reverse proxy). 
⦁ To do this, navigate to IIS Manager > Your Server > Role Services, and enable both CGI and URL Rewrite modules.
Γªü Install Application Request Routing (ARR): 
Γªü Download and install Γªü ARR from the official Microsoft site. 
⦁ ARR helps IIS handle high traffic and serve as a reverse proxy, forwarding requests to your Python Flask app running on Waitress.
 

Step 4: Configure IIS as a Reverse Proxy

Now that IIS is installed, we need to configure it to forward incoming web requests to your Flask app running on Waitress.
Γªü Enable Proxy Settings in IIS 
Γªü Open IIS Manager. 
Γªü On the left, select your Server node (not your site, but the root of the server). 
Γªü In the middle panel, double-click Application Request Routing Cache. 
Γªü On the right, click Server Proxy Settings. 
Γªü Check Enable Proxy and click Apply. 
Γªü Configure URL Rewrite for Reverse Proxy 
 
Now, you need to configure the URL Rewrite module to forward incoming requests to your Flask app. 
Γªü In IIS Manager, select your site (e.g., Default Web Site). 
Γªü Double-click URL Rewrite under the IIS section. 
Γªü On the right, click Add Rule(s).... 
Γªü Select Blank Rule under the Inbound Rules section and click OK. 
Configure the Rule 
Γªü Name: Reverse Proxy Rule. 
Γªü Requested URL: Choose Matches the Pattern. 
Γªü Using: Choose Regular Expressions. 
Γªü Pattern: Enter ^(.*)$ to match all incoming requests. 
Action Settings 
Γªü Action Type: Select Rewrite. 
Γªü Rewrite URL: Enter http://127.0.0.1:5000/{R:1}. This forwards all incoming requests to your Waitress server running at 127.0.0.1:5000. 
⦁ Check Stop processing of subsequent rules.
 Click Apply to save the rule.
 

Step 5: Test Your Deployment

⦁ Start your Flask app with Waitress
Γªü Open a web browser and visit your site (e.g., http://yourdomain.com or https://yourdomain.com if using SSL). 
You should see the Flask app responding through IIS.
 

Step 6: Optimize for High Traffic

For high traffic, it’s crucial to optimize your server and Flask app.
Optimize Waitress for Performance:
⦁ Increase the number of threads in the serve() method for more concurrency:
⦁ Connection Limit: Increase the connection limit if needed for more users:
 
Optimize IIS for High Traffic:
Γªü Request Queue: Increase the request queue size in IIS: 
Γªü Go to IIS Manager > Your Site > Advanced Settings. 
Γªü Increase Queue Length (e.g., 5000). 
Γªü Application Pool: 
Γªü Set the Idle Timeout of your application pool to 0 to prevent automatic recycling. 
⦁ Enable Compression for better performance with static assets like images, CSS, and JavaScript.
 

Step 7: Set Up Flask App as a Service Using NSSM

To ensure your app runs continuously as a background service:
Γªü Download NSSM: 
Γªü Go to https://nssm.cc/download. 
Γªü Under the nssm (version e.g 2.24) section, click the download link for the ZIP file. 
Γªü Extract the ZIP File: 
Γªü Once downloaded, extract the ZIP file to a directory on your computer (e.g., C:\nssm-2.24). 
Γªü Add NSSM to PATH (Optional): 
Γªü For easier access, copy the nssm.exe file (located in the win32 or win64 folder inside the extracted directory) to a location in your system PATH (e.g., C:\Windows\System32). 
Γªü Verify NSSM Installation: 
Γªü Open a Command Prompt and type: 
nssm version
 If installed correctly, you’ll see the NSSM version information.
Create a Windows Service for Your Flask App
⦁ Open a Command Prompt with administrative privileges
⦁ Run the following command to create a new service:
nssm install MyFlaskApp (relevant name of your service)
⦁ A GUI window will appear. Fill out the fields as follows:
First provide the appropriate service name in the service name section
⦁ Path This is where you specify the path to the Python executable that will run your app. Click the Browse button next to the Path field. Locate the python.exe file used for your project. If you're using a virtual environment (e.g., venv), the path might look like this:
C:\<your project directory>\venv\Scripts\python.exe
⦁ Arguments
This is where you provide the command to run your main.py/ app.py file with Waitress. If main entry file name is main.py. then write following command
-m waitress --host=0.0.0.0 --port=5000 --threads=20 main:app
⦁ Startup Directory
It should be the folder containing your project files. Like:
C:\ <your project directory>\
Click Install Service and you are done. Now start the service by following command
nssm start <service_name_provided>
Conclusion
By following these steps, you’ve successfully deployed your Flask web app on a Windows Server with IIS as a reverse proxy. This setup is designed to handle high traffic and lots of concurrent users efficiently. With Waitress as the WSGI server and IIS as the reverse proxy, you ensure good performance and reliability for your web app.
By tweaking configurations and optimizing both Waitress and IIS, you can scale this setup to handle even more traffic and concurrent users.
This blog should serve as a clear, beginner-friendly guide for deploying Python Flask apps on Windows servers with high traffic and performance in mind.
Note: If you're using Waitress, restart it by stopping and restarting the service:
# Stop the Waitress server
taskkill /F /IM python.exe
# Start the Waitress server again
python -m waitress --host=0.0.0.0 --port=8080 myapp:app
MOSHIUR
MOSHIUR SHOHEL
Author