gunicorn+gevent+nginx部署flask应用

这次部署是在ubuntu上进行的,在部署开始之前要做以下事情:

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,写入以下内容:

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.然后执行以下代码启动

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

如果看到类似下面的反应就表示启动成功,如果失败请检查你所在的目录是否为Flask项目的根目录

(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文件,这里不要加入后缀,例如:

#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应用。

7.另外启动一个终端,安装nginx并配置(之后的操作不需要再虚拟环境中进行)
sudo apt-get install nginx

打开/etc/nginx/sites-enabled/,备份default文件后,在default加入以下内容:

    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-IPremote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

然后输入以下命令检查nginx配置文件是否有错:

sudo nginx -t

#成功会出现类似的提示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#出错请检查你的配置

启动nginx

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部署.