菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
167
0

uwsgi常用配置

原创
05/13 14:22
阅读数 62778

一、安装方式

  1、wget 

    可以去官网:https://pypi.python.org/pypi/uWSGI/

    下载对应的版本

      tar -xvf uwsgi-2.13.1.tar.gz

    cd uwsgi-2.13.1

    make

    sudo cp uwsgi /usr/bin/uwsgi

  2、sudo pip install uwsgi

  成功后查看版本

    uwsgi --version

  3、测试是否安装成功

    新建:server.py

def application(env, start_response):
	start_response('200 OK', [('Content-Type','text/html')])
	return "Hello World"

   uwsgi --http :8001 --wsgi-file server.py

  浏览器访问,如果出现 Hello World则表示uwsgi安装成功

  或者curl http://127.0.0.1:8001

二、配置

uwsgi 有多种配置可用:
1,ini ,
2,xml ,
3,json,
4,yaml。

从uwsgi的官方文档来看,貌似(我个人的理解)推荐用ini方式,所以下面的配置也都是基于ini的。

● ini 格式说明:
1,ini配置为 key=value 形式
2,在ini配置文件里,#号为注释,
3,布尔值为 true 和 false
4,在命令行里,uwsgi myconf.ini 等价于 uwsgi --ini myconf.ini

● uwsgi.ini 配置示例:
[uwsgi]
socket = 127.0.0.1:8000
workers = 4

uwsgi 选项说明:
● 选项的格式:
1,命令行参数格式:--<option>
2,配置格式(以ini为例):option = xxxx

● 常用选项:
socket : 地址和端口号,例如:socket = 127.0.0.1:50000
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。如果不开启这项,那么你的日志中会大量出现这种记录:
[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)

● 其他选项说明:
其他选项,具体可以通过 --help 选项来查看:
uwsgi --help

项目中的配置文件
[root@6c2879a830ce run]# cat /tmp/uwsgi.ini
[uwsgi]
http-socket = :80
plugin = python
chdir = /code/run/cms
wsgi-file = cms/wsgi.py
processes = 4
threads = 4
max-request = 20480
log-x-forwarded-for = true
logto = /code/uwsgi_web.log
stats = 127.0.0.1:9191

顺便推荐另一xml方式

<uwsgi id="webapi">
  <socket>127.0.0.1:7625</socket>
  <module>webapi</module>
  <master />
  <processes>48</processes>
  <thread>4</thread>
  <chdir>..</chdir>
  <pidfile>../../../data/webapi.pid</pidfile>
  <daemonize>../../../log/system_log/web_api/uwsgi_webapi.log</daemonize>
  <buffer-size>65535</buffer-size>
  <listen>1000</listen>
</uwsgi>

启动

  uwsgi -x webapi.xml

查看 ps -ef | grep uwsgi

发表评论

0/200
167 点赞
0 评论
收藏