使用 WSGI 和 apache 设置 django
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setup django with WSGI and apache
提问by Doug Miller
I have been sold on mod_wsgi and apache rather than mod_python. I have all the parts installed (django, apache, mod_wsgi) but have run into a problem deploying.
我已经在 mod_wsgi 和 apache 上出售而不是 mod_python。我已经安装了所有部件(django、apache、mod_wsgi),但是在部署时遇到了问题。
I am on osx 10.5 with apache 2.2 and django 1.0b2, mod_wsgi-2.3
我在 osx 10.5 上使用 apache 2.2 和 django 1.0b2,mod_wsgi-2.3
My application is called tred.
我的应用程序称为 tred。
Here are the relevant files: httpd-vhosts (included in httpd-conf)
以下是相关文件:httpd-vhosts(包含在 httpd-conf 中)
NameVirtualHost tred:80
ServerName tred
Alias /admin_media /usr/lib/python2.5/site-packages/django/contrib/admin/media
Order allow,deny
Allow from all
Alias /media /Users/dmg/Sites/tred/media
Order allow,deny
Allow from all
Alias / /Users/dmg/Sites/tred/
Order allow,deny
Allow from all
WSGIScriptAlias / /Users/dmg/Sites/tred/mod_wsgi-handler.wsgi
WSGIDaemonProcess tred user=dmg group=staff processes=1 threads=10
WSGIProcessGroup tred
mod_wsgi-handle.wsgi
mod_wsgi-handle.wsgi
import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..') os.environ['DJANGO_SETTINGS_MODULE'] = 'tred.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
When I go to http://tredI get a directory listing rather than the rendered website. I think I have followed the tutorials correctly but it is obviously not right. What can I do to make this work?
当我访问http://tred 时,我得到的是目录列表,而不是呈现的网站。我想我已经正确地遵循了教程,但这显然是不对的。我该怎么做才能使这项工作发挥作用?
采纳答案by John Millikin
What happens if you remove the Alias /directive?
如果删除Alias /指令会发生什么?
回答by Graham Dumpleton
Note that Alias and WSGIScriptAlias directives do not have the same precedence. Thus, they will not be processed in file order as written. Instead, all Alias directives get precedence over WSGIScriptAlias directives. Thus, it wouldn't have mattered if the Alias for '/' appeared after WSGIScriptAlias, it would still have taken precedence.
请注意, Alias 和 WSGIScriptAlias 指令没有相同的优先级。因此,它们不会按照写入的文件顺序进行处理。相反,所有 Alias 指令都优先于 WSGIScriptAlias 指令。因此,如果 '/' 的别名出现在 WSGIScriptAlias 之后并不重要,它仍然具有优先权。
回答by John Millikin
It works. I have no idea why, but it does.
有用。我不知道为什么,但确实如此。
For future reference:
备查:
It works because Apache processes alias directives in order, and uses the first match. It was always hitting Alias /, which will match anything, before WSGIScriptAlias.
它起作用是因为 Apache 按顺序处理别名指令,并使用第一个匹配项。它总是命中Alias /,它会匹配任何东西,之前WSGIScriptAlias。
From the mod_aliasdocumentation:
First, all Redirects are processed before Aliases are processed, and therefore a request that matches a
RedirectorRedirectMatchwill never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence.
首先,在处理别名之前处理所有重定向,因此匹配 a
Redirect或RedirectMatch永远不会应用别名的请求。其次,别名和重定向按照它们在配置文件中出现的顺序进行处理,第一个匹配优先。
回答by Shashank Singla
try following this tutorial - http://singlas.in/5-step-tutorial-for-using-django-with-apache-and-mod_wsgi/
尝试遵循本教程 - http://singlas.in/5-step-tutorial-for-using-django-with-apache-and-mod_wsgi/
you are trying to host apache /var/www/ folder and the Django app both at root (/). Since Alias directive takes precedence over WSGIScriptAlias, it is rendering apache directory.
您正在尝试在根 (/) 中托管 apache /var/www/ 文件夹和 Django 应用程序。由于 Alias 指令优先于 WSGIScriptAlias,因此它正在呈现 apache 目录。
you can try to host the django app at /app. Alternatively host the /var/www/ folder at a different location like /public
您可以尝试在 /app 上托管 django 应用程序。或者将 /var/www/ 文件夹托管在不同的位置,如 /public

