运行 mod_wsgi 与 pyenv

Run mod_wsgi with pyenv

我正在尝试 运行 Apache 服务器上的 python 网络应用程序。我已经安装了 mod_wsgi: sudo apt-get install libapache2-mod-wsgi.

这是我的站点配置文件:

<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>

但是,在尝试访问网络应用程序时,我得到 "Internal server error"。这是日志中的错误:

[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

我检查了pyenv python安装,它安装了pathlib模块。这意味着可能没有使用正确版本的 Python。如何检查正在使用哪个 Python 版本?我是不是设置错了 python-home 属性?

您不能在 pyenv 环境中使用 mod_wsgi 的系统打包版本。 mod_wsgi 必须针对正在使用的特定 Python installation/version 进行编译。使用 pyenv 时,它是一个单独的 Python 安装,而不是正在使用的系统 Python。您应该使用 pip install 方法来安装 mod_wsgi 为由 pyenv 创建的 Python 安装编译。请确保在安装 pyenv 时 Python 启用了共享库。

IOW,卸载mod_wsgi的系统打包版本,然后使用pip install方法安装mod_wsgi并配置Apache使用它。参见: