安装与部署

安装客户端

编译源码

克隆仓库

Tafor 的源码公开在 GitHub, 你可以在这里找到项目的 介绍

你可以使用 Git 工具克隆仓库:

$ git clone https://github.com/up1and/tafor.git

安装依赖

首先确保已经正确安装 Python3:

$ pip install -r requirements.txt

安装打包工具 PyInstaller:

$ pip install pyinstaller

Windows 下如何安装 PyInstaller 的请参考 这里

编译

打包成可执行的 exe 程序,请在仓库根目录下运行:

$ python setup.py build

打包成功~

下载发行版

Tafor 的发行版同样放在 GitHub, 你可以在这里 下载

发行版依赖

  • 操作系统需 Windows 7 SP1 或以上
  • 遇到缺失 api-ms-win-crt-runtime-l1-1-0.dll,请确保安装 Microsoft Visual C++ 2015 Redistributable

部署数据源接口

数据源的接口样例可以在 tafor/scripts/ 找到。

注解

不同地方获取数据源的方式不同,所以请根据实际情况更改。

下面用一个简单的例子来说明如何在 Ubuntu 22.04 部署 Flask 应用。

安装依赖

安装 NginxSupervisor 以及所需的一些依赖:

$ sudo apt install nginx supervisor
$ sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev

安装 pyenv & pyenv-virtualenv [1]

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc
$ exec $SHELL

安装 Python:

$ pyenv install 3.10.8

tafor/scripts/ 目录下的文件放入 /var/www/tafor-api 目录下, 创建虚拟环境并安装依赖:

$ cd /var/www/tafor-api
$ pyenv virtualenv 3.10.8 tafor-api
$ pyenv activate tafor-api
$ pip install -r requirements.txt

测试启动 Flask 应用

我们通过 Gunicorn 来启动 Flask,你也可以使用 uwsgi:

$ gunicorn --bind 0.0.0.0:8000 tafor:app

如果你能通过本机 IP 地址加端口 :8000 访问网页,则应用没有问题。

使用 Supervisor 守护进程

Supervisor 是一个进程守护软件,我们通过它来启动 Flask 程序,你也可以使用 UpstartSystemd 等。

创建配置文件:

$ sudo vi /etc/supervisor/conf.d/tafor-api.conf

写入配置:

[program:tafor-api]
environment = TAFOR_API_ENV=prod
command = /home/user/.pyenv/versions/tafor-api/bin/gunicorn tafor:app --workers 1 --bind unix:tafor-api.sock -m 644
directory = /var/www/tafor-api
user = root
startsecs = 0
stopwaitsecs = 0
autostart = true
autorestart = true

注解

请注意把 user 替换为实际用户所在的地址。

生效配置:

$ sudo service supervisor restart

使用 Nginx 代理请求

我们需要通过 Nginx 把请求转发到 tafor-api.sock 文件上,

创建配置文件:

$ sudo vi /etc/nginx/sites-available/tafor-api

写入配置:

upstream tafor-api {
    server unix:///var/www/tafor-api/tafor-api.sock fail_timeout=0;
}


server {

    listen 6575;
    listen [::]:6575;

    access_log    /var/log/nginx/tafor-api-access.log;
    error_log    /var/log/nginx/tafor-api-error.log;

    root /var/www/tafor-api;

    location / {
        proxy_pass http://tafor-api;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

生效配置:

$ sudo ln -s /etc/nginx/sites-available/tafor-api /etc/nginx/sites-enabled
$ sudo service nginx restart

注解

如果是第一次安装使用 Nginx 或者重启 Nginx 服务之后 localhost 还是显示默认欢迎页面,请删除 /etc/nginx/sites-enabled/default 文件。

其他部署方法请参考 How To Serve Flask Applications with Gunicorn and Nginx on Ubuntu 22.04

部署的方式有多种,这只是其中一些例子,你也可以选择 Docker 部署 Flask 应用。

[1]pipenv 也是一个不错的选择。