使用mod_wsgi在Apache上启动Django时出错500

时间:2017-01-29 07:45:10

标签: python django apache postgresql mod-wsgi

我尝试使用 postgre DB在Apache(使用mod_wsgi)上启动Django(1.10.5)时出现500错误。安装依赖项是django和。的 psycopg2 即可。它们安装在 Python 3.6 虚拟环境中。

提示:当我尝试通过" python manage.py runserver 0.0.0.0:8000"来启动虚拟环境时,一切正常。但是当我尝试在apache上访问:80端口时,/ var / log / httpd / error_log中列出了以下错误

Handling WSGI exception 
Traceback (most recent call last):
File "/root/djangotest/djangotest/wsgi.py", line 40, in <module> application = get_wsgi_application()
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application django.setup(set_prefix=False)
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS)
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate app_config.import_models(all_models)
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/apps/config.py", line 199, in import_models self.models_module = import_module(models_module_name)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module __import__(name)
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/contrib/auth/models.py", line 4, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 52, in <module> class AbstractBaseUser(models.Model):
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/db/models/base.py", line 119, in __new__ new_class.add_to_class('_meta', Options(meta, app_label))
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/db/models/base.py", line 316, in add_to_class value.contribute_to_class(cls, name)
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/db/models/options.py", line 214, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/db/utils.py", line 211, in __getitem__ backend = load_backend(db['ENGINE'])
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/db/utils.py", line 115, in load_backend return import_module('%s.base' % backend_name)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module __import__(name)
File "/root/djangotest/djangotestvenv/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 24, in <module> raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
ImproperlyConfigured: Error loading psycopg2 module: No module named _psycopg [remote 88.AAA.BBB.CC:76] mod_wsgi (pid=18487): Target WSGI script '/root/djangotest/djangotest/wsgi.py' does not contain WSGI application 'application'.

/etc/httpd/conf.d/django.conf的内容:

Alias /static /root/djangotest/static

WSGIScriptAlias / /root/djangotest/djangotest/wsgi.py
WSGIPythonPath /root/djangotest:/root/djangotest/djangotestvenv/lib/python3.6/site-packages

<Directory /root/djangotest/static>
    Require all granted
</Directory>

<Directory /root/djangotest/djangotest>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess djangotest python-path=/root/djangotest:/root/djangotest/djangotestvenv/lib/python3.6/site-packages
WSGIProcessGroup djangotest

wsgi.py的内容:

import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'djangotest.settings'

try:
    application = get_wsgi_application()
    print("WSGI without exception")
except Exception:
    print("handling WSGI exception")
    # Error loading applications
    if 'mod_wsgi' in sys.modules:
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGINT)
        time.sleep(2.5)

尽管psycopg2成功地安装在venv上并且工作正常,但是当尝试达到时,它会引发错误:80通过apache&amp; mod_wsgi的。

谢谢。

0 个答案:

没有答案