Flask+uWSGI+nginx

來源:互聯網
上載者:User

環境

CentOS

名詞解釋

flask:Flask是一個Python實現的Web開發微服務架構,相關資料,http://docs.jinkan.org/docs/flask/

uWSGI:uWSGI是一個web伺服器,它實現了WSGI協議、uwsgi、http協議等

部署釋疑

單獨Flask也可以啟動並提供web服務,但是Flask畢竟只是一個web架構,它的web服務只能用於開發環境,不能用於生產環境 ,因此還需要和uWSGI及nginx一起整合

uWSGI本身就是一個web服務,這裡再次引入nginx的原因是因為nginx是一個反向 Proxy。這樣,一些圖片、js等靜態資源就可以用nginx提供服務,而其他的轉寄給uWSGI,這也是我們這套部署架構的最終目的

整合步驟

1、全域安裝flask

>sudo pip install flask

2、實現一個最小應用hello.py

from flask import Flaskapp = Flask(__name__)@app.route('/')def hello_world():    return 'Hello World!'if __name__ == '__main__':    app.run(host='0.0.0.0') # host='0.0.0.0' 標示外部可訪問


3、安裝nginx

>sudo yum install nginx

4、修改nginx配置

如果是自己下載安裝的nginx,預設的安裝目錄可能是/usr/local/nginx

但如果是yum安裝的,預設的安裝目錄則在/etc/nginx/,並且預設安裝的nginx設定檔內有如下配置  

include /etc/nginx/conf.d/*.conf;

這樣,我們只需要在/etc/nginx/conf.d/目錄下增加一個hello.conf

upstream hello-test {    server unix:///root/works/flask/hello.sock;}server {    listen      8088;    server_name  hello.yunzong  localhost;    charset    utf-8;    client_max_body_size 100M;    location / {        uwsgi_pass  hello-test;        add_header 'Access-Control-Allow-Origin' '*';        add_header 'Access-Control-Allow-Credentials' 'true';        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';    }}


>systemctl restart nginx

注意:如果啟動失敗提示:nginx: [emerg] bind() to 0.0.0.0:8088 failed (13: Permission denied)

那麼可能是系統開啟了selinux,我們需要關閉selinux,以下是臨時關閉命令,如果想永久關閉請另行google

>setenforce 0

那麼此時我們就已經啟動了nginx,然後我們訪問http://10.211.55.3:8088/ ,這裡ip是nginx伺服器的ip,則會提示:502 Bad Gateway,這裡的原因是在上面的配置中我們配置了“server unix:///root/works/flask/hello.sock;”,但是我們沒有部署uWSGI,並且還沒有提供socket服務

5、安裝uWSGI

>pip install uwsgi

6、建立一個hello.ini類型的檔案,當然其他的也可以,具體請google

[uwsgi]pythonpath = /root/tensorflow   # 可刪除chdir           = /root/works/flask/wsgi-file  = /root/works/flask/hello.py#socket file's locationsocket = /root/works/flask/hello.sock#permissions for the socket filechmod-socket    = 666#the variable that holds a flask application inside the module imported at line #6callable = app#location of log fileslogto = /root/works/flask/hello.log


注意:/root/flask目錄下放著我們之前的hello.py檔案

7、啟動uWSGI

>uwsgi --ini hello.ini &

注意:啟動uWSGI時注意命令中涉及的各種檔案及路徑


結束語:

整個過程還是遇到不少問題的

1、nginx 中配置的/root/works/flask/hello.sock 訪問提示(13: Permission denied),尋找資料說是關閉SeLinux,自己測試不行,所以就把這段配置改成了0.0.0.0:1500即ip和連接埠模式,同時修改了uWSGI的hello.ini配置 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.