Centos上uwsgi+flask項目部署

最近需要提供服務給後臺,決定採用flask加載模型提供服務,網上查到的資料大多都是nginx+uwsgi+flask的方式,個人需求demo級別的,因此只用了uwsgi+flask。

一、安裝

pip install uwsgi  (好像只能在Linux上安裝)

pip install flask、

二、新建一個最簡單的應用

from flask import Flask
 
app = Flask(__name__)
 
 
@app.route('/')
def hello_world():
    return 'Hello World'
 
 
if __name__ == '__main__':
    app.run()

三、配置uwsgi

在應用目錄下新建一個文件uwsgi.ini,這裏名字你可以隨便取,後綴支持ini,xml,json三種格式。

配置內容如下:

[uwsgi]
http=0.0.0.0:8080
wsgi-file=/root/project/application/demo.py
callable=app
touch-reload=/root/project/application/
pidfile = /root/project/application/uwsgi.pid   #指定pid文件

1、http=0.0.0.0:8080,表示任意ip,端口號爲8080

2、wsgi-file=/root/project/application/demo.py,應用程序位置

3、callable=app,實例名稱

4、touch-reload=/root/project/application/,表示要監聽的文件路徑,當要監聽的文件路徑下的文件發生變化的時候自動重新加載服務器

5、pidfile = /root/project/application/uwsgi.pid    指定pid文件,用於啓動、重啓、關閉服務

四、運行

uwsgi uwsgi.ini  就可以運行

也可以:uwsgi --ini uwsigi.ini

重啓: uwsgi --reload uwsigi.ini

關閉:uwsgi --stop uwsigi.ini

五、注意

當服務器上同時存在python2和python3時:會出現找不到uwsgi的情況:

解決方法:

        ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi       實際情況根據自己安裝的位置設置

六、參考文獻

1、https://www.jianshu.com/p/1a65964c5b51

2、https://www.jianshu.com/p/830c4fd94605

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章