我在使用 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 配置:
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 放在与 settings.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 配置文件:
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>
关于python - Django WSGI 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4194243/