基本的架构如下:

  • Django 使用 flup 提供一个 WSGI 接口的服务
  • lighttpd 使用 FastCGI 来访问 flup 提供的 WSGI 接口,然后在 http 层面为访问者服务
  • http-lighttpd-FastCGI-WSGI-flup-Django

需要安装的程序有:

  • python, django
  • flup 一个 WSGI的实现
  • lighttpd

目录部署结构

/home
....+/yourWebName
........+yourWebName.pid
........+yourWebName.sock
........+/yourDjangoName
............+settingsYourWebName.py #适用于正式部署的网站的配置文件,一般情况下是先 import settings.py 然后在根据网站实际部署情况修改其中的配置。如数据库环境等等
............+yourWebName.fcgi
........+/lighttpd
............+11-simple-vhost-yourWebName.conf
........+/scripts
............+StartFastCGI.sh
............+RestartFastCGI.sh

其中:
yourWebName 是给网站定义的名称
yourDjangoName 是 Django 项目的名称
/home/yourWebName 是将网站项目的根目录,与之相关的都放在这个目录下面
/home/yourWebName/yourDjangoName 是网站所用到的 Django 系统的项目根目录

lighttpd 配置文件 /etc/lighttpd/conf-enabled/11-simple-vhost-yourWebName.conf

$HTTP["host"] == "www.yourWebName.com" {
    server.document-root = "/home/yourWebName/yourDjangoName/"
    fastcgi.server = (
        "/yourWebName.fcgi" => (
            "main" => (
                "socket" => "/home/yourWebName/yourWebName.sock",
                "check-local" => "disable",
            )
        ),
    )

    alias.url = (
        "/media" => "/home/user/django/contrib/admin/media",
        "/static" => "/home/yourWebName/yourDjangoName/static",
    )

    url.rewrite-once = (
        "^(/media.*)$" => "$1",
        "^(/static.*)$" => "$1",
        "^/favicon\.ico$" => "/static/favicon.ico",
        "^(/.*)$" => "/yourWebName.fcgi$1",
    )

    accesslog.filename = "/var/log/lighttpd/www.yourWebName.com.access.log"
}

Django FastCGI 配置文件 yourWebName.fcgi

# !/usr/bin/python

import sys, os

# Add a custom Python path
# sys.path.insert(0, "/home/user/python")
# Switch to the directory of your project. (Optional.)
# os.chdir("/home/user/myproject")
# Set the DJANGO_SETTINGS_MODULE environment variable
os.environ['DJANGO_SETTINGS_MODULE'] = "yourDjangoName.settingsYourWebName"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

启动 Django 服务脚本 StartFastCGI.sh

# !/bin/bash

# Replace these four settings
PROJDIR="/home/yourWebName"
DJANDIR="$PROJDIR"
PIDFILE="$PROJDIR/yourWebName.pid"
SOCKET="$PROJDIR/yourWebName.sock"

# start program
cd $DJANDIR
python manage.py runfcgi method=threaded socket=$SOCKET pidfile=$PIDFILE --settings=yourDjangoName.settingsYourWebName
chmod 777 $SOCKET

重启 Django 服务的脚本 RestartFastCGI.sh

# !/bin/bash

# Replace these three settings
PROJDIR="/yourWebName"
PIDFILE="$PROJDIR/yourWebName.pid"
SOCKET="$PROJDIR/yourWebName.sock"

# kill program and remove pidfile #杀死旧的进程
cd $PROJDIR
if [ -f $PIDFILE ]; then
    kill `cat -- $PIDFILE`
    rm -f -- $PIDFILE
fi

# upgrade program #更新代码
cd $PROJDIR
hg pull
hg update

# start program #重启 Django 服务
$PROJDIR/scripts/StartFastCGI.sh

settingsYourWebName.py

# 避免在 lighttp-fastcgi 环境下 {% url %} 被加上 yourWebName.fcgi 的 url 前缀
FORCE_SCRIPT_NAME = ''

注:

  • 如果只是修改了网站的代码,只需要重启 Django 服务即可
  • 当 lighttpd 相关的配置文件发生了变化才需要重启 lighttpd

UPDATE:

  • 20130825 支持 Django V1.5 环境