pip
install uwsgi
ln -s /usr/local/python3.6.4/bin/uwsgi /usr/bin/uwsgi
vim config.ini
[uwsgi]
chdir=/home/Py
pythonpath= /home/Py
#項目啓動文件
wsgi-file = /home/Py/run.py
#爲你的項目入口文件
module=run
#實例化flask的變量 application = Flask(__name__)
callable=application
#當修改文件內容後重啓uwsgi
py-autoreload=1
#開啓一個master進程監控項目運行
master=true
#uwsgi的端口。要與項目運行的端口一致
socket=/home/Py/flask_socket.sock
socket=127.0.0.1:5000
processes=4
#threads=2
buffer-size=65536
#容許用內嵌的語言啓動線程。這將容許你在app程序中產生一個子線程。
enable-threads=true
pidfile=/tmp/uwsgi-master.pid
uid=nginx
gid=root
#當服務器退出的時候自動刪除unix socket文件和pid文件。
vacuum=true
#皇帝」模式運行。在這種模式下,它將件事uWSGI配置文件的路徑而且爲每個它找到的文件生成實例(‘諸侯’)。不管何時一個配置文件被修改了,皇帝都會自動重啓諸侯
emperor=true
logto=/var/log/nginx/uwsgi.log
後臺啓動
uwsgi --ini config.ini &
● 經常使用選項:
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)
location /{
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /usr/bin/python; #也可對應虛擬環境
uwsgi_param UWSGI_CHDIR /home/Py;
uwsgi_param UWSGI_SCRIPT run:application;
}
匹配該路徑下的項目 重定向到81端口 定義81端口的項目uwsgi配置 一個項目對應一個uwsgi配置
location /Py1/{
proxy_pass http://127.0.0.1:81/;
}
server{
listen 81;
server_name localhost;
location /{
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /usr/bin/python;
uwsgi_param UWSGI_CHDIR /home/Py1;
uwsgi_param UWSGI_SCRIPT run:application;
}
}
location /Py2/{
proxy_pass http://127.0.0.1:82/;
}
server{
listen 82;
server_name localhost;
location /{
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
uwsgi_param UWSGI_PYHOME /usr/bin/python;
uwsgi_param UWSGI_CHDIR /home/Py2;
uwsgi_param UWSGI_SCRIPT run:application;
}
}