mod_wsgi:处理WSGI脚本(django部署)发生异常

时间:2018-03-15 13:40:35

标签: django apache mod-wsgi

我尝试使用以下Apache配置部署django项目:

Apache虚拟主机配置

<Virtualhost *:80>
    DocumentRoot /var/www/project/backend
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    WSGIDaemonProcess backend python-home=/var/www/project/myenv python-path=/var/www/gestor_documental/backend

    WSGIProcessGroup backend
    WSGIScriptAlias / /var/www/project/backend/backend/wsgi.py process-group=backend
    Alias /static/ /var/www/project/backend/static/

    <Directory /var/www/project/backend>
            Require all granted
    </Directory>
</Virtualhost>

wsgi.load文件

LoadModule wsgi_module "/var/www/project/myenv/lib/python3.5/site-packages/mod_wsgi/server/mod_wsgi-py35.cpython-35m-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/project/myenv"

wsgi.py是django默认带来的那个

wsgi.py

import os
from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

这是项目树:

project
|- backend
|  |- api (django app)
|  |- backend
|     |- ...
|     |- settings.py
|     |- wsgi.py
|-- myenv (virtualenv)

这是我尝试加载网页时遇到的错误日志:

mod_wsgi (pid=38953): Failed to exec Python script file '/var/www/project/backend/backend/wsgi.py'.
mod_wsgi (pid=38953): Exception occurred processing WSGI script '/var/www/project/backend/backend/wsgi.py'.
Traceback (most recent call last):
 File "/var/www/project/backend/backend/wsgi.py", line 16, in <module>
  application = get_wsgi_application()
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
  django.setup(set_prefix=False)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/__init__.py", line 22, in setup
  configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
  self._setup(name)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
  self._wrapped = Settings(settings_module)
 File "/var/www/project/myenv/lib/python3.5/site-packages/django/conf/__init__.py", line 97, in __init__
  mod = importlib.import_module(self.SETTINGS_MODULE)
 File "/var/www/project/myenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 986, in _gcd_import
 File "<frozen importlib._bootstrap>", line 969, in _find_and_load
 File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
 File "<frozen importlib._bootstrap>", line 986, in _gcd_import
 File "<frozen importlib._bootstrap>", line 969, in _find_and_load
 File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'backend'

我已经按照每个教程进行了大量配置,但仍然遇到了相同的错误。

我在virtualenv中使用 python 3.5.2 Apache 2.4.18

我已经通过pip3安装了mod_wsgi。

有人可以帮助我,并告诉我我做错了什么?

谢谢。

2 个答案:

答案 0 :(得分:2)

我解决了更改wsgi.py文件添加下一行的问题:

import sys

sys.path.append('/var/www/project/backend')

我不知道这是否是正确的答案,但它实际上适用于此解决方法。

答案 1 :(得分:0)

它在wsgi.py

中的这一行
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")

backend.settings中没有project/backend/backend。 它正在尝试查找第三个后端文件夹,因此请从该行中删除backend