简体   繁体   English

Django/mod_wsgi/Apache - mod_wsgi 没有使用为它编译的 Python 版本 - “ModuleNotFoundError: No module named 'math'”

[英]Django/mod_wsgi/Apache - mod_wsgi is not using the Python version it was compiled for - “ModuleNotFoundError: No module named 'math' ”

I'm attempting to deploy a Django application with Apache2 and mod_wsgi on a Ubuntu 16.04.6 server, but I'm struggling with getting mod_wsgi to use the correct python version. I'm attempting to deploy a Django application with Apache2 and mod_wsgi on a Ubuntu 16.04.6 server, but I'm struggling with getting mod_wsgi to use the correct python version.

I installed mod_wsgi from source, and configured it to compile against the system python3, specifically python3.7.8我从源代码安装了 mod_wsgi,并将其配置为针对系统 python3 编译,特别是 python3.7.8

My Virtual environment for the Django app is also running python3.7.8.我的 Django 应用程序的虚拟环境也在运行 python3.7.8。

My VirtualHost config file looks like this:我的 VirtualHost 配置文件如下所示:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName kumberas.com
        ServerAdmin webmaster@localhost.com

        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>

And when I try to access the site, I get a 500 Error and the following in my Apache error log.当我尝试访问该站点时,我的 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'

I did a bit of digging and found this wsgi.py file in the mod_wsgi documentation for checking the python version that mod_wsgi is using, and get the following output when I visit the site:我做了一些挖掘,在 mod_wsgi 文档中找到了这个wsgi.py文件,用于检查 mod_wsgi 正在使用的 python 版本,并在我访问该站点时得到以下 output:

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'

I assume the mismatching python version 3.7.8.= 3.7.2 is the issue here, but as far as I'm aware there isn't a python3.7.2 installation on the server, and I've reinstalled mod_wsgi several times making certain that I configure it to use 3.7.8.我认为不匹配的 python 版本3.7.8.= 3.7.2是这里的问题,但据我所知,服务器上没有安装 python3.7.2,并且我已经重新安装了 mod_wsgi 多次确定我将其配置为使用 3.7.8。

I've also confirmed that while in the virtual environment I'm, a) able to run the Django application with manage.py runserver , and b) can run get_wsgi_application() manually in the Django shell.我还确认,在虚拟环境中,我 a) 能够使用manage.py runserver运行 Django 应用程序,并且 b) 可以在 Django Z2591C98B70119FE6E248 中手动运行get_wsgi_application()

Does anyone have any knowledge on whether this version mismatch is the issue, and how it could be resolved?有谁知道这个版本不匹配是否是问题,以及如何解决?

If virtualenv or virtualenvwrapper have been used to create environment then you need to call activation script in wsgi.py:如果已使用 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))

It looks like you might have a version mismatch.看起来你可能有版本不匹配。 Some linux is still using python 2.7 as the default.一些 linux 仍然使用 python 2.7 作为默认值。

Also check if the system has more than one version of python using locate python and/or locate python3还要检查系统是否有多个版本的 python 使用locate python和/或locate python3

If it's an issue on modwsgi , it might be easier for you to build mod_wsgi from source following link https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html如果这是modwsgi的问题,您可能更容易通过链接https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html从源代码构建 mod_wsgi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM