---
title: "Apache + Django + WSGI 配置"
author: "kingoal"
date: "2009-06-22T23:15:11Z"
tags: []
slug: "apache-django-wsgi"
draft: false
lastmod: "2009-06-22T23:15:11Z"
id: 413169
url: "https://kingoal.iteye.com/blog/413169"
---

下面假设我的django应用程序放置在d:/works/web/myweb/

 

今天参考mod\_wsgi的教程，配置了基于apache+mod\_wsgi的django的部署，下面记录下来，以备将来参考

 

- 当然是下载mod\_wsgi， 官方主页为[http://code.google.com/p/modwsgi/](http://code.google.com/p/modwsgi/ "mod_wsgi") , 当然你也可以下载已经编译好的版本(binary)， [http://code.google.com/p/modwsgi/downloads/list](http://code.google.com/p/modwsgi/downloads/list)
- 然后在apache的配置文件httpd.conf里面添加：

#WSGI For Python Integration  
LoadModule wsgi\_module modules/mod\_wsgi.so

 

再在httpd.conf的最后部分添加Include "d:/works/web/myweb/apache/apache\_django\_wsgi.conf"

 

- 在d:/works/web下面使用python django-admin.py startproject myweb来创建应用程序myweb
- 在django的应用程序目录myweb下创建目录apache,然后在目录下放置如下两个文件apache\_django\_wsgi.conf和django.wsgi

apache\_django\_wsgi.conf的内容为:

 

Alias /site\_media/ "d:/works/web/myweb/media/"

&lt;Directory "d:/works/web/myweb/media"&gt;

Order allow,deny

Options Indexes

Allow from all

IndexOptions FancyIndexing

&lt;/Directory&gt;

 

Alias /media/ "D:/Sources/Library/django/django/contrib/admin/media/"

&lt;Directory "D:/Sources/Library/django/django/contrib/admin/media"&gt;

Order allow,deny

Options Indexes

Allow from all

IndexOptions FancyIndexing

&lt;/Directory&gt;

 

WSGIScriptAlias / "d:/works/web/myweb/apache/django.wsgi"

 

&lt;Directory "d:/works/web/myweb/apache"&gt;

Allow from all

&lt;/Directory&gt;

 

django.wsgi的内容为:

import os, sys

 

#Calculate the path based on the location of the WSGI script.

apache\_configuration= os.path.dirname(\_\_file\_\_)

project = os.path.dirname(apache\_configuration)

workspace = os.path.dirname(project)

sys.path.append(workspace) 

 

os.environ\['DJANGO\_SETTINGS\_MODULE'] = 'myweb.settings'

import django.core.handlers.wsgi

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

 

print &gt;&gt; sys.stderr, sys.path

 

然后启动apache的web服务器，输入http://localhost就会出现Django的欢迎页面