pythonapachemod-wsgipyenv

Run mod_wsgi with pyenv


I'm trying to run a python web app on Apache server. I have installed mod_wsgi: sudo apt-get install libapache2-mod-wsgi.

This is my site config file:

<virtualhost *:80>
    ServerName 192.168.60.144

    WSGIDaemonProcess myapp.dev processes=1 threads=1 python-home="/home/user/.pyenv/versions/3.6.1" python-path="/home/user/API" home='/home/user/API'
    WSGIProcessGroup myapp.dev
    WSGIScriptAlias /  /home/user/API/config_files/myservice.wsgi.py

    <Directory /home/user/API>
        Order allow,deny
        Allow from all
    </Directory>

    DocumentRoot /home/user/API/myservice/testdir
    <Directory />
         AllowOverride None
          Require all granted
     </Directory>

</virtualhost>

However, when trying to access the web app, I get "Internal server error". This is the error from logs:

[Mon Mar 12 13:34:04.054636 2018] [wsgi:error] [pid 6762:tid 139768980616960] [remote 10.10.30.13:34438] mod_wsgi (pid=6762): Target WSGI script '/home/user/API/config_files/myservice.wsgi.py' cannot be loaded as Python module. [Mon Mar 12 13:34:04.054665 2018] [wsgi:error] [pid 6762:tid 139768980616960] [remote 10.10.30.13:34438] mod_wsgi (pid=6762): Exception occurred processing WSGI script '/home/user/API/config_files/myservice.wsgi.py'. [Mon Mar 12 13:34:04.054693 2018] [wsgi:error] [pid 6762:tid 139768980616960] [remote 10.10.30.13:34438] Traceback (most recent call last): [Mon Mar 12 13:34:04.054717 2018] [wsgi:error] [pid 6762:tid 139768980616960] [remote 10.10.30.13:34438] File "/home/user/API/config_files/myservice.wsgi.py", line 6, in [Mon Mar 12 13:34:04.054752 2018] [wsgi:error] [pid 6762:tid 139768980616960] [remote 10.10.30.13:34438] from pathlib import Path [Mon Mar 12 13:34:04.054775 2018] [wsgi:error] [pid 6762:tid 139768980616960] [remote 10.10.30.13:34438] ImportError: No module named pathlib

I have checked the pyenv python installation, and it has the pathlib module installed. This means that probably, not the right version of Python is being used. How can I check which Python version is being used? Did I set the python-home property incorrectly?


Solution

  • You can't use system packaged version of mod_wsgi with a pyenv environment. It is necessary for mod_wsgi to be compiled for the specific Python installation/version being used. When using pyenv it is a separate Python installation, not the system Python that is being used. You should use the pip install method for installing mod_wsgi compiled for Python installation created by pyenv. Do make sure that when you had pyenv install Python that shared libraries were enabled.

    IOW, uninstall system packaged version of mod_wsgi and then use pip install method to install mod_wsgi and configure Apache to use it. See:

    In particular, see the Connecting into Apache installation section for how to load the pip installed mod_wsgi into Apache. I.e., modifying the path in /etc/apache2/mods-available/wsgi.load to point to the mod_wsgi-<version>.so file in the virtual environment.