关于wsgi.py中wsgi模块的Ubuntu 16.04服务器错误

时间:2017-08-12 20:37:23

标签: django apache ubuntu server mod-wsgi

我要在虚拟主机的ubuntu服务器上设置django应用程序 我已经配置了我的虚拟主机以便出现问题! 但是由于你在这里看到的一些问题,它仍然会给我500个内部错误:

mod_wsgi (pid=31972): Target WSGI script '/var/www/mmcba/mmemp/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=31972): Exception occurred processing WSGI script '/var/www/mmcba/mmemp/wsgi.py'.
Traceback (most recent call last):
  File "/var/www/mmcba/mmemp/wsgi.py", line 25, in <module>
    application = django.core.handlers.wsgi.WSGIHandler()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/wsgi.py", line 151, in __init__
    self.load_middleware()
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 80, in load_middleware
    middleware = import_string(middleware_path)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/module_loading.py", line 20, in import_string
    module = import_module(module_path)
  File "/usr/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 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/middleware.py", line 4, in <module>
    from django.contrib.auth.backends import RemoteUserBackend
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/backends.py", line 4, in <module>
    from django.contrib.auth.models import Permission
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 110, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 247, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 125, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

我非常感谢您的回复

<小时/> 更新#2

如果我在wsgi.py中使用此代码:

import os

from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()

堆栈跟踪将成为:( mmemp是我应用的名称):

 mod_wsgi (pid=5579): Target WSGI script '/var/www/mmcba/mmemp/wsgi.py' cannot be loaded as Python module.
 mod_wsgi (pid=5579): Exception occurred processing WSGI script '/var/www/mmcba/mmemp/wsgi.py'.
 Traceback (most recent call last):
   File "/var/www/mmcba/mmemp/wsgi.py", line 16, in <module>
     application = get_wsgi_application()
   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
     django.setup(set_prefix=False)
   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 22, in setup
     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 56, in __getattr__
     self._setup(name)
   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 41, in _setup
     self._wrapped = Settings(settings_module)
   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 110, in __init__
     mod = importlib.import_module(self.SETTINGS_MODULE)
   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
 ImportError: No module named mmemp.settings

我的虚拟主机配置,这是:

Listen 80

NameVirtualHost 148.251.218.202:80

<VirtualHost *:80>
        ServerAdmin webmaster@mmcba.com
        ServerName mmcba.com
        ServerAlias www.mmcba.com

        WSGIScriptAlias / /var/www/mmcba/mmemp/wsgi.py

        <Directory /var/www/mmcba/mmemp>
        <Files wsgi.py>
        Require all granted
        </Files>
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

2 个答案:

答案 0 :(得分:2)

如果您正在使用django 1.7+,请将wsgi.py的内容替换为:

import os

from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()

或尝试将以下行添加到settings.py

import django
django.setup()

有一个类似的问题here。希望它有所帮助。

答案 1 :(得分:1)

解决方案:
将项目和应用程序目录添加到wsgi.py中的sys.path并且它可以正常工作。
它适用于您应用中的每个未知模块

import sys
sys.path.append('<Project_Directory>')
sys.path.append('<Project_Directory>/AppName')