Django WGSI paths

2019-05-03 07:19发布

问题:

I am having problems setting up wgsi with django. I'm following this http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/ . Yet I am still really confused as to where to put the .wsgi file and if I need to set the sys.path. I have tried it both directly outside and inside the web root and I can't get anything to work as expected.

# /home/ben/public_html/django_test/testproject/apache/django.wsgi:

import os
import sys

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

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Relivant apache conf:

DocumentRoot "/home/ben/public_html/django_test/testproject/"
WSGIScriptAlias / "/home/ben/public_html/django_test/testproject/apache/django.wsgi"

Apache Logs Error (standard apache 500 page):

ImportError: Could not import settings 'testproject.settings' (Is it on sys.path? ...

I can at get django to at least throw an error of it's own by using this:

import os
import sys

path = '/home/ben/public_html/django_test/testproject'
if path not in sys.path:
    sys.path.append(path)

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

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

which resulted in this django error page:

ImportError at /admin/
No module named testproject.urls

回答1:

I put the wsgi at same level than settings.py, and looks like this:

import os
import sys

sys.path.insert(0,os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))


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

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

this is the apache conf file:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName www.yourprojectname.com
    Alias /media/ /home/diegueus9/workspace/yourprojectname/media/

    <Directory /home/diegueus9/workspace/yourprojectname/media/>
        Order deny,allow
        Allow from all
    </Directory>

    WSGIScriptReloading On
    WSGIDaemonProcess yourprojectname 
    WSGIProcessGroup yourprojectname
    WSGIApplicationGroup yourprojectname
    WSGIPassAuthorization On

    WSGIScriptAlias / /home/diegueus9/workspace/yourprojectname/yourfile.wsgi
    ErrorLog /var/log/apache2/yourprojectname-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel debug

    CustomLog /var/log/apache2/yourprojectname-access.log combined

</VirtualHost>


回答2:

As a quick fix you can, next to adding this to sys.path:

path = '/home/ben/public_html/django_test/testproject'

also add this:

path2 = '/home/ben/public_html/django_test'

You can also change Python path in your Apache VirtualHost:

WSGIDaemonProcess [...] python-path=/home/ben/public_html/django_test

But you should also review mod_wsgi docs and learn what Graham Dumpleton advises there.



回答3:

Here is a nice tutorial that shows you how to use mod_wsgi with Django and also some other items that are not related to this question.



回答4:

I just spent a few hours troubleshooting this very issue and my fix was SELinux was prohibiting access to my /django/ folder.

To check if SEStatus is blocking your access, view the log at /var/log/audit/audit.log

My fix was a simple:

restorecon -R /var/www/django/