1. 安装
pip install uwsgi
//测试uWSGI是否安装成功 在终端中输入以下命令查看uwsgi的版本:uwsgi --version
2.简单运行
运行uwsgi:uwsgi --http :8000 --wsgi-file test.py
参数解释: http :8000表示使用http协议,端口号为8000, wigi-file则表示要运行的wsgi应用程序文件。uwsgi运行后打开浏览器,访问http://127.0.0.1:8000/ ,或者是相应服务器地址的8000端口,就可以看到hello world 页面了。
3.简单运行项目
如果想要运行项目来测试
uwsgi --http :8000 --chdir 项目路径 -w 项目.wsg --static-map=/static=static uwsgi --http :8000 --chdir /home/teacher/ -w teacher.wsgi --static-map=/static=static
4.配置文件
[uwsgi]
# uwsgi 启动时所使用的地址与端口注意socket与http不同,http可以被外网访问0.0.0.0
#一般与nginx一起使用
socket = 127.0.0.1:8001
# 指向网站目录 chdir = /home/www/
# python 启动程序文件
wsgi-file = manage.py
# python 程序内用以启动的 application 变量名
#注意:callable=app 这个 app 是 manage.py 程序文件内的一个变量,这个变量的类型是 Flask的 application 类 。
callable = app
# 指定项目的application
module=teacher.wsgi:application
# 处理器数
processes = 4
# uwsgi的进程名称前缀
procname-prefix-spaced=mysite
# 进程个数
workers=5
# 线程数
threads = 2
#状态检测地址
stats = 127.0.0.1:9191 stats=%(chdir)/uwsgi/uwsgi.status # status文件,可以查看uwsgi的运行状态
#必须要有:方便关闭 pid文件,通过该文件可以控制uwsgi的重启和停止
pidfile=/opt/project_teacher/script/uwsgi.pid
# 指定静态文件
static-map=/static=/opt/test_project/teacher/static
#做一个映射,指定静态文件
static=/
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候 退出时清楚环竟 包含pid、sock和status文件
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间 一个请求超时时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录:这个也必须要有,因为不这样日志会在后台直接打印,当退出xshell程序,这个程序的后台也就自动停止
daemonize=/opt/project_teacher/script/uwsgi.log
# 指定sock的文件路径 可以ip地址,也可以是文件 socket=/opt/project_teacher/script/uwsgi.sock
5.常用命令
uwsgi --ini uwsgi.ini # 启动 uwsgi --reload uwsgi.pid # 重启 uwsgi --stop uwsgi.pid # 关闭 uwsgi的启动可以把参数加载命令行中,也可以是配置文件 .ini, .xml, .yaml 配置文件中,个人用的比较多得是 .ini 文件。