Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing mod_wsgi for Python3 on Ubuntu

Tags:

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?

like image 365
P i Avatar asked Jun 05 '15 19:06

P i


People also ask

Where is mod_wsgi so?

If installing the Apache module by hand, the file is called 'mod_wsgi.so'. The compiled Apache module can be found in the “. libs” subdirectory. The name of the file should be kept the same when copied into its appropriate location.

What is mod_ wsgi in Apache?

mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache. As of version 4.5. 3, mod_wsgi supports Python 2 and 3 (starting from 2.6 and 3.2).


2 Answers

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
like image 56
P i Avatar answered Oct 09 '22 05:10

P i


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.

like image 43
Graham Dumpleton Avatar answered Oct 09 '22 04:10

Graham Dumpleton