安装uwsgi
# 在系统环境下直接运行
pip3 install uwsgi
# 进入项目虚拟环境再安装一边
workon env # 进入虚拟环境的方法可能有所不同
(env): pip3 install uwsgi
# 给uwsgi建立软链接,方便使用
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
配置uwsgi
uwsgi可支持命令行启动、json格式配置文件启动、ini格式配置文件启动、xml配置文件启动
- 端口
- 项目路径(chdir)
- wsgi.py所在目录名(–wsgi-file、module)
- 进程数等
命令行启动:
uwsgi --http :8000 --chdir /。。。/demo --wsgi-file /。。。/demo/demo/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
xml配置文件启动
# 项目根目录/项目名.xml
<uwsgi>
<socket>127.0.0.1:8000</socket> <!-- 内部端口,自定义 -->
<chdir>/data/wwwroot/mysite/</chdir> <!-- 项目路径 -->
<module>mysite.wsgi</module> <!-- mysite为wsgi.py所在目录名-->
<processes>4</processes> <!-- 进程数 -->
<daemonize>uwsgi.log</daemonize> <!-- 日志文件 -->
</uwsgi>
启动之xml
uwsgi -x 项目名.xml
ini配置文件启动(我选择的方式)
# 项目根目录下:uwsgi.ini
[uwsgi]
#端口
socket = 127.0.0.1:8000
# django项目绝对路径
chdir = /opt/app/devops
# 模块路径(项目名称.wsgi)可以理解为wsgi.py的位置
module = devops.wsgi
# 允许主进程
master = true
#最多进程数
processes = 4
# 退出时候回收pid文件
vacuum = true
#日志大小配置500M
log-maxsize = 500000000
#记录日志配置
logto = /tmp/uwsgi.log
# 指定依赖的虚拟环境
virtualenv=/root/.virtualenvs/env
启动之ini
uwsgi --ini /opt/app/devops/uwsgi.ini --daemonize /tmp/uwsgi.log
注意:启动uwsgi的命令需要在项目的虚拟环境里启动哦!
常用配置参数
http : #协议类型和端口号
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)
log-maxsize :#记录日志配置最大多少,超过这个数会切割,单位kb
logto :#指定输出日志到文件logto = /tmp/uwsgi.log
资料补充
WSGI
WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。自从 WSGI 被开发出来以后,许多其它语言中也出现了类似接口。
WSGI 的官方定义是,the Python Web Server Gateway Interface。从名字就可以看出来,这东西是一个Gateway,也就是网关。网关的作用就是在协议之间进行转换。
WSGI 是作为 Web 服务器与 Web 应用程序或应用框架之间的一种低级别的接口,以提升可移植 Web 应用开发的共同点。WSGI 是基于现存的 CGI 标准而设计的。
很多框架都自带了 WSGI server ,比如 Flask,Django等等。当然性能都不好,自带的 web server 更多的是测试用途,发布时则使用生产环境的 WSGI server或者是联合 nginx 做 uwsgi 。uwsgi
uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
uWSGI
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。
要注意 WSGI / uwsgi / uWSGI 这三个概念的区分。
WSGI看过前面小节的同学很清楚了,是一种通信协议。
uwsgi同WSGI一样是一种通信协议。
而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。
为什么有了uWSGI为什么还需要nginx?因为nginx具备优秀的静态内容处理能力,然后将动态内容转发给uWSGI服务器,这样可以达到很好的客户端响应。
by :https://blog.csdn.net/midion9/article/details/51354774?utm_source=blogxgwz7
图片来自:https://www.jianshu.com/p/c3b13b5ad3d7
参考链接:
https://www.cnblogs.com/wdliu/p/8932816.html
https://www.django.cn/article/show-4.html