How to deploy Python / Flask Web Applications?

dev0928 profile image dev0928 ・2 min read

I was trying to figure out steps involved in deploying a typical Flask framework based Python web application in a production environment. This writeup has details of my findings.

Flask framework does have a built-in server. But it is suitable only for development purposes and cannot be used in a production environment as it does not scale well. So to deploy a Flask based web application in production, we need to pick a WSGI compliant server.

What is WSGI?

WSGI stands for Web Server Gateway Interface. It is a standard specification that the web server has to implement to run a Python web application. A typical web server does not have the ability to run Python web applications. Historically, Apache server's module implementation called mod_python used to be the only web server option for running Python web applications. The Python community recognized the need for standardization around this and came up with the WSGI interface. With this WSGI standard, web servers have to implement the server side of the WSGI interface and Flask application object is the actual WSGI application. Flask derives WSGI application functionality from Werkzeug which is a comprehensive WSGI web application library.

Below illustration shows what happens when a user makes a request to a Python web application hosted in a production environment:

Python Web Request Flow

Deployment Options:

There are several WSGI compliant server options to choose from to host Python web applications. Also, both hosted and self-hosted options are available. Below link from Flask documentation website has a list of deployment options along with their deployment steps.



Editor guide
m1yag1 profile image

You might want to include how Flask utilizes Werkzeug for its WSGI application. Flask is the "glue" between Werkzeug, the request, Jinja2, and other extensions. As per the palletsproject.org site, "Flask wraps Werkzeug, using it to handle the details of WSGI while providing more structure and patterns for defining powerful applications." ref: palletsprojects.com/p/werkzeug/

dev0928 profile image
dev0928 Author

Great point! Updated post to include mention of Werkzeug library. Thanks for pointing out!

m1yag1 profile image

n/p I really liked your post. I don't think there is enough discussion about this topic for those that are learning how to build web applications with Flask (or any python web framework). I think it's difficult for beginners to grasp what exactly is going on at this level. If you wanted to take it further you could go a little more in-depth and discuss routing and the various HTTP verbs. Either way, this is a good reference.

ozzythegiant profile image
Oziel Perez

For those not sure on what to choose for deployment or if this is your first time, I recommend using either Gunicorn, or if you are a polyglot programmer, look into Phusion Passenger so that you can learn how to set up an app server for multiple programming languages. Once you set either one of those up, use your web server of choice (nginx, apache) and reverse proxy into that app server. In my opinion, I think this helps to decouple your infrastructure, which is nice if you ever need to change between web servers, deploy on multiple servers at once, or if you need to deploy multiple web applications on the same web server, having multiple proxies to different back ends.

Not sure if anyone has an easier method though; enlighten me please if you do.