かぴぶろぐ

またかぴったかと思った・・・(´A`;)

lighttpd + fcgi + Django環境構築メモ

カテゴリ[ django ]
■Lightttpdインストール
Lightttpdをwgetとかで最新版とってくる。
Lightttpdオフィシャル:http://www.lighttpd.net/
./configure --prefix=/opt/lighttpd-1.4
make
make install
■fcgi-2.4.0.tar.gz
fastcgi使うからfcgi-2.4.0を入れる
./configure
make
install
■python2.4
Django使うからpython2.4をインストールしとく

./configure --prefix=/opt/python2.4
make
make install

ln -s /opt/python2.4/bin/python /usr/local/bin/python24
ついでにJapanesecodecも。JapaneseCodecs-1.4.11
python24 setup.py install
■flupのインストール
Django + lighttpdを繋ぐのに必要?flupも入れる。flup-0.5
python24 serup.py install
■Lightttpd環境設定
Sourceからインストールするとconfファイルが出来ない場合があるので、
自前で作る。
mkdir /opt/lighttpd-1.4/conf
touch /opt/lighttpd-1.4/conf/lighttpd.conf
lighttpd.confの内容
# lighttpd configuration file
#
# use it as a base for lighttpd 1.0.0 and above
#
# $Id: lighttpd.conf,v 1.7 2004/11/03 22:26:05 weigon Exp $

############ Options you really have to take care of ####################

## modules to load
# at least mod_access and mod_accesslog should be loaded
# all other module should only be loaded if really neccesary
# - saves some time
# - saves memory
server.modules              = (
                                "mod_rewrite",
                                "mod_redirect",
                                "mod_alias",
                                "mod_access",
#                               "mod_cml",
#                               "mod_trigger_b4_dl",
#                               "mod_auth",
#                               "mod_status",
#                               "mod_setenv",
                                "mod_fastcgi",
#                               "mod_proxy",
#                               "mod_simple_vhost",
#                               "mod_evhost",
#                               "mod_userdir",
#                               "mod_cgi",
#                               "mod_compress",
#                               "mod_ssi",
#                               "mod_usertrack",
#                               "mod_expire",
#                               "mod_secdownload",
#                               "mod_rrdtool",
                                "mod_accesslog" )

## a static document-root, for virtual-hosting take look at the
## server.virtual-* options
server.document-root        = "/var/www/docroot/"

## where to send error-messages to
server.errorlog             = "/var/log/lighttpd/error.log"

# files to check for if .../ is requested
index-file.names            = ( "index.php", "index.html",
                                "index.htm", "default.htm" )

## set the event-handler (read the performance section in the manual)
# server.event-handler = "freebsd-kqueue" # needed on OS X

# mimetype mapping
mimetype.assign             = (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "application/ogg",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".cpp"          =>      "text/plain",
  ".log"          =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
  # default mime type
  ""              =>      "application/octet-stream",
 )

# Use the "Content-Type" extended attribute to obtain mime type if possible
#mimetype.use-xattr        = "enable"


## send a different Server: header
## be nice and keep it at lighttpd
# server.tag                 = "lighttpd"

#### accesslog module
accesslog.filename          = "/var/log/lighttpd/access.log"

## deny access the file-extensions
#
# ~    is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
#      of the document-root
url.access-deny             = ( "~", ".inc" )

$HTTP["url"] =~ "\.pdf$" {
  server.range-requests = "disable"
}

##
# which extensions should not be handle via static-file transfer
#
# .php, .pl, .fcgi are most often handled by mod_fastcgi or mod_cgi
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

######### Options that are good to be but not neccesary to be changed #######

## bind to port (default: 3000)
server.port                = 3111

## bind to localhost (default: all interfaces)
#server.bind                = "127.0.0.1"

## error-handler for status 404
#server.error-handler-404   = "/error-handler.html"
#server.error-handler-404   = "/error-handler.php"

## to help the rc.scripts
server.pid-file            = "/var/run/lighttpd.pid"


###### virtual hosts
##
##  If you want name-based virtual hosting add the next three settings and load
##  mod_simple_vhost
##
## document-root =
##   virtual-server-root + virtual-server-default-host + virtual-server-docroot
## or
##   virtual-server-root + http-host + virtual-server-docroot
##
#simple-vhost.server-root   = "/srv/www/vhosts/"
#simple-vhost.default-host  = "www.example.org"
#simple-vhost.document-root = "/htdocs/"


##
## Format: <errorfile-prefix><status-code>.html
## -> ..../status-404.html for 'File not found'
#server.errorfile-prefix    = "/usr/share/lighttpd/errors/status-"
#server.errorfile-prefix    = "/srv/www/errors/status-"

## virtual directory listings
#dir-listing.activate       = "enable"

## enable debugging
#debug.log-request-header   = "enable"
#debug.log-response-header  = "enable"
#debug.log-request-handling = "enable"
#debug.log-file-not-found   = "enable"

### only root can use these options
#
# chroot() to directory (default: no chroot() )
#server.chroot              = "/"

## change uid to <uid> (default: don't care)
#server.username            = "wwwrun"

## change uid to <uid> (default: don't care)
#server.groupname           = "wwwrun"

#### compress module
#compress.cache-dir         = "/var/cache/lighttpd/compress/"
#compress.filetype          = ("text/plain", "text/html")


fastcgi.server = (
    "/maint" => (
        "main" => (
            # Use host / port instead of socket for TCP fastcgi
            # "host" => "127.0.0.1",
            # "port" => 3033,
            "socket" => "/tmp/mysite.sock",
            "check-local" => "disable",
        )
    ),
)
alias.url = (
    "/media/" => "/opt/python2.4.2/lib/python2.4/site-packages/django/contrib/admin/media/",
)

url.rewrite-once = (
    "^(/media.*)$" => "$1",
    "^/favicon\.ico$" => "/media/favicon.ico",
    "^(/.*)$" => "/maint$1",
)
【重要なところ】
server.modules
...
                                "mod_rewrite",
                                "mod_redirect",
...
                                "mod_fastcgi",
                              
mod_rewrite、mod_fastcgiを有効にする。
server.document-root        = "/var/www/docroot/"
ドキュメントルートの設定。

server.errorlog             = "/var/log/lighttpd/error.log"
accesslog.filename          = "/var/log/lighttpd/access.log"
アクセスログ、エラーログの場所。(ディレクトリ予め作っとく事。)
fastcgi.server = (
    "/maint" => (
        "main" => (
            # Use host / port instead of socket for TCP fastcgi
            # "host" => "127.0.0.1",
            # "port" => 3033,
            "socket" => "/tmp/mysite.sock",
            "check-local" => "disable",
        )
    ),
)
Django環境起動時に必要。
alias.url = (
    "/media/" => "/opt/python2.4.2/lib/python2.4/site-packages/django/contrib/admin/media/",
)
Djangoのメディアファイル置き場。
url.rewrite-once = (
    "^(/media.*)$" => "$1",
    "^/favicon\.ico$" => "/media/favicon.ico",
    "^(/.*)$" => "/maint$1",
)
DjangoのURL設定。
■Lighttpdの起動スクリプト作成
lighttpd.shを作る。
BSDなら/usr/local/etc/rc.dに追加し、/etc/rc.dに登録する。
file : lighttpd.sh
#!/bin/sh
#
# $FreeBSD: ports/www/lighttpd/files/lighttpd.sh.in,v 1.3 2006/02/06 12:19:17 mnag Exp $
#
# PROVIDE: lighttpd
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable lighttpd:
#
# lighttpd_enable (bool):       Set it to "YES" to enable lighttpd
#                               Default is "NO".
# lighttpd_conf (path):         Set full path to config file.
#                               Default is "/usr/local/etc/lighttpd.conf".
#

. /etc/rc.subr

name="lighttpd"
rcvar=`set_rcvar`

load_rc_config $name

: ${lighttpd_enable="NO"}
: ${lighttpd_conf="/opt/lighttpd-1.4/conf/lighttpd.conf"}

command=/opt/lighttpd-1.4/sbin/lighttpd
command_args="-f ${lighttpd_conf}"
pidfile=/var/run/lighttpd.pid
required_files=${lighttpd_conf}
stop_postcmd=stop_postcmd
restart_precmd="checkconfig"

checkconfig()
{
        echo "Performing sanity check on ${name} configuration:"
        eval "${command} ${command_args} -t"
}

stop_postcmd()
{
        rm -f ${pidfile}
}

run_rc_command "$1"
■Djangoの起動スクリプトを作成
file : django.sh
#!/bin/sh
#
# $FreeBSD: ports/www/lighttpd/files/lighttpd.sh.in,v 1.3 2006/02/06 12:19:17 mnag Exp $
#
# PROVIDE: lighttpd
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable lighttpd:
#
# lighttpd_enable (bool):       Set it to "YES" to enable lighttpd
#                               Default is "NO".
# lighttpd_conf (path):         Set full path to config file.
#                               Default is "/usr/local/etc/lighttpd.conf".
#

. /etc/rc.subr

name="django"
rcvar=`set_rcvar`

load_rc_config $name

: ${django_enable="NO"}

command_interpreter="/usr/local/bin/python24"
command="/var/www/django/site_manager/manage.py"
pidfile="/var/run/django.pid"
command_args="runfcgi method=prefork socket=/tmp/mysite.sock pidfile=${pidfile}"
stop_postcmd=stop_postcmd

stop_postcmd()
{
        rm -f ${pidfile}
}

run_rc_command "$1"
■Djangoの配置
作成したDjango Projectを設置する。
例> 
cd /var/www/django/site_manager  ←Projectのルート
python24 manager.py syncdb
■それぞれ起動
/usr/local/etc/rc.d/lighttpd.sh start
/usr/local/etc/rc.d/django.sh start
■トラブルシューティング
djangoのpythonのみkillしたい場合。
kill -INT `cat /var/run/django.pid`
を実行

■参考URL

http://kapi.jp/kapi_blog/82

2007年11月20日

関連カテゴリ Python django

この記事のコメント

この記事にコメントする