使用 Gunicorn 和 Nginx 部署 Flask 项目

    运行 Flask 应用提供服务的,正常都会集成 WSGI Web服务器提供服务,而在众多的 WSGI Web 服务器中,比较常用的主要有两种,分别是 Gunicorn 和 UWSGI,同时,我们也会使用 Nginx 作为反向代理进行部署应用。

    本文因为需要安装 Nginx,所以文章内的命令和使用的系统相关,但是这样的命令不多,本文使用的 Ubuntu 16.04,因此包管理软件是 apt,如果使用的 RedHat 系列的话,那完全可以用 yum 代替。其他系列的系统可以查找相关文档寻找代替管理工具。

    1. sudo apt-get install python-pip python-dev nginx
    2. pip install gunicorn
    3. pip install flask

    这里前两句是更新一下软件源,并且保证我们的 pip 和 python 依赖库已经安装上了,同时,别忘了安装反向代理 Nginx。后面两句就是安装我们必备的 Gunicorn 和 Flask Python 库了。

    因为在我们的前文中已经写了一个代码了,所以这里就继续使用这段代码,使用方式是:

    1. git clone git@github.com:luke0922/the-way-to-flask.git
    2. cd the-way-to-flask/code
    3. python manage.py runserver

    此时,我们的服务器应该是已经运行起来了,但是,默认 Ubuntu 是开启了防火墙屏蔽所有端口访问的,所以我们可能需要打开防火墙端口,在 Ubuntu 16.04 中可以这样做:

    1. sudo ufw allow 5000

    一切正常的话,

    里面内容填:

    1. from myproject import app
    2. if __name__ == "__main__":
    3. app.run()

    然后使用这个命令运行代码:

    1. gunicorn --bind 0.0.0.0:5000 wsgi:app

    依旧访问这个地址看看:

    1. http://localhost:5000
    1. vim /etc/systemd/system/app.service

    里面的内容写:

    1. sudo systemctl start app
    2. sudo systemctl enable app

    配置Nginx

      里面写:

      1. server {
      2. listen 80;
      3. server_name server_domain_or_IP;
      4. location / {
      5. include proxy_params;
      6. proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
      7. }
      8. }

      保存之后,用 nginx 自带工具验证一遍

      如果ok的话然后让 nginx 重新加载配置

      关闭服务器端口:

      • sudo ufw delete allow 5000
      • sudo ufw allow ‘Nginx Full’