Django/mod_wsgi/Apache - mod_wsgi 未使用为其编译的 Python 版本 - “ModuleNotFoundError:没有名为 'math' 的模块”

2024-01-09

我正在尝试在 Ubuntu 16.04.6 服务器上部署带有 Apache2 和 mod_wsgi 的 Django 应用程序,但我正在努力让 mod_wsgi 使用正确的 python 版本。

我从源代码安装了 mod_wsgi,并且配置它 https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html#configuring-the-source-code针对系统 python3 进行编译,特别是 python3.7.8

我的 Django 应用程序的虚拟环境也在运行 python3.7.8。

我的 VirtualHost 配置文件如下所示:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName kumberas.com
        ServerAdmin [email protected] /cdn-cgi/l/email-protection

        WSGIScriptAlias / /home/dan/app/kumberas/kumberas/main/wsgi.py
        Alias /static/ /home/dan/app/kumberas/kumberas/static/

        <Directory /home/dan/app/kumberas/kumberas/static>
            Require all granted
        </Directory>

        <Directory /home/dan/app/kumberas/venv>
            Require all granted
        </Directory>

        <Directory /home/dan/app/kumberas/kumberas/main>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>

        <Location />
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require user kr
            AuthBasicProvider file
        </Location>

        WSGIDaemonProcess kumberas \
            python-home=/home/dan/app/kumberas/venv \
            python-path=/home/dan/app/kumberas
        WSGIProcessGroup kumberas

    </VirtualHost>
</IfModule>

当我尝试访问该站点时,我在 Apache 错误日志中收到 500 错误和以下内容。

[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] mod_wsgi (pid=21635): Failed to exec Python script file '/home/dan/app/kumberas/kumberas/main/wsgi.py'.
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] mod_wsgi (pid=21635): Exception occurred processing WSGI script '/home/dan/app/kumberas/kumberas/main/wsgi.py'.
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] Traceback (most recent call last):
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/home/dan/app/kumberas/kumberas/main/wsgi.py", line 12, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     from django.core.wsgi import get_wsgi_application
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/home/dan/app/kumberas/venv/lib/python3.7/site-packages/django/__init__.py", line 1, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     from django.utils.version import get_version
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/home/dan/app/kumberas/venv/lib/python3.7/site-packages/django/utils/version.py", line 1, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     import datetime
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/usr/lib/python3.7/datetime.py", line 8, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     import math as _math
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] ModuleNotFoundError: No module named 'math'

我做了一些挖掘并发现了这个wsgi.py https://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-installation-in-usemod_wsgi 文档中的文件用于检查 mod_wsgi 正在使用的 python 版本,并在访问该站点时获得以下输出:

sys.version = '3.7.2 (default, Jan 23 2020, 09:44:10) \n[GCC 5.4.0 20160609]'
sys.prefix = '/home/dan/app/kumberas/venv'

我假设 python 版本不匹配3.7.8 != 3.7.2是这里的问题,但据我所知,服务器上没有安装 python3.7.2,并且我已经重新安装了 mod_wsgi 几次,以确保将其配置为使用 3.7.8。

我还确认,在虚拟环境中,a) 能够使用以下命令运行 Django 应用程序manage.py runserver和 b) 可以运行get_wsgi_application()在 Django shell 中手动。

有谁知道这个版本不匹配是否是问题所在,以及如何解决?


如果使用 virtualenv 或 virtualenvwrapper 创建环境,则需要调用 wsgi.py 中的激活脚本:

python_home = '/home/dan/app/kumberas/venv'

activate_this = python_home + '/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django/mod_wsgi/Apache - mod_wsgi 未使用为其编译的 Python 版本 - “ModuleNotFoundError:没有名为 'math' 的模块” 的相关文章