以前寫了一個簡易的運維管理系統,奈何一直都是用的開發者模式啓動django,現想用ngxin代理,參照<<The Django Book>>,上面提供了Apache+mod_python(mod_wsgi|FastCGI)等方式,而我選擇了Nginx+FastCGI的方式(機器上原本就有nginx了,而且我平時用nginx也比較多).css
Django經過FastCGI啓動的方式有以下幾種:html
在tcp端口上運行一個線程服務器:
python
./manage.py runfcgi method=threaded host=127.0.0.1 port=3033jquery
在Unix socket 上運行prefork 服務器:nginx
./manage.py runfcgi method=prefork socket=/home/user/mysite.sock pidfile=django.pidsql
啓動,但不做爲後臺進程(在調試時比較方便):django
./manage.py runfcgi daemonize=false socket=/tmp/mysite.sockbootstrap
Django的目錄結構以下:centos
[root@VM_56_231_centos tencent]# tree tencent/ tencent/ ├── db.sqlite3 ├── log │ └── all.log ├── manage.py ├── scripts │ ├── changetime.sh │ ├── test.sh │ └── testtime.sh ├── static │ ├── css │ │ ├── bootstrap.css .... │ │ └── bootstrap-responsive.min.css │ ├── img │ │ ├── glyphicons-halflings.png │ │ └── glyphicons-halflings-white.png │ ├── jquery │ │ └── jquery-1.8.3.min.js │ └── js │ ├── bootstrap-datetimepicker.js .... ├── tencent │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── tencentapp ├── admin.py ├── __init__.py ├── models.py ├── template │ ├── bb.html .... │ └── ee.html ├── tests.py └── views.py
static是放置靜態文件的目錄
服務器
Nginx的配置文件以下:
server {
listen 9007;
server_name default;
index index.html index.htm;
location / {
fastcgi_pass 127.0.0.1:9008; #這是是Django啓動時的套接字地址,啓動方式可參照上文
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
location ~ ^/static/ {
root /data/python/tencent/tencent; #這是Django靜態文件的目錄
expires 24h;
}
}