Understanding WSGI and mod_wsgi: A Simple Guide

Mostafa-DEMostafa-DE
3 min read

Introduction

Deploying Python applications hasn't always been as straightforward as it is today. In the early days, web developers faced significant challenges integrating their Python applications with web servers. There was no standard way for web servers to communicate with Python applications. In this article we are going to discuss some topics such as WSGi/mod_wsgi and see some examples of why deploying Python app is considered a challenge.

Before WSGI, deploying Python web apps was extremely hard and challenging especially because of the limitation of resources, even these days you don't find that many articles related to deployment or DevOps to find anything you need to dig deeper and read a lot of old articles, books, and watch old conferences just to get your head around.

Not to mention, Python itself can be a challenging language to work with. The Global Interpreter Lock (GIL) introduces complexities, and it requires a carefully configured environment to run properly. Add to that the package management and the potential for dependency conflicts. Because of these factors, deploying a Python app can be tricky. If you’re not sure what you’re doing, it could lead to a disaster once your app hits production and starts serving users around the globe.

What is WSGI?

WSGI stands for (Web Server Gateway Interface). It is essentially a specification that outlines how to implement web server gateways. Remember, it is a specification, not an implementation. When someone mentions that an application is deployed on WSGI, they usually mean it is deployed using a web server implementation like mod_wsgi, Gunicorn, etc.

It's crucial to understand the distinction between implementation and specification. A specification provides the guidelines and rules for implementing something specific, while an implementation is the actual execution of those guidelines.

What is mod_wsgi?

mod_wsgi is an Apache module that implements the WSGI specification, enabling seamless deployment of Python applications on Apache web servers.

  • Integration with Apache: Apache was (and still is) a widely used web server. mod_wsgi’s integration with Apache made it a natural choice for deploying Python applications.

  • Performance: Compared to others, mod_wsgi offered significant performance improvements by using persistent processes to handle multiple requests.

How mod_wsgi Works

At a high level, mod_wsgi acts as a bridge between Apache and your Python application, you can say it acts as a Proxy in the middle (Although I don't like this term as it has a different meaning) but anyway, let's put it in this way:

  1. Apache receives an HTTP request.

  2. mod_wsgi translates the request into a format that the Python application understands (using the WSGI standard).

  3. The application processes the request and returns a response.

  4. mod_wsgi sends the response back to Apache, which delivers it to the client.

Although this way of interaction is specific to the daemon mode, we will discuess the different types of modes in mod_wsgi later in a different article but for now let's just stick with the daemon mode.

That's all for now. If you're interested in more advanced content about WSGI, Apache, or other DevOps topics, there are many articles in the DevOps series that you might find interesting.

Mostafa-DE Fayyad

Software Engineer

0
Subscribe to my newsletter

Read articles from Mostafa-DE directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mostafa-DE
Mostafa-DE