LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > Linux教程 >

uwsgi的部署和优化

时间:2013-08-05 10:44来源:opython.com/?p=196 编辑:opython 点击:
安装 uwsgi有很多安装方式,编译安装,pip ,系统包管理(apt,yum) 。 所有的服务器端软件,无特殊需求,推荐以系统包管理(apt,yum)的方式安装。以程序来使用的python模块,诸如,salt,hg,s

安装
uwsgi有很多安装方式,编译安装,pip ,系统包管理(apt,yum) 。

所有的服务器端软件,无特殊需求,推荐以系统包管理(apt,yum)的方式安装。以程序来使用的python模块,诸如,salt,hg,supervisor 也推荐以系统包管理的方式安装。以库来使用的才用pip的方式安装。

所以uwsgi的最好安装方式是以包管理的方式安装。
apt-get install uwsgi uwsgi-plugins-all

运行
uwsgi能部署所有wsgi程序。

我们有如下的一个 app.py
def application(environ, start_response):
    status = '200 OK'
    output = 'World!'
    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(12)]
    write = start_response(status, response_headers)
    write('Hello ')
    return [output]
可以通过uwsgi直接运行
uwsgi -s 127.0.0.1:8080 -w app
在nginx我们这样配置
server {
    server_name example.com;
    location / {
        include uwsgi_params;
        uwsgi_pass 192.168.1.201:8081;
    }
}
这样就完成了最简单的部署。

举个例子,我们通过uwsgi部署hg的web。我们假设hg仓库的根目录是/var/hg

新建hgweb.conf 这个和wsgi无关,是hg的web配置
[collections]
/var/hg = /var/hg
[web]
style = paper
allow_archive = gz, zip, bz2
baseurl = /
hidden = false
allow_read = *
allow_push = *
push_ssl = false
新建 hgweb.py
from mercurial import demandimport; demandimport.enable()
from mercurial.hgweb.hgwebdir_mod import hgwebdir
application = hgwebdir('/var/hg/hgweb.conf')
运行
uwsgi -s 127.0.0.1:8080  -w hgweb --daemonize /var/log/hgrepo.wsgi
其中参数-w是 hgweb.py –daemonzie 以守护进程启动。

Django 的部署
对于流行的框架,uwsgi有特别的支持。下面是一个典型的uwsgi django运行命令
uwsgi --plugins python -s 127.0.0.1:8080 -M -p 8 -t 600 -R 2000 \
    --virtualenv=/usr/local/python-env/django
    --env DJANGO_SETTINGS_MODULE=settings \
    -w "django.core.handlers.wsgi:WSGIHandler()" \
    --daemonize django.wsgi --pidfile /var/run/django.pid
    --touch-reload /var/run/django.pid
其中uwsgi提供了方便的virtualenv支持,能够直接指定运行需要的virtualenv,对于多站点的部署隔离环境非常好用。touch-reload 提供了方便的重新载入方式。一般uwsgi启动cpu核心倍数的进程,由-p指定。其他参数请参考uwsgi文档。或 uwsgi -h 直接查看。

生产运营
在实际运营当中并不是直接部署就万事大吉。要想提供稳定的服务还有很多监控和优化的工作需要做。

在每秒数百请求的压力下,有可能面临文件描述符不足。ubuntu server默认只有1024
ulimit -n 10240
在处理较费时的请求时,uwsgi队列数可能面临不足 添加–listen参数,该参数默认为100,可改为1024. 诸如此类的性能参数需要在运营中调整。

转载请保留固定链接: https://linuxeye.com/Linux/1879.html

------分隔线----------------------------
标签:uwsgi
栏目列表
推荐内容