如何用Nginx搭建flask的https服务

Flask & uWSGI & Nginx

Posted by AlbertWu on August 4, 2018

本文主要记录了如何用uWSGI,Ngnix,Let’s Encrypt实现对flask应用的https服务。

第一部分,我们将使用Ubuntu 16.04上的Flask框架设置一个简单的Python应用程序。 本文的大部分内容将涉及如何设置uWSGI应用程序服务器以启动应用程序,以及如何设置Nginx作为前端的反向代理。三者关系详细配置说明

第二部分,我们采用Let’s Encrypt这一证书颁发机构(CA),提供一种获取和安装免费TLS / SSL证书的简便方法,从而在Web服务器上启用加密的HTTPS。详细配置说明

第一部分 完成flask,uWSGI,nginx互通信

配置flask应用

首先假设项目放置在~/myproject/

创建项目文件夹

$ mkdir ~/myproject/
$ cd ~/myproject/

建立虚拟环境并激活

$ virtualenv myprojectenv
$ source myprojectenv/bin/activate

创建flask应用

(myprojectenv)$ vi myproject.py

建立wsgi通道

$ vi wsgi.py
from myproject import app

if __name__ == "__main__":
    app.run()

测试并配置uWSGI

测试uWSGI服务是否可用

$ uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

配置uWSGI服务

$ vi myproject.ini
[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = myproject.sock
chmod-socket = 660
vacuum = true

die-on-term = true

注册服务单元

我们需要处理的下一件事是systemd服务单元文件。 创建一个systemd单元文件将允许Ubuntu的init系统在服务器启动时自动启动uWSGI并为Flask应用程序提供服务。

$ sudo vi /etc/systemd/system/myproject.service
[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=haow
Group=www-data
WorkingDirectory=/home/haow/myproject
Environment="PATH=/home/haow/myproject/myprojectenv/bin"
ExecStart=/home/haow/myproject/myprojectenv/bin/uwsgi --ini myproject.ini

[Install]
WantedBy=multi-user.target
$ sudo systemctl start myproject
$ sudo systemctl enable myproject

配置nginx服务

$ sudo vi /etc/nginx/sites-available/default
server {
    listen 443;
    server_name example.com www.example.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/haow/myproject/myproject.sock;
    }
}

第二部分 获取CA证书并获取https服务

安装Certbot

使用Let’s Encrypt获取SSL证书的第一步是在服务器上安装Certbot软件。

Certbot处于非常活跃的开发阶段,因此Ubuntu提供的Certbot软件包往往过时。 但是,Certbot开发人员维护一个具有最新版本的Ubuntu软件存储库,因此我们将使用该存储库。

$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx

启动Nginx服务

检查配置文件语法合法

$ sudo nginx -t

启动服务

$ sudo systemctl start nginx

检查服务状态

$ sudo systemctl status nginx

获取SSL认证

$ sudo certbot --nginx -d example.com -d www.example.com

第三部分 配置grafana服务

用https的子路径 https://example.com/grafana/ 访问grafana服务端口

配置Nginx服务

$ sudo vi /etc/nginx/sites-available/default
server {
    listen 443;
    
    server_name example.com www.example.com;

    location / {
        include uwsgi_params;

        uwsgi_pass unix:/home/haow/myproject/myproject.sock;
    }

    location /grafana/ {
        proxy_pass http://localhost:3000/;
  }
}

重新加载Nginx服务

$ sudo systemctl reload nginx

配置grafana服务

$ sudo vi /etc/grafana/gragana.ini
[server]
domain = example.com
root_url = %(protocol)s://%(domain)s/grafana/

重新启动grafana服务

$ sudo service grafana-server restart

参考资料

  1. 如何理解Nginx, WSGI, Flask之间的关系

  2. How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 16.04

  3. How To Secure Nginx with Let’s Encrypt on Ubuntu 16.04

  4. Running Grafana behind a reverse proxy


Creative Commons License
This work is licensed under a CC A-S 4.0 International License.