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

If they had activated the Python 3 virtual environment and its bin directory was in there path, then likely they could simply run 'pip' rather than 'pip3'. You should run 'which pip' to verify whether it was coming from the virtual environment you expect.

The 'mod_wsgi-httpd' package is specifically for installing a distinct instance of Apache itself to get around issues where the system wide Apache is out of date or missing development header files, or otherwise can't modify the system wide configuration. It is generally of more relevance when you don't have root access to fix system wide issues and are only interested in running on an unprivileged port and so do not need root access. You would also have to be using 'mod_wsgi-express' from the 'mod_wsgi' package which was 'pip' installed after 'mod_wsgi-httpd' had been installed as that is the only way to use 'mod_wsgi-httpd' installed version of Apache.

So, for 'mod_wsgi-httpd' you would really want to ignore it. If you have already installed it, then 'pip' uninstall both it and 'mod_wsgi'. The latter needs to be uninstalled as it will be bound to the 'mod_wsgi-httpd' Apache version and the 'mod_wsgi.so' will not work with the system wide.

As to separate users steps in link, they look correct, although I would say that in step 3, it should be highlighted that the LoadModule and WSGIPythonHome lines to be added are what is output when running mod_wsgi-express install-module command. What are output is customised to match what your installation should be. So don't make up values, just use what that command output.

If you have a wsgi.load file still around, run sudo a2dismod wsgi to get rid of it or otherwise manually remove it. It is a left over from old system packaged mod_wsgi.