首先安裝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即可