Installing mod_wsgi for Python3 on Ubuntu

I’m intending this answer as a “note to self that may be of use to others”.

apt-get at time of writing installs an outdated version of mod_wsgi.

pip installs an up-to-date version. It does this by downloading the source code and compiling it.

Set up a Python3 virtualenv and activate it with source ./venv3/bin/activate, verify that which pip confirms it is now using this environment. It appears that pip and pip3 are interchangeable.

In my case my ./venv3 is inside my flask folder. And the only purpose of mod_wsgi is to have Apache route http://myfoo.org/flask requests to my flask app. So it makes sense to install mod_wsgi into this venv3.

However, for pip to successfully compile it, I first need sudo apt-get install apache2-dev which provides necessary header files. Then I required a reboot. Then pip install mod_wsgi completes okay.

Then following the instructions from the original link:

(venv3)
$ sudo venv3/bin/mod_wsgi-express install-module
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py34.cpython-34m.so
WSGIPythonHome /home/pi/web/piFlask/venv3

Then I have to create /etc/apache2/mods-available/wsgi_express.{load,conf} containing these 2 lines respectively.

Finally enable the module and check Apache’s error log:

a2enmod wsgi
sudo service apache2 restart
cat /var/log/apache2/error.log

Leave a Comment