版權(quán)聲明:本文章博主原創(chuàng)文章,未經(jīng)允許不得轉(zhuǎn)載
配置環(huán)境:阿里云服務(wù)器
1哼转、Nginx和uwsgi關(guān)系
Nginx相當(dāng)于是服務(wù)器明未,負(fù)責(zé)接收請(qǐng)求
uwsgi是服務(wù)器和服務(wù)端應(yīng)用程序的通信協(xié)議,規(guī)定了怎么把請(qǐng)求轉(zhuǎn)發(fā)給應(yīng)用程序和返回
兩個(gè)基本概念:
服務(wù)器(接收請(qǐng)求)壹蔓,應(yīng)用程序(處理請(qǐng)求并返回)
通信過(guò)程:
客戶端發(fā)送一個(gè)http請(qǐng)求趟妥,被nginx服務(wù)器接收,nginx服務(wù)器將請(qǐng)求轉(zhuǎn)發(fā)給uwsgi,uwsgi將請(qǐng)求轉(zhuǎn)發(fā)給實(shí)現(xiàn)uwsgi協(xié)議的應(yīng)用程序(flask,gunicorn等等)
2佣蓉、安裝
啟動(dòng)虛擬環(huán)境
pyenv activate env352(注:python3.5.2環(huán)境)
安裝flask和uwsgi
pip install flask
pip install uwsgi
安裝nignx
sudo apt-get install nginx
啟動(dòng)nignx
sudo /etc/init.d/nignx start
3披摄、修改配置
添加test.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
? ? return "I am app"
添加uwsgi配置文件uwsgi_flask.ini
[uwsgi]
http = :9090 ? ?#方便HTTP訪問(wèn)調(diào)試
socket = 127.0.0.1:3031 ? #綁定socket通信端口,注:和后面的nignx配置一致
processes = 4
threads = 2
master = true
pythonpath = /home/kivvisystemserver/www/ ? #flask網(wǎng)站目錄
module = test ? ?#運(yùn)行的flask主文件
callable = app ? #flask主文件中的調(diào)用應(yīng)用名稱
memory-report = true
stats = 127.0.0.1:9191 ? #檢測(cè)狀態(tài)
daemonize = /var/log/uwsgi.log ? ?#后臺(tái)運(yùn)行并保存日志
使用uwsgi托管flask應(yīng)用
bash中運(yùn)行:uwsgi uwsgi_flask.ini
4勇凭、nignx
修改配置文件/etc/nginx/sites-enabled/default添加如下內(nèi)容:
location/{
? ? include uwsgi_params;
? ? uwsgi_pass127.0.0.1:3031;
? ? ?#try_files $uri $uri/ =404; ?注:一定注釋掉這行疚膊,否則無(wú)法跳轉(zhuǎn)到Nignx,出現(xiàn)404錯(cuò)誤
}
去掉前面uwsgi_flask.ini文件中的http= :9090參數(shù)
重啟nignx
/etc/init.d/nignx restart
訪問(wèn)http://127.0.0.1:80/
至此搭建成功虾标!
關(guān)閉uwsgi進(jìn)程
killall -9 uwsgi
查看所有uwsgi進(jìn)程
ps aux | grep uwsgi