這次嘗試新的配置關於Lighttpd + Django + Fastcgi,為何會想要這麼做呢??
由於每次更改專案 web的版面、Layout 就改了「一偷拉庫」,說實在的效率大打折扣
看了網路上幾篇文章的分享,決定嘗試這樣的架構「Lighttpd + Django + Fastcgi」,在這裡做個紀錄
實作環境:
- Ubuntu 10.04
- Lighttpd 1.4.26
- Flup 1.0.2
- Python 2.6
- Django 1.4
# Note 在執行下列動作之前,我已經切換成sudo模式,所以下列動作不加入sudo指令
1.到官網下載或是用svn
一. svn co http://code.djangoproject.com/svn/django/trunk /opt/django (藍色部分是我放置django目錄位置)
二. python setup.py install
或者
一. 下載Django-1.4.tar.gz
二. tar xzvf Django-1.4.tar.gz
三. cd Django-1.4 sudo
四. python setup.py install
安裝完畢之後,到你要配置的web folder底下,這裡舉例 /opt
2. cd /opt
3. django-admin.py startproject myfirst
# 這裡可能會產生 django-admin.py no such file or directory.... 暫解方法: 你可以在 /opt/djagno/django/bin/ 底下找到 django-admin.py
/opt/django/django/bin/django-admin.py startproject myfirst
4. cd myfirst
5. chmod +x manage.py
6. python manage.py runserver
|
0 errors found Django version 1.5, using settings 'myfirst.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C. ^Croot@ubuntu:/opt/myfirst# python manage.py runserver Validating models...
|
如果看到 0 errors found 代表就是執行成功
1. 先確定你的python版本,這裡是用python 2.6
2. tar xzvf flup-1.0.2.tar.gz
3. cd flup-1.0.2
4. python setup.py install
- 加入第一隻myfirst.fcgi ,此目的告訴 server 如何處理fastcgi 程式
|
#!/usr/bin/python import sys, os
# Add a custom Python path. sys.path.insert(0, "/usr/bin/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'] = "myfirst.settings"
from django.core.servers.fastcgi import runfastcgi runfastcgi(["method=threaded", "daemonize=false"])
|
#注意紅色的部分,這個跟官網上面的會有出入,如果照著官網好像會有錯誤
然後執行python myfirst.fcgi,如果有看到<h1> It worked!</h1> 就代表正確無誤
|
$SERVER["socket"] == "192.168.81.79:8000" { server.document-root = "/opt/myfirst"
fastcgi.server = ( "/myfirst.fcgi" => ( "main" => ( # Use host / port instead of socket for TCP fastcgi "socket" => "/opt/myfirst/django.sock", "check-local" => "disable", ) ), ) alias.url = ( "/media" => "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/", )
url.rewrite-once = ( "^(/media.*)$" => "$1", "^/favicon\.ico$" => "/media/favicon.ico", "^(/.*)$" => "/myfirst.fcgi$1", ) }
|
#Note:紅色的比較需要注意一下
|
#!/bin/bash
PROJDIR="/opt/myfirst" PIDFILE="$PROJDIR/django.pid" SOCKET="$PROJDIR/django.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=$PIDFIL
|
#Note:django.pid 或是 django.sock ,名稱可以更改
建立完成之後記得 chmod +x runfastcgi
/etc/init.d/lighttpd reload
./runfastcgi
#如果出現503錯誤,去找Lighttpd 的error.log 發現出現以下的訊息
|
2012-04-16 09:09:13: (mod_fastcgi.c.1734) connect failed: Permission denied on unix:/opt/myfirst/django.sock 2012-04-16 09:09:13: (mod_fastcgi.c.3037) backend died; we'll disable it for 1 seconds and send the request to another backend instead: reconnects: 0 load: 1 2012-04-16 09:09:15: (mod_fastcgi.c.2864) fcgi-server re-enabled: unix:/opt/myfirst/django.sock
|
很顯然的是沒有執行的權限,在去我們/opt/myfirst目錄底下,去更改權限就可以執行了
[Reference]
請先 登入 以發表留言。