Ubuntu下先安裝下C編譯器和Python環境:
sudo apt-get install build-essential python-dev
使用pip安裝uWSGI:
pip install uwsgi
nginx配置:
可以單獨為站點設置一個配置文件:
sudo vim /etc/nginx/sites-enabled/mysite
或者直接在nginx.conf中設置:
sudo vim /etc/nginx/nginx.conf
設置:
server {    listen   80; ## listen for ipv4; this line is default and implied    #listen   [::]:80 default ipv6only=on; ## listen for ipv6     server_name 站點域名;    location / {            uwsgi_pass 127.0.0.1:8080;            include uwsgi_params;    }    #設置該網站應用中所需要的靜態文件的根目錄,需要將admin和用到的第三方庫像restframework的靜態文件都放到此目錄中     location ~/static/ {            root   /home/user/mysite/;  #項目地址           # root  html;           # index  index.html index.htm;            break;        }        #設置媒體文件的根目錄       location ~/media/ {            root   /home/user/mysite/;           # root  html;           # index  index.html index.htm;            break;        }}自己電腦上搭建localhost服務器時,注意別被/etc/nginx/sites-enabled/default中的配置覆蓋掉了,最好將其注釋掉。
然后設置uWSGI,建立文件myfile.ini:
[uwsgi]socket = 127.0.0.1:8080 #與nginx配置中的uwsgi_pass相同chdir = /home/user/mysite/ #項目地址wsgi-file = mysite/wsgi.pyPRocesses = 4threads = 2pidfile=/tmp/project-master.pidstats = 127.0.0.1:9191
其中wsgi-file是django(1.4版本以上)項目自動建立的文件,里面內容為:
import osos.environ.setdefault("DJANGO_SETTINGS_MODULE", "weixian.settings")from django.core.wsgi import get_wsgi_applicationapplication = get_wsgi_application()如果django版本過低無此文件的話,可以自己建立,或者在myfile.ini中設置env,module,pythonpath:
[uwsgi]socket = 127.0.0.1:8080chdir = /home/user/mysite/pythonpath = ..env = DJANGO_SETTINGS_MODULE=mysite.settingsmodule = django.core.handlers.wsgi:WSGIHandler()processes = 4threads = 2stats = 127.0.0.1:9191
按配置重啟nginx:
/usr/sbin/nginx -s reload
或者:
killall -9 nginxnginx -c /etc/nginx/nginx.conf
啟動uWSGI:
uwsgi myfile.ini
重啟uWSGI:
# using kill to send the signalkill -HUP `cat /tmp/project-master.pid`# or the convenience option --reloaduwsgi --reload /tmp/project-master.pid# or if uwsgi was started with touch-reload=/tmp/somefiletouch /tmp/somefile
ini文件也可以用xml文件來設置。
添加xml支持:
sudo apt-get install libxml2-dev
配置myfile.xml
<uwsgi> <socket>127.0.0.1:8080</socket> <chdir>/home/user/mysite/</chdir> <module>mysite/wsgi</module></uwsgi>
啟動:
uwsgi -x myfile.xml
fastcgi需要安裝flup:
wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gztar zxvf flup-1.0.2.tar.gzcd flup-1.0.2python setup.py install
或者:
sudo apt-get install python-flup
nginx配置:
server {        listen       80;        server_name  localhost;        #設置該網站應用中所需要的靜態文件的根目錄        location ~/static/ {            root   /home/user/mysite/;           # root  html;           # index  index.html index.htm;            break;        }        #設置媒體的根目錄        location ~/media/ {            root   /home/user/mysite/;           # root  html;           # index  index.html index.htm;            break;        }        #host and port to fastcgi server         location / {        fastcgi_pass 127.0.0.1:8080;        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 pass_header Authorization;        fastcgi_intercept_errors off;         }        #設置瀏覽器緩存這些圖片格式文件瀏覽器緩存時間是30天,CSS/js緩存時間1小時        location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$        {            expires 30d;        }        location ~ .*/.(js|css)?$        {            expires 1h;        }}重啟nginx,然后啟動fcgi:
python manage.py runfcgi host=127.0.0.1 port=8080 method=prefork --settings=mysite.settings #采用靜態進程池的話性能會比動態線程生成高2倍左右
ok。
要更新django項目的話,
ps -ef |grep fcgi
找出主進程號kill掉,再重啟fcgi即可。
也可以寫個重啟腳本。使用pidfile選項將fcgi的pid存儲到文件中,重啟時讀取kill掉。
#!/bin/bashPROJDIR="/home/user/myproject"PIDFILE="$PROJDIR/mysite.pid"cd $PROJDIRif [ -f $PIDFILE ]; then kill `cat -- $PIDFILE` rm -f -- $PIDFILEfiexec python manage.py runfcgi host=127.0.0.1 port=8080 method=prefork pidfile=$PIDFILE --settings=mysite.settings
 
  | 
新聞熱點
疑難解答