Django Apache集成缺少模块
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django Apache集成缺少模块相关的知识,希望对你有一定的参考价值。
我正在尝试设置我的第一个Django项目,而Apache集成存在一些问题。
这里是我的文件树
venv-django
aci_project/
|_manage.py
|_aci_project/
| |_ __init__.py
| |_settings.py
| |_ urls.py
| |_ asgi.py
| |_ wsgi.py
|
|_aci_api/
|_ __init__.py
|_aci_script
|_admin.py
|_apps.py
|_migrations/
| |_ __init__.py
|_static --> css
|_templates --> template html
|_tests.py
|_urls.py
|_views.py
基本上,流程如下:
浏览器-> aci_project-> urls.py-> aci_api-> views.py-> aci_script中的调用脚本
如果我运行命令“ python manage.py runserver”,它将正常工作。
现在,我正在尝试将Django项目与Apache集成,但无法正常工作。
这是我在Apache中的vhost文件:
<VirtualHost *:8000>
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/httpd/access_log combine
<Directory /var/opt/aci_project/aci_project/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIApplicationGroup aciweb01t.stluc.ucl.ac.be
WSGIDaemonProcess aciweb01t.stluc.ucl.ac.be group=www-data python-path=/var/opt/aci_project
WSGIProcessGroup aciweb01t.stluc.ucl.ac.be
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/opt/aci_project/aci_project/wsgi.py
</VirtualHost>
这里是我在aci_project中的wsgi.py文件-> manage.py-> _aci_project
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'aci_project.settings')
application = get_wsgi_application()
这是我在apache日志中的错误:
[Wed Apr 22 16:49:12.468252 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] mod_wsgi (pid=7818): Failed to exec Python script file '/var/opt/aci_project/aci_project/wsgi.py'.
[Wed Apr 22 16:49:12.468453 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] mod_wsgi (pid=7818): Exception occurred processing WSGI script '/var/opt/aci_project/aci_project/wsgi.py'.
[Wed Apr 22 16:49:12.468926 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] Traceback (most recent call last):
[Wed Apr 22 16:49:12.469073 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "/var/opt/aci_project/aci_project/wsgi.py", line 16, in <module>
[Wed Apr 22 16:49:12.469092 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] application = get_wsgi_application()
[Wed Apr 22 16:49:12.469109 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "/var/opt/venv-django/lib64/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Wed Apr 22 16:49:12.469118 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] django.setup(set_prefix=False)
[Wed Apr 22 16:49:12.469132 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "/var/opt/venv-django/lib64/python3.6/site-packages/django/__init__.py", line 19, in setup
[Wed Apr 22 16:49:12.469140 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Wed Apr 22 16:49:12.469153 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "/var/opt/venv-django/lib64/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
[Wed Apr 22 16:49:12.469162 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] self._setup(name)
[Wed Apr 22 16:49:12.469175 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "/var/opt/venv-django/lib64/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
[Wed Apr 22 16:49:12.469183 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] self._wrapped = Settings(settings_module)
[Wed Apr 22 16:49:12.469196 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "/var/opt/venv-django/lib64/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
[Wed Apr 22 16:49:12.469204 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] mod = importlib.import_module(self.SETTINGS_MODULE)
[Wed Apr 22 16:49:12.469234 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
[Wed Apr 22 16:49:12.469245 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] return _bootstrap._gcd_import(name[level:], package, level)
[Wed Apr 22 16:49:12.469259 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Wed Apr 22 16:49:12.469272 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Wed Apr 22 16:49:12.469285 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
[Wed Apr 22 16:49:12.469298 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Wed Apr 22 16:49:12.469323 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Wed Apr 22 16:49:12.469337 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Wed Apr 22 16:49:12.469350 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Wed Apr 22 16:49:12.469412 2020] [wsgi:error] [pid 7818:tid 140205314995968] [remote 10.195.113.74:28031] ModuleNotFoundError: No module named 'aci_project'
非常感谢您的帮助!
答案
我添加以如下方式更新文件wsgi.py:
#sys.path.append('/var/opt/aci_project')
sys.path.append('/var/opt/aci_project/aci_project')
sys.path.append('/var/opt/aci_project/aci_api')
#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'aci_project.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
以上是关于Django Apache集成缺少模块的主要内容,如果未能解决你的问题,请参考以下文章
django rest_framework 中缺少授权标头,是 apache 的错吗?
尝试将 Django 与 Apache 集成时出现 ServerSelectionTimeoutError