The mod_wsgi Apache module is used for serving Python scripts over HTTP via the Apache web server. This tutorial helps you to how to install the Apache mod_wsgi module on Ubuntu 18.04 (Bionic Beaver).

Advertisement

Step 1 – Prerequisites

Login to Ubuntu 18.04 server console via SSH and install some prerequisites packages on the system.

sudo apt-get update
sudo apt-get install python libexpat1 

Step 2 – Install mod_wsgi Module

Before starting, you will need to install some prerequisite Apache components in order to work with mod_wsgi. You can install all the required components by simply running the following command:

sudo apt-get update
sudo apt-get install apache2 apache2-utils ssl-cert

Now, install mod_wsgi Apache module by running the following command:

sudo apt-get install libapache2-mod-wsgi

Restart Apache service to get mod_wsgi to work.

sudo systemctl restart apache2

Step 3 – Configure Apache for WSGI

Next, create a python script to serve via mod_wsgi Apache module.

sudo vi /var/www/html/wsgi_test_script.py

Add the following content:

After that, you need to configure the Apache server to serve this file over the HTTP protocol. Let’s create a configuration file to serve the wsgi_test_script.py script over a sub URL.

sudo nano /etc/apache2/conf-available/mod-wsgi.conf

Add the following content:

WSGIScriptAlias /test_wsgi /var/www/html/wsgi_test_script.py

After completing the above steps enable mod-wsgi configuration and restart Apache service.

sudo a2enconf mod-wsgi
sudo systemctl restart apache2

Step 4 – Testing

The setup is ready now. You can test the script by accessing the following URL in a web browser.

 http://SERVER_IP/test_wsgi

See below screenshot:

Share.

11 Comments

  1. I do have a 404 error, file not found … it seems that WSGIScriptAlias isn’t correctly read by the system (the .py file is readable and if called the browser asks me to dowload it

  2. Hugh Parker on

    With Python 3 the example wsgi script gives an error. WSGI expects byte sequences, not strings, e.g.
    b’\n’ \
    b’\n’ \

  3. This isn’t working for me. I’m using bitnami; so, instead of /var/www/… I have a different directory structure. The mod-wsgi.conf file isn’t at the specified location, and a2enconf isn’t recognized. Any suggestions? Bitnami doesn’t seem to have any instructions for Python or mod-wsgi.

  4. Thanks. After a great deal of searching and faffing with various Stack Overflow type suggestions, your tutorial was ultimately the one that got it working for me.

  5. Your tutorial was very helpful, and concise, mod_wsgi is working now. I have been trying to get mod_wsgi to serve flask apps on my mac for a while now and decided to move to bionic beaver instead. The next step serving flask apps.

    Thanks again!

Leave A Reply


Exit mobile version