让Python 2.7与我的django项目一起工作的问题

时间:2016-11-17 21:00:04

标签: django apache python-2.7 virtualenv mod-wsgi

有一段时间了,我一直试图让我的django项目安装badgr(github上的badgr-server)与mod_wsgi一起工作。我很喜欢我,但我相信我已经取得了一些进展。以下是我目前收到的错误消息:

    [Thu Nov 17 12:48:48.182772 2016] [core:notice] [pid 24556] AH00094:    Command line: '/opt/rh/httpd24/root/usr/sbin/httpd'
    [Thu Nov 17 12:48:50.421089 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] mod_wsgi (pid=24558): Target WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py' cannot be loaded as Python module.
    [Thu Nov 17 12:48:50.421165 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] mod_wsgi (pid=24558): Exception occurred processing WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py'.
    [Thu Nov 17 12:48:50.421200 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] Traceback (most recent call last):
    [Thu Nov 17 12:48:50.421228 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]   File "/opt/badgr/code/apps/mainsite/wsgi.py", line 27, in <module>
    [Thu Nov 17 12:48:50.421348 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]     application = get_wsgi_application()
    [Thu Nov 17 12:48:50.421374 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]   File "/opt/badgr/env/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    [Thu Nov 17 12:48:50.421450 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]     django.setup()
    [Thu Nov 17 12:48:50.421472 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]   File "/opt/badgr/env/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
    [Thu Nov 17 12:48:50.421558 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    [Thu Nov 17 12:48:50.421581 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]   File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    [Thu Nov 17 12:48:50.421705 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]     self._setup(name)
    [Thu Nov 17 12:48:50.421728 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]   File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    [Thu Nov 17 12:48:50.421764 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]     self._wrapped = Settings(settings_module)
    [Thu Nov 17 12:48:50.421782 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]   File "/opt/badgr/env/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
    [Thu Nov 17 12:48:50.421811 2016] [:error] [pid 24558] [remote 140.225.0.153:60440]     % (self.SETTINGS_MODULE, e)
    [Thu Nov 17 12:48:50.421841 2016] [:error] [pid 24558] [remote 140.225.0.153:60440] ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings`

以下是我的wsgi.py(/opt/badgr/code/apps/mainsite/wsgi.py)的副本:

    """
    WSGI config for badgr project.

    It exposes the WSGI callable as a module-level variable named ``application``.

    For more information on this file, see
    https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
    """

    import sys
    import os
    import site

    activate_this = os.path.expanduser("/opt/badgr/env/bin/activate_this.py")
    execfile(activate_this, dict(__file__=activate_this))

    path = '/opt/badgr/code/apps/mainsite'
    if path not in sys.path:
       sys.path.append(path)

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

    site.addsitedir('/opt/badgr/env/lib/python2.7/site-packages')


    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

我很确定我的设置文件是/opt/badgr/code/apps/mainsite/settings.py(或.pyc)。但它似乎很难被召唤出来。

还应该注意我正在运行Python 2.7的环境:         /opt/badgr/env/lib/python2.7 /

Finnally:这是我在httpd24中的django配置(/opt/rh/httpd24/root/etc/httpd/conf.d/django.conf):

    Alias /static /opt/badgr/code/staticfiles
    <Directory /opt/badgr/code/staticfiles>
     Require all granted
    </Directory>


    <Directory /opt/badgr/code/apps/mainsite>
        <Files wsgi.py>
     Require all granted
        </Files>
    </Directory>

    WSGISocketPrefix /var/run/wsgi/wsgi
    WSGIDaemonProcess badgr
    WSGIProcessGroup badgr
    WSGIPythonPath /opt/badgr:/opt/badgr/env/lib/python2.7/site-packages
    WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py
    WSGIPythonHome /opt/badgr/env

感谢任何帮助或帮助。

1 个答案:

答案 0 :(得分:0)

对于Apache配置,请尝试使用:

Alias /static /opt/badgr/code/staticfiles
<Directory /opt/badgr/code/staticfiles>
 Require all granted
</Directory>

<Directory /opt/badgr/code/apps/mainsite>
    <Files wsgi.py>
    Require all granted
    </Files>
</Directory>

WSGISocketPrefix /var/run/wsgi/wsgi
WSGIRestrictEmbedded On

WSGIDaemonProcess badge python-home=/opt/badgr/env python-path=/opt/badgr/code/apps
WSGIProcessGroup badge

WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py

对于wsgi.py文件,将其恢复为与最初创建的文件类似。

import os

os.environ["DJANGO_SETTINGS_MODULE"] = 'mainsite.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

WSGIRestrictEmbedded确保仅使用mod_wsgi守护程序模式。

使用python-home选项将Python虚拟环境的位置设置为WSGIDaemonProcess。在python-path中设置项目的父目录。更改DJANGO_SETTINGS_MODULE以再次在模块路径中包含项目名称。

更新1

添加到wsgi.py文件:

import sys
print 'SYS.PATH = %r' % sys.path

检查Apache错误日志以查看路径是什么,并验证列出项目所在位置的父目录。