首先安装uwsgi
#使用pip安装
pip install uwsgi
安装后可以到python的bin目录中找到可执行文件
ls -s /usr/local/python/bin/uswgi /usr/bin
这样就可以直接使用uwsgi命令了
入门
先写一个wsgi的应用
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
保存成foobar.py
uwsgi - -http :9090 - -wsgi-file foobar.py
在浏览器中输入网址 http://127.0.0.1:9090
就可以看到内容为Hello World的网页
这样第一个http 的uwsgi的应用就写好了
设置进程和线程
- -master 管理processes进程
- -processes 4 启动4个进程
- -threads 2 每个进程的线程 为2
- -stats 127.0.0.1:9191 uwsgi的状态
shell 命令:
uwsgi - -http :9090 - -wsgi-file foobar.py - -master - -processes 4 - -threads 2 - -stats 127.0.0.1:9191
如何和nginx搭配使用
1.nginx的server配置
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
2.uwsgi的启动配置
uwsgi - -socket 127.0.0.1:9090 - -wsgi-file foobar.py - -master - -processes 4 - -threads 2 - -stats 127.0.0.1:9191
记住要是–socket
这样访问你配置的server就可以了
我的nginx配置文件贴出来
server {
listen 80;
server_name hishop.xiaohei.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
include uwsgi_params
uwsgi_pass 127.0.0.1:9090;
}
}
#这是最简单的,实际环境需要自己配置其他选项
这样在浏览器里访问 hishop.xiaohei.com就可以看到hello world的网页了
如何和django搭配使用
–chdir /home/foobar/myproject/ #项目的文件目录
–wsgi-file myproject/wsgi.py #wsgi的入口文件
uwsgi –socket 127.0.0.1:9090 –chdir /home/foobar/myproject/ –wsgi-file myproject/wsgi.py –master –processes 4 –threads 2 –stats 127.0.0.1:9191
使用uwsgi的配置文件
vim myproject.ini
[uwsgi]
socket = 127.0.0.1:9090
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191
保存
然后执行 uwsgi myproject.ini即可