我的环境
os linux deepin 11.12(ubuntu 11.10)
python 2.7.2+
nginx 1.0.5
django 1.3.1
uwsgi 1.0
1.python 2.7.2 nginx 1.0.5安装忽略
apt-get install nginx 最新版本中已经带了 uwsgi模块,通过nginx -V可以查看nginx 配置模块
2.安装uwsgi
wget http://projects.unbit.it/downloads/uwsgi-1.0.tar.gz
tar xzf uwsgi-1.0.tar.gz
cd uwsgi-1.0
make -f Makefile.Py27(指定你python的版本,如果你的python是3.0 就应该是 make -f Makefile.Py3.0)
cp uwsgi /usr/sbin/uwsgi
3.修改 nginx 配置
location / {
include uwsgi_params;
#uwsgi_pass 127.0.0.1:9090; # http 方式
uwsgi_pass unix:///tmp/test_pootle.sock; # socket 方式
}
4.启动 django 应用
我的pootle下下载时就自带栏wsgi.py模块不需要重新创建,直接就可以用了,如果没有在copy一份内容如下
import os,sys
if not os.path.dirname(__file__) in sys.path[:1]:
sys.path.insert(0, os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = ‘settings’
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
1) 命令参数启动 sudo uwsgi -s /tmp/test_pootle.sock -w wsgi -M -p 4
2) 将启动参数写入xml文件 xml启动.(需要 sudo apt-get install libxml2-dev 支持)
在app 中创建 pootle.xml文件内容如下
<uwsgi>
<socket>/tmp/test_pootle.sock</socket>
<pidfile>/var/www/pootle/log/pootle.pid</pidfile>
<daemonize>/var/www/pootle/log/pootle.log</daemonize>
<chmod-socket/>
<chdir>/var/www/pootle</chdir>
<pythonpath>..</pythonpath>
<master/>
<module>wsgi</module>
<buffer-size>32768</buffer-size>
</uwsgi>
or
<uwsgi>
<socket>127.0.0.1:9090</socket>
<pidfile>/var/www/pootle/log/pootle.pid</pidfile>
<daemonize>/var/www/pootle/log/pootle.log</daemonize>
<chdir>/var/www/pootle</chdir>
<pythonpath>..</pythonpath>
<master/>
<module>wsgi</module>
<buffer-size>32768</buffer-size>
</uwsgi>
-C(chmod-socket) 表示将 /tmp/test_pootle.sock 文件权限改成 666 以便 nginx 可以读取
pidfile 保存进程pid的文件路进,方便后面管理重启,停止uwsgi
chdir path to your django project directory
daemonize 守护进程 后台运行
sudo uwsgi -x pootle.xml
更多参数配置,uwsgi –help 或者见 http://projects.unbit.it/uwsgi/wiki/Doc http://projects.unbit.it/uwsgi/wiki/Example
http://ichuan.net/post/6/using-uwsgi-instead-of-fastcgi-for-django-app/
采用unix socket 时,注意保持nginx 有权访问 /tmp/test_pootle.sock
5.启动,重启,停止 uwsgi 命令
启动 sudo uwsgi -x pootle.xml
重启 sudo kill -SIGHUP `cat /var/www/pootle/log/pootle.pid`
停止 sudo kill -SIGINT `cat /var/www/pootle/log/pootle.pid`
6.精选参考资料
配置Nginx+uwsgi更方便地部署python应用 http://obmem.info/?p=703
Nginx+uWSGI 部署 Django 应用 http://simple-is-better.com/news/756
Nginx+uWSGI部署django应用 http://blog.hysia.com/post/2/nginx-uwsgi-deploy-django-apps/
用uWSGI替代fastcgi部署django应用 http://ichuan.net/post/6/using-uwsgi-instead-of-fastcgi-for-django-app/ 推荐这篇文章
Sexy: NGiNX + uWSGI deployment for Django with virtualenv http://senya.pl/2011/03/sexy-nginx-uwsgi-stack-for-django-with-virtualenv/
Nginx+uWSGI部署Django应用 http://blog.freestorm.org/?p=70
6.说明
uwsgi 参数 cddir ? pythonpath or pp ,这两个参数,偶还有些疑惑
在产品上,可去掉 pootle.xml 中daemonize中的日志设置,不然日志会一直增加,可设置为<daemonize/>,该日志记录uwsgi的访问状态
多个app的配置,一般有两种方式.