Installing mod_wsgi for Python3 on Ubuntu

P i picture P i · Jun 5, 2015 · Viewed 26.8k times · Source

Could anyone give me a clear set of instructions for installing mod_wsgi on Ubuntu for Python 3?

I did get Flask & mod_wsgi successfully using Python3, and for a brief moment felt happy.

...until I looked at Apache's log and realised that I've run into this problem: https://askubuntu.com/questions/569550/assertionerror-using-apache2-and-libapache2-mod-wsgi-py3-on-ubuntu-14-04-python

apt-get is installing an out of date version of libapache2-mod-wsgi-py3 and this is causing errors in Apache's log. (Should I report this and if so where?)

In the link, the engineer is using pip to install a more up-to-date version.

Interestingly, he appears to be installing it into a virtual Python3 environment. (Would this be any different than using the system pip3?)

Also he uses pip3 install mod_wsgi, but pip3 search mod_wsgi returns:

(EDIT: no he doesn't, he uses pip not pip3. Can that be right? Has he got his wires crossed? Isn't pip going to ignore the fact that he is in his py3venv and simply use the system's py2 installation? But anyway that doesn't resolve the confusion...)

mod_wsgi-metrics          - Metrics package for Apache/mod_wsgi.
cykooz.recipe.pastewsgi   - Buildout recipe to create paste.deploy entry points for mod_wsgi or uwsgi
mod_wsgi-httpd            - Installer for Apache httpd web server.
apachemiddleware          - Useful Python middleware for use with mod_wsgi deployments
tranchitella.recipe.wsgi  - Buildout recipe to create paste.deploy entry points for mod_wsgi
mod_wsgi                  - Installer for Apache/mod_wsgi.

So what is mod_wsgi-httpd? And is it certain this isn't the one I want?

Finally, can anyone provide a link to installing mod_wsgi from source?

EDIT: I don't get why the engineer is using pip install mod_wsgi rather than pip3 .... Surely that can't be right? But if I do use pip3, I get:

pi@PiDroplet:~$ cd web/piFlask

pi@PiDroplet:~/web/piFlask$ source ./venv3/bin/activate
(venv3)
pi@PiDroplet:~/web/piFlask$ pip3 install mod_wsgi
Downloading/unpacking mod-wsgi
  Downloading mod_wsgi-4.4.12.tar.gz (991kB): 991kB downloaded
  Running setup.py (path:/home/pi/web/piFlask/venv3/build/mod-wsgi/setup.py) egg_info for package mod-wsgi
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/home/pi/web/piFlask/venv3/build/mod-wsgi/setup.py", line 141, in <module>
        'missing Apache httpd server packages.' % APXS)
    RuntimeError: The 'apxs' command appears not to be installed or is not executable. Please check the list of prerequisites in the documentation for this package and install any missing Apache httpd server packages.
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/home/pi/web/piFlask/venv3/build/mod-wsgi/setup.py", line 141, in <module>

    'missing Apache httpd server packages.' % APXS)

RuntimeError: The 'apxs' command appears not to be installed or is not executable. Please check the list of prerequisites in the documentation for this package and install any missing Apache httpd server packages.

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /home/pi/web/piFlask/venv3/build/mod-wsgi
Storing debug log for failure in /home/pi/.pip/pip.log

And now I try pip3 install mod_wsgi-httpd, it takes about five minutes to compile:

(venv3)
pi@PiDroplet:~/web/piFlask$ pip3 install mod_wsgi-httpd
Downloading/unpacking mod-wsgi-httpd
  Downloading mod_wsgi-httpd-2.4.12.5.tar.gz
  Running setup.py (path:/home/pi/web/piFlask/venv3/build/mod-wsgi-httpd/setup.py) egg_info for package mod-wsgi-httpd
    apr-1.5.2/
    apr-1.5.2/config.layout
    apr-1.5.2/build.conf
    apr-1.5.2/emacs-mode
    :

So now I'm worried I've got a second Apache sitting in my Py3 virtualenv.

However that does get rid of the error; pip3 install mod_wsgi now completes successfully.

EDIT: but now I have come completely unstuck trying to follow his instructions: I don't have a /etc/apache2/mods-available/wsgi_express.load, and if he is suggesting making it, then for a start this seems arbitrary, and secondly the text he suggests putting in it, i.e. LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py34.cpython-34m.so -- this file doesn't exist on my system.

I do have /etc/apache2/mods-available/wsgi.load

Drowning in technology again, can someone throw me a line?

Answer

P i picture P i · Jun 6, 2015

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