django 是一個開放源代碼的web應用架構,由python寫成,主力開發者是adrian holovaty。它采用了mvc的設計模式。
django 的名字來自于比利時的爵士吉他手django reinhardt,他是歐陸爵士樂發展的創始者,也是爵士史上最偉大的吉他巨匠,中國樂迷稱之為“三指琴魔”。
django 源自一個線上新聞 web 站點,于 2005 年7月在bsd許可證下釋出。django 架構的核心元件有:
用于建立模型的對象關系映射
為最終使用者設計的管理界面
一流的 url 設計
設計者友好的模闆語言
緩存系統
django+apache+mod_python 的用法
為何要切換到 mod_wsgi ?
mod_python 其實也不差。
但我一直困惑于 apache+mod_python 的莫名頻繁崩潰,錯誤日志中會出現大量的如下錯誤:
[error] [client x.x.x.x] oserror: [errno 0] error [notice] parent: child process exited with status 3221225477 -- restarting.
這個狀态号 3221225477 基本沒什麼意義,再加上 oserror 0 ,這個異常日志讓人很無奈。甚至在 windows 平台下,apache 最惡劣情況下竟然會彈出一個崩潰錯誤框,此時事件日志會報告:
cation popup:0:29722:26:eventlog_information_type:彈出應用程式: microsoft visual c++ runtime library: runtime error! program: c:\apache2.2\bin\httpd.exe this application has requested the runtime to terminate it in an unusual way.
一直無法對症下藥。于是,期冀 mod_wsgi 能讓我擺脫這兩個問題。
另一個就是性能。
mechanism requests/sec
mod_cgi (scriptalias) 10
mod_python (pythonhandler) 400
mod_wsgi (wsgidaemonprocess) 700
mod_wsgi (.htaccess/sethandler) 850
mod_wsgi (wsgiscriptalias) 900
切換到 mod_wsgi 很容易
1、下載下傳
2、放置
把 mod_wsgi 放到你的apache安裝目錄下的 modules 檔案夾内。
3、配置apache
在apache配置檔案httpd.conf中,增加一行:
4、修改virtual host配置
mod_python 下的 virtualhost 配置主要複雜在 python 配置這塊,如下所示:
<virtualhost *:80>
servername rt.ju690.com
serveralias rt.ju690.cn
documentroot d:/socialrecommend
<location "/">
sethandler python-program
pythonpath "['d:/socialrecommend']+sys.path"
pythonhandler django.core.handlers.modpython
setenv django_settings_module settings_yourproject
pythonautoreload off
pythondebug off
</location>
alias /static d:/socialrecommend/static
<location "/static">
sethandler none
</location>
<directory "d:/socialrecommend/static">
order deny,allow
allow from all
</directory>
</virtualhost>
現在改為 mod_wsgi ,隻需要稍加改變即可:
documentroot d:/socialrecommend
wsgiscriptalias / d:\socialrecommend\wsgi\yourproject.wsgi
</directory>
<directory "d:/socialrecommend/wsgi">
賦予本地wsgi檔案夾allow from all權限,是因為這樣才能執行wsgi script。
5、建立wsgi script
在你的web site django總目錄下建立一個檔案夾wsgi。
在 wsgi 檔案夾下建立一個檔案 yourproject.wsgi ,内容如下所示:
# complete_project.wsgi is configured to live in projects/complete_project/deploy.
# if you move this file you need to reconfigure the paths below.
import os
import sys
# redirect sys.stdout to sys.stderr for bad libraries like geopy that uses
# print statements for optional import exceptions.
sys.stdout = sys.stderr
from os.path import abspath, dirname, join
from site import addsitedir
from django.core.handlers.wsgi import wsgihandler
sys.path.insert(0, abspath(join(dirname(__file__), "../")))
sys.path.insert(0, abspath(join(dirname(__file__), ". . /. . /")))
os.environ["django_settings_module"] = "yourprojectname.settings_yourproject" #你的settings module名
application = wsgihandler()
6、重新開機aapche即可。
參考資源: