首页 > 解决方案 > wsgi.py 在 apache 上的 virtualenv 中失败,并显示“没有名为 'django' 的模块”

问题描述

我在 Amazon Linux EC2 服务器上运行 Django webapp,并且已正确安装和验证 Apache 和 mod_wsgi。我如下所示配置了 Apache 配置文件,并通过打印验证了 Python 中是否加载了正确的路径sys.path(显示在错误日志中),但是,我不断收到 500 错误,wsgi.py文件失败并出现ModuleNotFoundError: No module named 'django'错误。在 apache 配置文件中使用守护程序模式时出现相同的错误。我错过了什么?

我的 virtualenv 在/var/www/html/test,我的 webapp 在/var/www/html/ai_demo_webapp/webapp(这是我的manage.py文件的位置)。我正在使用 Apache 2.2.34 和 mod_wsgi 的 pip 版本,并将 wsgi 模块加载到 apache 配置文件中为LoadModule wsgi_module "/home/ec2-user/.local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so".

wsgi.py

import os
import sys

print(sys.path)

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "webapp.settings")

application = get_wsgi_application()

阿帕奇配置文件

WSGIPythonHome /var/www/html/test
WSGIPythonPath /var/www/html/ai_demo_webapp/webapp

<VirtualHost *:80>
WSGIScriptAlias / /var/www/html/ai_demo_webapp/webapp/webapp/wsgi.py

<Directory /var/www/html/ai_demo_webapp/webapp/webapp>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>

重新启动 apache 并访问站点时的 Apache 错误日志

[Mon Jun 11 05:01:46 2018] [notice] SIGHUP received.  Attempting to restart
[Mon Jun 11 05:01:46 2018] [notice] Digest: generating secret for digest authentication ...
[Mon Jun 11 05:01:46 2018] [notice] Digest: done
[Mon Jun 11 05:01:46 2018] [notice] Apache/2.2.34 (Unix) DAV/2 mod_wsgi/4.6.4 Python/3.6 configured -- resuming normal operations
[Mon Jun 11 05:01:50 2018] [error] ['/var/www/html/ai_demo_webapp/webapp', '/var/www/html/test/local/lib64/python3.6/site-packages', '/var/www/html/test/local/lib/python3.6/site-packages', '/var/www/html/test/lib64/python3.6', '/var/www/html/test/lib/python3.6', '/var/www/html/test/lib64/python3.6/site-packages', '/var/www/html/test/lib/python3.6/site-packages', '/var/www/html/test/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/lib64/python3.6', '/usr/lib/python3.6', '/usr/lib64/python3.6/dist-packages', '/usr/lib/python3.6/dist-packages', '/usr/lib64/python3.6/lib-dynload']
[Mon Jun 11 05:01:50 2018] [error] [client 75.82.153.200] mod_wsgi (pid=27915): Failed to exec Python script file '/var/www/html/ai_demo_webapp/webapp/webapp/wsgi.py'.
[Mon Jun 11 05:01:50 2018] [error] [client 75.82.153.200] mod_wsgi (pid=27915): Exception occurred processing WSGI script '/var/www/html/ai_demo_webapp/webapp/webapp/wsgi.py'.
[Mon Jun 11 05:01:50 2018] [error] [client 75.82.153.200] Traceback (most recent call last):
[Mon Jun 11 05:01:50 2018] [error] [client 75.82.153.200]   File "/var/www/html/ai_demo_webapp/webapp/webapp/wsgi.py", line 27, in <module>
[Mon Jun 11 05:01:50 2018] [error] [client 75.82.153.200]     from django.core.wsgi import get_wsgi_application
[Mon Jun 11 05:01:50 2018] [error] [client 75.82.153.200] ModuleNotFoundError: No module named 'django'
[Mon Jun 11 05:01:51 2018] [error] ['/var/www/html/ai_demo_webapp/webapp', '/var/www/html/test/local/lib64/python3.6/site-packages', '/var/www/html/test/local/lib/python3.6/site-packages', '/var/www/html/test/lib64/python3.6', '/var/www/html/test/lib/python3.6', '/var/www/html/test/lib64/python3.6/site-packages', '/var/www/html/test/lib/python3.6/site-packages', '/var/www/html/test/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/lib64/python3.6', '/usr/lib/python3.6', '/usr/lib64/python3.6/dist-packages', '/usr/lib/python3.6/dist-packages', '/usr/lib64/python3.6/lib-dynload']
[Mon Jun 11 05:01:51 2018] [error] [client 75.82.153.200] mod_wsgi (pid=27919): Failed to exec Python script file '/var/www/html/ai_demo_webapp/webapp/webapp/wsgi.py'., referer: http://ec2-54-149-122-177.us-west-2.compute.amazonaws.com/
[Mon Jun 11 05:01:51 2018] [error] [client 75.82.153.200] mod_wsgi (pid=27919): Exception occurred processing WSGI script '/var/www/html/ai_demo_webapp/webapp/webapp/wsgi.py'., referer: http://ec2-54-149-122-177.us-west-2.compute.amazonaws.com/
[Mon Jun 11 05:01:51 2018] [error] [client 75.82.153.200] Traceback (most recent call last):, referer: http://ec2-54-149-122-177.us-west-2.compute.amazonaws.com/
[Mon Jun 11 05:01:51 2018] [error] [client 75.82.153.200]   File "/var/www/html/ai_demo_webapp/webapp/webapp/wsgi.py", line 27, in <module>, referer: http://ec2-54-149-122-177.us-west-2.compute.amazonaws.com/
[Mon Jun 11 05:01:51 2018] [error] [client 75.82.153.200]     from django.core.wsgi import get_wsgi_application, referer: http://ec2-54-149-122-177.us-west-2.compute.amazonaws.com/
[Mon Jun 11 05:01:51 2018] [error] [client 75.82.153.200] ModuleNotFoundError: No module named 'django', referer: http://ec2-54-149-122-177.us-west-2.compute.amazonaws.com/

标签: pythondjangoapachevirtualenvmod-wsgi

解决方案


推荐阅读