Issues getting Python 2.7 to work with my django p

2019-09-12 03:18发布

问题:

For a while now I have been trying to get my django project install of badgr (badgr-server on github) working with mod_wsgi. I am pretty way in over my head but I do believe I have made some progress. Here is the error message I am currently getting:

    [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`

Here is a copy of my 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()

I am pretty sure my settings file is /opt/badgr/code/apps/mainsite/settings.py (or .pyc). But it seems to have trouble being called up.

Also it should be noted that I am running an env of Python 2.7: /opt/badgr/env/lib/python2.7/

Finnally: Here is my django config in httpd24 (/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

Any help or assistance appreciated.

回答1:

For the Apache configuration try using:

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

For the wsgi.py file revert it back to being similar to what was created originally.

import os

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

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

The WSGIRestrictEmbedded ensures only mod_wsgi daemon mode is used.

Set the location of the Python virtual environment using python-home option to WSGIDaemonProcess. Set the parent directory of your project in python-path. Change DJANGO_SETTINGS_MODULE to include the project name in module path again.


UPDATE 1

Add to the wsgi.py file:

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

Check the Apache error log to see what the path is and verify that the parent directory above where your project is, is listed.