25

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?

P i
  • 29,020
  • 36
  • 159
  • 267

3 Answers3

43

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
P i
  • 29,020
  • 36
  • 159
  • 267
10

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.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • I've removed `mod_wsgi` and `mod_wsgi-httpd` using pip. Now I'm stuck. If I `pip install mod_wsgi` I get the same error as in my post, where I'm trying `pip3 ...`. the only thing I can think of is to use the system pip to do the installation, in the hope that my find the system Apache...? – P i Jun 05 '15 at 22:32
  • 3
    Sorry, totally overlooked the apxs issue. That is because you haven't installed the appropriate 'dev' package for Apache from Ubuntu package repositories. That this is required is mentioned on the PyPi page for mod_wsgi. Without that package you will be missing the Apache header files and its build tool called 'apxs'. – Graham Dumpleton Jun 05 '15 at 23:42
  • 1
    I found the page https://pypi.python.org/pypi/mod_wsgi -- it is frustrating because it says *"For example, ..., if you were using the Apache prefork MPM you would need... If instead you were using the Apache worker MPM, you would need ..."* But I have never heard of these terms. Are these different flavours of Apache? So somehow I have to figure out what flavour my Apache is and what the corresponding dev file would be...? – P i Jun 06 '15 at 09:19
  • 4
    I've tried `sudo apt-get install apache2-dev` which completes. But now `pip install mod_wsgi` hangs: https://gist.github.com/p-i-/b559dc5e43f0776e13fd EDIT: just needed a reboot, now it completes. Seems to be working. Thanks! – P i Jun 06 '15 at 10:30
  • You can determine which Apache MPM is likely being used by following instructions in http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Apache_Build_Information Some Linux systems screw up the ability to determine this easily in this way as they require special environment variables to be set as well. More recent Linux versions also don't use either prefork or worker MPM but event MPM and that may be the only option and in that case you just need the generic Apache dev package. – Graham Dumpleton Jun 06 '15 at 20:25
  • @GrahamDumpleton Very informative answer thanks. I found the comment about apache2-dev requirement. useful, might be worth adding to the main answer content. – Algebra Nov 21 '17 at 08:10
  • I did "sudo apt-get install apache2-dev", then "pip install mod_wsgi". When I did "sudo a2enmod wsgi", apache 2.4 gave me an error: "Module wsgi does not exist!". To solve this I did "mod_wsgi-express module-config > /etc/apache2/mods-available/wsgi.load" (generate the mod configuration in Apache). After looking at apache error logs, I found I also had to disable mod_python. I was then able to successfully enable wsgi. – nettie Feb 21 '20 at 20:31
2

When I typed apxs in terminal it said the command can be installed with:

sudo apt install apache2-dev

And it worked!

Nikolay Dyankov
  • 6,491
  • 11
  • 58
  • 79