nginx+uWSGI
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.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:
[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
nginx+fastcgi
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
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