Apache+mod_wsgi+Django windows下的安装和配置

来源:互联网 发布:安卓 api 网络请求 编辑:程序博客网 时间:2024/06/09 18:46

 

我的环境XP, juxin在c:/下

1) 安装Python2.5

2) 安装Django1.1.1

检测:

      cd c:/

python django-admin.py startprotject juxin
cd juxin

#启动mytest的server
manage.py runserver 
open http://127.0.0.1:8000/ 你将会看到 welcome to django,django 安装成功

3) 安装Apache2.2, 并下载mod_wsgi.so, 拷贝到Apache的modules目录,在httpd.conf配置文件中添加以下

LoadModule wsgi_module modules/mod_wsgi.so

(#注意wsgi_module而非mod_wsgi; modules/mod_wsgi.so中 mod_wsgi.so应为实际mod_wsgi.so文件的名称)

 


 

Include "c:/juxin/apache_django_wsgi.conf "

 

 

 

 

4) 在juxin的目录添加两个文件apache_django_wsgi.conf(site的配置文件)和django.wsgi(wsgi脚本)


apache_django_wsgi.conf

 

AliasMatch /js/.*/.(js) c:/media/js/$1 

 

<Directory "c:/media/js "> 

Order allow,deny

Options Indexes

Allow from all

#IndexOptions FancyIndexing

</Directory>

 

Alias /static/.*/.(css|gif|png|jpg|jpeg) c:/media/static/$1 

 

<Directory "c:/media/static "> 

Order allow,deny

Options Indexes

Allow from all

#IndexOptions FancyIndexing

</Directory>

 

 

 

#for testing wsgi

#WSGIScriptAlias / "dc:/juxin/test.wsgi "  

 

WSGIScriptAlias / "c:/django.wsgi "

 

<Directory "c:/juxin "> 

Allow from all

</Directory>

 

django.wsgi

 

import os, sys

 

#Calculate the path based on the location of the WSGI 

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'] = 'juxin.settings'

import django.core.handlers.wsgi

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

 

 

这里需要解释一下, 'juxin.settings' 中 juxin是我的Django项目名, 

apache_django_wsgi.conf中的 #WSGIScriptAlias / "c:/juxin/test.wsgi"  test.wsgi文件是为了测试mod_wsgi是否工作的脚本(后面将会提到)

5) 这个时候就可以启动Apache来检测mod_wsgi是否完成(这里mod_wsgi必须跟Apache和python的版本匹配,如果不同,mod_wsgi启动会提示mod_wsgi.so找不到,我也遇到比较奇怪的问题,版本也匹配了,可就是Apache启动不成功,最后还是重启了机器才搞定,在这个过程中可以用test apache来测试是否有什么问题)

6) Apache能启动之后,可以用下面的test.wsgi脚本来测试mod_wsgi是否work,(注意必须更改apache_django_wsgi.conf中 WSGIScriptAlias 的wsgi脚本到test.wsgi,测试完成后改回django.wsgi,每次改动都需要重启Apache)

test.wsgi


def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

如果输出Hello World! 就表明wsgi配置ok啦

Tips:

整个过程中,多看logs/error.log,查找其中错误的原因

python2.6的bin目录和Django的bin目录最好放在系统的环境的PATH中

先预告一下,下面将要做一个Django集成Mako模板的网站,会做一些Django和Mako模板相关的总结。

 

 

错误总结:

 

1:

在启动项目时有这个错误'IOError: sys.stdout access restricted by mod_wsgi'是因为

 

A: A portable WSGI application or application component should not output anything to standard output. This is because some WSGI hosting mechanisms use standard output to communicate with the web server. If a WSGI application outputs anything to standard output it will thus potentially interleave with the response sent back to the client.
To promote portability of WSGI applications, mod_wsgi by default restricts direct use of 'sys.stdout' and 'sys.stdin'. Because the 'print' statement defaults to outputing text to 'sys.stdout', using 'print' for debugging purposes can cause this error.

 

意思就是项目中不能出现print打印语句,需要删除

 

2:

Internal Server Error

 

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, xiaochun006@126.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

 

 

这个问题是因为估计你的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.stdout = sys.stderr

sys.path.append(workspace)

#print workspace 

sys.path.append(workspace + "WWW ")

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

import django.core.handlers.wsgi

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

 

 

原创粉丝点击