2008/04/08

Django开发及mod_python安装

python+Django+apache+mysql 开发网站,different from LAMP which mainly uses PHP

也许大伙都听说过 Ruby on Rails? 一个以 ruby 写的 RAD framework; django 与它大约同期诞生, 两者有着相近的目标, 各自的哲学. (有意思的是: RoR 和 django 的作者之前都是 php 开发方面的高手!)

django 现在官方站点上发布的版本是 0.91, 但是这不过这只是对外的一个静态的小版本, 而其 svn 中每天都有更新, 如果要学习 django 的话, 还是建议从它的 svn 中直接取出最新的版本. 尤其是 magic-removal 分支 - 这将会成为 django 下一个版本: 0.92.

magic-removal 是 django 开发人员们目前正在全力耕作的一个分支 (branch), 最大的目标就是将 django 多年遗留下来的一些疙疙瘩瘩的地方做一次 "大扫除", 以期提升 django 这一开发框架的简洁性与易用性.

如何安装
http://www.djangoproject.com/documentation/install/
简单说,下载version 0.96,解压,然后执行
sudo python setup.py install
如果用于开发,最好之前安装mod_python, which embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements. Make sure you have Apache installed, with the mod_python module activated. Django requires Apache 2.x and mod_python 3.x.

但是据说mod_python和 mod_php冲突,所以现在还在犹豫是否安装Django。本机上原来装有bbPress based on LAMP(php).

Django 中文文档
http://www.yarshure.com/documentation/

Django step by step 中文
http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/


Ubuntu下mod_python的安装

目的: 为了能在网页中嵌入python代码
环境: Ubuntu, apache2, python2.5, 并且已安装 apache2-php5 (安装完后php依旧工作,哈哈)

步骤:
首先安装 mod_python .
sudo apt-get install libapache2-mod-python, 或用synaptic装亦可

(按mod_python的说明步骤要安装apxs,但是apache2找不到apxs,因为这种方式已过时了。apache将所有的载入的包都放在
/etc/apache2/mods-enabled 即可加载! 注: apache2的 ServerRoot = /etc/apache2 )

然后检查是否已在 mods-enabled目录下, 如有,则不用执行下面命令:
cd /etc/apache2/mods-enabled/
sudo ln -s ../mods-available/mod_python.load mod_python.load

接着,编辑:
cd /etc/apache2/sites-available/
sudo gedit default

On line 10 you should have:

Options Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/

加入新行后变为:

Options Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
allow from all

AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/

(注: 该文件被链接到 /etc/apache2/sites-enabled/000-default 。)

现在重启 apache server:
sudo /etc/init.d/apache2 restart

测试:
sudo gedit /var/www/test.py
文件内容如下:
def index(req):
return "Test successful";

访问 http://localhost/test.py 应该可以看到 "Test successful" in plain text。


一个显示网页的python程序

import sys
import time

def index(req):

# Following line causes error to be sent to browser
# rather than to log file (great for debug!)

sys.stderr = sys.stdout

#print "Content-type: text/html\n"

#print """
blah1 = """<html>
<head><title>A page from Python</title></head>
<body>
<h4>This page is generated by a Python script!</h4>
The current date and time is """

now = time.gmtime()
displaytime = time.strftime("%A %d %B %Y, %X",now)

#print displaytime,
blah1 += displaytime

#print """
blah1 += """
<hr>
Well House Consultants demonstration
</body>
</html>
"""
return blah1

没有评论: