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



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

我从源代码安装了mod_wsgi,并将其配置为针对系统python3进行编译,特别是python3.7.8

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

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

当我尝试访问该网站时,我会在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'

我做了一些挖掘,在mod_wsgi文档中找到了这个wsgi.py文件,用于检查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(能够使用manage.py runserver运行Django应用程序,b(可以在Django shell中手动运行get_wsgi_application()

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

如果使用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))

看起来可能存在版本不匹配的情况。一些linux仍然使用python 2.7作为默认设置。

同时检查系统是否有多个使用locate python和/或locate python3的python版本

如果这是modwsgi上的一个问题,那么通过以下链接从源代码构建mod_wsgi可能会更容易https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html

最新更新