ubuntu下先安裝下C編譯器和Python環境:css
sudo apt-get install build-essential python-dev
使用pip安裝uWSGI:html
pip install uwsgi
nginx配置:python
能夠單獨爲站點設置一個配置文件:nginx
sudo vim /etc/nginx/sites-enabled/mysite
或者直接在nginx.conf中設置:django
sudo vim /etc/nginx/nginx.conf
設置:ubuntu
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中的配置覆蓋掉了,最好將其註釋掉。vim
而後設置uWSGI,創建文件myfile.ini:瀏覽器
[uwsgi] socket = 127.0.0.1:8080 #與nginx配置中的uwsgi_pass相同 chdir = /home/user/mysite/ #項目地址 wsgi-file = mysite/wsgi.py processes = 4 threads = 2 pidfile=/tmp/project-master.pid stats = 127.0.0.1:9191 virtualenv = <path to env> #virtualenv
其中wsgi-file是django(1.4版本以上)項目自動創建的文件,裏面內容爲:緩存
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "weixian.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
若是django版本太低無此文件的話,能夠本身創建,或者在myfile.ini中設置env,module,pythonpath:bash
[uwsgi] socket = 127.0.0.1:8080 chdir = /home/user/mysite/ pythonpath = .. env = DJANGO_SETTINGS_MODULE=mysite.settings module = django.core.handlers.wsgi:WSGIHandler() processes = 4 threads = 2 stats = 127.0.0.1:9191
virtualenv = <path to env>
按配置重啓nginx:
/usr/sbin/nginx -s reload
或者:
killall -9 nginx nginx -c /etc/nginx/nginx.conf
啓動uWSGI:
uwsgi myfile.ini
重啓uWSGI:
# using kill to send the signal kill -HUP `cat /tmp/project-master.pid` # or the convenience option --reload uwsgi --reload /tmp/project-master.pid # or if uwsgi was started with touch-reload=/tmp/somefile touch /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.gz tar zxvf flup-1.0.2.tar.gz cd flup-1.0.2 python 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/bash PROJDIR="/home/user/myproject" PIDFILE="$PROJDIR/mysite.pid" cd $PROJDIR if [ -f $PIDFILE ]; then kill `cat -- $PIDFILE` rm -f -- $PIDFILE fi exec python manage.py runfcgi host=127.0.0.1 port=8080 method=prefork pidfile=$PIDFILE --settings=mysite.settings