我在和django建立wgsi时遇到问题。我正在关注这个http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/。但是,我仍然很困惑该将.wsgi文件放在哪里,以及是否需要设置sys.path。我已经在web根目录的外部和内部都尝试过了,但是我不能像预期的那样工作。

# /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()

缓解APACHE CONF:
DocumentRoot "/home/ben/public_html/django_test/testproject/"
WSGIScriptAlias / "/home/ben/public_html/django_test/testproject/apache/django.wsgi"

apache日志错误(标准apache 500页):
ImportError: Could not import settings 'testproject.settings' (Is it on sys.path? ...

我至少可以让django自己抛出一个错误:
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()

导致此django错误页:
ImportError at /admin/
No module named testproject.urls

最佳答案

我把WSGI放在与Stutux.Py相同的级别,看起来像这样:

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()

这是apache conf文件:
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>

08-18 18:48
查看更多