笑傲江湖

My Life and My Way...
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Django之Apache/mod_python安装及HelloWorld

Posted on 2011-01-22 02:14  Simon.Wong  阅读(363)  评论(0编辑  收藏  举报

Django自带的服务器不是很好,改成Apache+mod_python的方式写一个HelloWorld

一、安装Apache

下载地址: http://httpd.apache.org/

apache_2.2.3-win32-x86-no_ssl.msi安装很方便,注意安装过程中要填email,否则启动报错

二、安装mod_python

下载地址:http://www.modpython.org/

mod_python-3.3.0b.win32-py2.4-Apache2.2.exe点击安装即可,注意python用的是2.4的

三、配置虚拟主机

编辑httpd.conf:

设置MaxRequestsPerChild 1,这样可以在开发阶段不用重启Apache进行测试

添加LoadModule python_module modules/mod_python.so

去掉注释Include conf/extra/httpd-vhosts.conf

假如工作目录为D:\py,cmd切换到该目录运行“django-admin.py startproject myproj

编辑httpd-vhosts.conf:

NameVirtualHost 127.0.0.1:80

<VirtualHost 127.0.0.1:80>
    <Location "/">
        SetHandler python-program
        PythonPath "['D:/py'] + sys.path"
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE myproj.settings
        PythonAutoReload Off
        PythonDebug On
    </Location>
</VirtualHost>

在D:\py\myproj下新建helloworld.py:

from django.http import HttpResponse

def index(request):
    return HttpResponse('Hello, Django!')

修改urls.py:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    # Example:
    # (r'^myproj/', include('myproj.apps.foo.urls.foo')),
    (r'^$', 'myproj.helloworld.index'),

    # Uncomment this for admin:
#     (r'^admin/', include('django.contrib.admin.urls')),
)

OK,启动Apache访问http://localhost