最近由於項目上的須要開始大量使用nginx,所以也想趁機將之前經常使用的django+apache的架構換成django+nginx+fastcgi,此文是整個搭建的步驟,主要留做備忘,也但願對你們有所幫助。css
注意:雖然本文成功的搭建了django運行fastcgi的實例,可是在實際運行中發現了不少問題,好比程序執行異常,進程在每次請求以後退出之類的。多是我機器的問題,也多是程序自己bug,你們若是用來搭建外網環境,請務必多多測試。
python
一.編譯nginx
在網上買了一本《實戰nginx-取代Apache的高性能服務器》,寫的比較淺,主要是些配置方面的東西,不過卻正是目前我所須要的。因爲須要支持https和rewrite,因此除了nginx的源碼以外,又下載了 openssl-0.9.8r.tar.gz 和 pcre-8.12.tar.gz,把他們和nginx-1.0.4.tar.gz放到同一個目錄。
爲了方便編譯,筆者寫了一個腳本,代碼以下:nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 |
#!/bin/bash #============================================================================= #腳本所在絕對目錄 abs_path(){ local path=$1 local basename=$( basename $path ) local dirname=$( dirname $path ) cd $dirname if [ -h $basename ]; then path=$( readlink $basename ) abs_path $path else pwd fi } #============================================================================= #依賴的目錄 src_base_dir=$( abs_path $ ) src_openssl_dir=$src_base_dir'/openssl-0.9.8r' src_pcre_dir=$src_base_dir'/pcre-8.12' src_nginx_dir=$src_base_dir'/nginx-1.0.4' #============================================================================= #目標的目錄 dest_base_dir=$src_base_dir'/release' dest_nginx_dir=$dest_base_dir'/nginx' #============================================================================= #把全部的tar.gz解壓 find . -name "*.tar.gz" | xargs -IX tar zxvf X
#============================================================================= #編譯nginx cd $src_nginx_dir chmod u+x ./configure
./configure --with-http_stub_status_module --with-http_ssl_module --with-openssl=$src_openssl_dir --with-pcre=$src_pcre_dir --prefix=$dest_nginx_dir make && make install |
編譯完成後,咱們就須要來配置nginx了apache
二.配置nginx
在server配置項下增長django
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
location / { #fastcgi_pass 127.0.0.1:9001; fastcgi_pass unix:django.sock;
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_pass_header Authorization; fastcgi_intercept_errors off; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; }
location /admin_media/ { alias /usr/local/lib/python2.7/site-packages/django/contrib/admin/media/; break; }
location /site_media/ { alias /home/dantezhu/htdocs/ngx_django/media/; break; } |
這裏的3個location配置分別解決了,與python進程通訊、django後臺管理端樣式存放、網站樣式存放的問題。對照着apache的配置來看,就很容易明白了vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 |
WSGIPythonEggs /tmp<VirtualHost *> ServerName fuload.qq.com
WSGIScriptAlias / /home/dantezhu/htdocs/fuload/conf/setting.wsgi
<Directory /> Options FollowSymLinks
AllowOverride
Order allow,deny
Allow from all
</Directory> <Directory "/home/dantezhu/htdocs/fuload/mysite"> Order Deny,Allow
Deny from all
</Directory> Alias /admin_media "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media"
<Directory "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media"> Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
#AliasMatch /site_media/(.*\.(css|gif|png|jpg|jpeg)) /home/dantezhu/htdocs/fuload/media/$1
Alias /site_media /home/dantezhu/htdocs/fuload/media/
<Directory "/home/dantezhu/htdocs/fuload/media/"> Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory> </VirtualHost> |
三.安裝fastcgi依賴
須要到 http://trac.saddi.com/flup下載安裝,以後fastcgi纔可以正常啓動。bash
四.啓動django
建立django project的過程咱們就不說了,只列出啓動/中止的命令:
啓動:服務器
1
2 |
#python manage.py runfcgi daemonize=true pidfile=`pwd`/django.pid host=127.0.0.1 port=9001 maxrequests=1 & python manage.py runfcgi daemonize=true pidfile=`pwd`/django.pid socket=/home/dantezhu/nginx/sbin/django.sock maxrequests=1 & |
中止:架構
1 |
kill -9 `cat django.pid` |
五.啓動nginx
啓動:python2.7
1 |
./nginx -p /home/dantezhu/nginx/ |
中止:
1 |
kill -QUIT `cat ../logs/nginx.pid` |
從新載入配置:
1
2 |
./nginx -t -c `pwd`/../conf/nginx.confkill -HUP `cat ../logs/nginx.pid` |
成功顯示了django的後臺界面:
OK,到此爲止,大功告成!