轉:Apache+Fastcgi+Django

來源:互聯網
上載者:User

標籤:安裝   apach   檔案   dir   .com   web   isa   class   favicon   

07年作者就貼出的文章了,可見多麼古老的運行方式還在用。

轉:http://www.cnblogs.com/RChen/archive/2007/03/23/django_fcgi.html

首先要安裝 flup (http://www.saddi.com/software/flup/),
這是 Python 處理 FastCGI 的庫。

FactCGI 採用 c/s 模型,獨立的運行一個進程。在需要處理請求時,網頁伺服器(apache, httpd,..)直接和 FactCGI 進程進行通訊即可。

網頁伺服器可通過兩種辦法和 FastCGI server 串連:
1. Unix domain socket(或 win32 的“具名管道")
2. TCP socket
通常 TCP socket 更簡單,因為許可權問題比較好配置。

如何啟動 FactCGI 伺服器:
到項目目錄中,執行:
./manage.py runfcgi [options]
如果要看協助:
./manage.py runfcgi help

在選項中,需要指定一個 socket 或 host 和 port 的組合,這樣的話,當你啟動 web server 時,就可以通過這些資訊來串連到 FactCGI 伺服器。

例子

在 TCP 通訊埠中運行一個線程內伺服器:
./manage.py runfcgi method=threaded host=127.0.0.1 port=3033

在 Unix domain socket 上運行一個 preforked 伺服器:
./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pid

不作為後台進程執行(便於調試):
./manage.py runfcgi daemonize=false socket=/tmp/mysite.sock

如何停止背景 FastCGI 進程:
1. 如果指定了 pidfile 屬性,則可以這樣:kill `cat $PIDFILE`。其中 $PIDFILE 是你指定的 pidfile 選項。

要重啟 Unix 上的後台 FactCGI 進程,可執行下列 shell 指令碼:

#!/bin/bash

# Replace these three settings.
PROJDIR="/home/user/myproject"
PIDFILE="$PROJDIR/mysite.pid"
SOCKET="$PROJDIR/mysite.sock"

cd $PROJDIR
if [ -f $PIDFILE ]; then
    kill `cat -- $PIDFILE`
    rm -f -- $PIDFILE
fi

exec /usr/bin/env - \
  PYTHONPATH="../python:.." \
  ./manage.py runfcgi socket=$SOCKET pidfile=$PIDFILE


Apache 的設定:
1. 需要安裝了 mod_fastcgi
2. 編輯 httpd.conf:
   (1) 用 FastCGIExternalServer 指向 FastCGI 伺服器的位置
 可以用 socket 也可以用主機+連接埠的方式指定。例子:

 # Connect to FastCGI via a socket / named pipe.
 FastCGIExternalServer /home/user/public_html/mysite.fcgi -socket /home/user/mysite.sock

 # Connect to FastCGI via a TCP host/port.
 FastCGIExternalServer /home/user/public_html/mysite.fcgi -host 127.0.0.1:3033

 不管上述那種情況,/home/user/public_html/mysite.fcgi 這個檔案是不需要存在的。其作用指示作為一個 URL 指示 Web 服務器哪些請求需要被 FastCGI 來處理。

   (2) 用 mod_rewrite 來指派需要處理的 URL 到 FastCGI.

   例子:
 <VirtualHost 12.34.56.78>
   ServerName example.com
   DocumentRoot /home/user/public_html
   Alias /media /home/user/python/django/contrib/admin/media
   RewriteEngine On
   RewriteRule ^/(media.*)$ /$1 [QSA,L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^/(.*)$ /mysite.fcgi/$1 [QSA,L]
 </VirtualHost>
 
 這裡指示的重寫後的 url 就是上面 FastCGIExternalServer 中那個。
 上述配置將不是 /media/ 開頭的,或者指向不存在的檔案的請求,轉給了 FastCGI.

lighttpd 配置

lighttpd 天然的支援 FastCGI.
首先要確認你的 mod_fastcgi 在模組列表中,並且在 mod_rewrite 和 mod_access 後,在 mod_accesslog 前。
為了服務 admin media, 也許你還需要 mod_alias.
在 lighttpd 的設定檔中加如下一段:

server.document-root = "/home/user/public_html"
fastcgi.server = (
    "/mysite.fcgi" => (
        "main" => (
            # Use host / port instead of socket for TCP fastcgi
            # "host" => "127.0.0.1",
            # "port" => 3033,
            "socket" => "/home/user/mysite.sock",
            "check-local" => "disable",
        )
    ),
)
alias.url = (
    "/media/" => "/home/user/django/contrib/admin/media/",
)

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

還可以在一個 lighttp 伺服器上跑多個 Django. 只要為每一個應用指定一個獨立的 FastCGI host.

如何在共用的 web hosting 上運行 Django / Apache:

編輯 .htaccess:
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]

再寫一個 mysite.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‘] = "myproject.settings"

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

在更新代碼後,可以通過重新上傳 mysite.fcgi 檔案來指示 Apache 重啟 Django 程式。
如果有 shell 許可權,可以直接用 touch 命令改變時間戳記:
touch mysite.fcgi

轉:Apache+Fastcgi+Django

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.