flask部署gunicorn+gevent其中一種部署方式

gunicorn+gevent+nginx部署flask應用

96 
應君江湖不相忘 關注
2016.11.16 19:44* 字數 451 閱讀 3924評論 0喜歡 5

gunicorn+gevent+nginx部署flask應用

此次部署是在ubuntu上進行的,在部署開始以前要作如下事情:php

1.在你的linux系統上安裝python-pip
sudo apt-get install python-setuptools
sudo apt-get install python-dev 
sudo apt-get install python-pip
2.安裝virtualenv
sudo apt-get install python-virtualenv
3.切換到你的flask應用項目的根目錄
virtualenv venv
source venv/bin/activate 
4.進入虛擬環境後,安裝你的flask應用的全部擴展包,最好把全部的擴展包寫入requirements.txt
pip install -r requirements.txt 
5.安裝gunicorn和gevent
pip install gunicorn
pip install gevent
6.啓動gunicorn(注:這時必須進入你項目的根目錄且處於虛擬環境中,由於gunicorn安裝在虛擬環境中)

1.配置gunicorn啓動配置文件,在項目的根目錄建立一個gun.conf,寫入如下內容:css

import os
bind = '0.0.0.0:8000' #綁定的ip及端口號 workers = 4 #進程數 backlog = 2048 #監聽隊列 worker_class = "gevent" #使用gevent模式,還可使用sync 模式,默認的是sync模式 debug = True chdir = #你項目的根目錄,好比個人app.py文件在/home/ubuntu/app目錄下,就填寫'/home/ubuntu/app' proc_name = 'gunicorn.proc' 

2.而後執行如下代碼啓動python

gunicorn -k gevent -c gun.conf app:app 

若是看到相似下面的反應就表示啓動成功,若是失敗請檢查你所在的目錄是否爲Flask項目的根目錄linux

(venv) ubuntu@VM-250-138-ubuntu:~/app$ gunicorn -k gevent -c gun.conf app:app [2016-09-10 23:07:55 +0800] [2641] [DEBUG] Current configuration: paste: None post_fork: <function Postfork.post_fork at 0x7fda10539bf8> threads: 1 chdir: /home/ubuntu/app group: 500 post_request: <function PostRequest.post_request at 0x7fda10544598> forwarded_allow_ips: ['127.0.0.1'] worker_exit: <function WorkerExit.worker_exit at 0x7fda10544730> worker_tmp_dir: None tmp_upload_dir: None secure_scheme_headers: {'X-FORWARDED-SSL': 'on', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-PROTOCOL': 'ssl'} syslog_addr: udp://localhost:514 post_worker_init: <function PostWorkerInit.post_worker_init at 0x7fda10539d90> pre_exec: <function PreExec.pre_exec at 0x7fda105442f0> max_requests_jitter: 0 timeout: 30 keepalive: 2 umask: 0 logger_class: gunicorn.glogging.Logger syslog_prefix: None spew: False pre_fork: <function Prefork.pre_fork at 0x7fda10539a60> keyfile: None ca_certs: None do_handshake_on_connect: False worker_connections: 1000 pre_request: <function PreRequest.pre_request at 0x7fda10544488> workers: 4 ssl_version: 3 sendfile: None reload: False syslog: False statsd_prefix: limit_request_field_size: 8190 cert_reqs: 0 ciphers: TLSv1 syslog_facility: user on_reload: <function OnReload.on_reload at 0x7fda10539730> on_exit: <function OnExit.on_exit at 0x7fda10544a60> proxy_protocol: False suppress_ragged_eofs: True check_config: False proc_name: gunicorn.proc capture_output: False enable_stdio_inheritance: False pidfile: /home/ubuntu/app/gunicorn.pid worker_int: <function WorkerInt.worker_int at 0x7fda10539f28> accesslog: None loglevel: debug logconfig: None statsd_host: None nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7fda105448c8> preload_app: False default_proc_name: app:app limit_request_line: 4094 errorlog: - daemon: False pythonpath: None certfile: None bind: ['127.0.0.1:8000'] when_ready: <function WhenReady.when_ready at 0x7fda105398c8> worker_class: gevent raw_env: [] graceful_timeout: 30 on_starting: <function OnStarting.on_starting at 0x7fda10539598> max_requests: 0 backlog: 2048 proxy_allow_ips: ['127.0.0.1'] django_settings: None config: gun.conf user: 500 access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" limit_request_fields: 100 worker_abort: <function WorkerAbort.worker_abort at 0x7fda10544158> [2016-09-10 23:07:55 +0800] [2641] [INFO] Starting gunicorn 19.6.0 [2016-09-10 23:07:55 +0800] [2641] [DEBUG] Arbiter booted [2016-09-10 23:07:55 +0800] [2641] [INFO] Listening at: http://127.0.0.1:8000 (2641) [2016-09-10 23:07:55 +0800] [2641] [INFO] Using worker: gevent [2016-09-10 23:07:55 +0800] [2644] [INFO] Booting worker with pid: 2644 [2016-09-10 23:07:55 +0800] [2646] [INFO] Booting worker with pid: 2646 [2016-09-10 23:07:55 +0800] [2647] [INFO] Booting worker with pid: 2647 [2016-09-10 23:07:55 +0800] [2648] [INFO] Booting worker with pid: 2648 [2016-09-10 23:07:55 +0800] [2641] [DEBUG] 4 workers 

命令的app:app中第一個爲你定義Flask應用實例的py文件,這裏不要加入後綴,例如:nginx

#app.py from flask import Flask app = Flask(__name__) @app.route("/") def index(): return "你已經成功了。" if __name__ == "__main__": app.run() 

第二個app是你在該文件中實例化的Flask應用的變量名,而後打開你本地電腦的瀏覽器,訪問http:\你的服務器ip:8000,
你就能夠看到你的Flask應用。sql

7.另外啓動一個終端,安裝nginx並配置(以後的操做不須要再虛擬環境中進行)
sudo apt-get install nginx

打開/etc/nginx/sites-enabled/,備份default文件後,在default加入如下內容:django

server {
        listen   80; server_name 公網IP或者你已經解析的域名; location / { proxy_pass http://127.0.0.1:8000; proxy_redirect off; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

而後輸入如下命令檢查nginx配置文件是否有錯:flask

sudo nginx -t

#成功會出現相似的提示: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #出錯請檢查你的配置 

啓動nginxubuntu

sudo service nginx start
sudo nginx -s reload

能夠經過如下命令查看nginx狀態瀏覽器

service nginx status

打開瀏覽器,訪問http://你的服務器ip,就能夠看到你的Flask應用界面。


項目目錄結構

app/
 |----app.py |----manage.py |----requirements.txt |----data.sqlite |----gun.conf |----static |----templates |----venv 

這個教程只是初步用來快速部署,以後會深刻研究下uwsgi和nginx部署.
相關文章
相關標籤/搜索