文章目录
  1. 1. Linux 系统下部署
    1. 1.1. 准备源码
    2. 1.2. 安装依赖
    3. 1.3. 安装 nginx
    4. 1.4. 重启服务
  2. 2. Docker 镜像制作
    1. 2.1. Dockerfile 编写
  3. 3. docker-compose 部署
  4. 4. AnkiDroid 配置
  5. 5. Anki for windows 配置

Anki 有个开源的同步服务器 ankisyncd,支持 AnkiDroid 和 Anki PC 版同步数据,下面介绍一下部署方法:

Linux 系统下部署

Linux 系统下部署需要一台 Linux 的服务器,我这里以 Ubuntu 为例,Debian 和 Centos 的方法类似。

准备源码

1
2
3
4
5
# 下载源码
wget https://github.com/ankicommunity/anki-sync-server/archive/refs/tags/v2.3.0.zip
# 解压
unzip v2.3.0.zip
rm -rf v2.3.0.zip

这里使用了 2.3.0 版本,其它版本方法类似,具体的请参考 ankisyncd

安装依赖

由于 ankisyncd 使用了 python 编写,而且官方使用了 poetry 做为包管理,所以我这里也直接使用 poetry 来安装依赖。

1
2
3
4
5
6
7
8
9
# 更新源
apt update
# 安装必要的系统依赖
apt install -y wget unzip nginx python3 python3-pip
pip install poetry
# 进入源码目录
cd anki-sync-server-2.3.0
# 安装依赖,且不安装开发才需要的依赖
poetry install --no-dev

安装 nginx

1
apt install nginx

修改 nginx 的配置,使用反向代理 ankisyncd, 并转成 http1.0。这步很重要,如果没有 nginx 转换成 http 1.0 会造成媒体文件上传失败。

配置文件: /etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## anki_sync_server;
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

# set client body size to 0, disable the body size check
client_max_body_size 0;

sendfile on;

keepalive_timeout 65;

server {
listen 80;
location / {
proxy_http_version 1.0;
proxy_pass http://127.0.0.1:27701;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

listen 80; 这个端口按自己的需求修改,这个是实际对外使用的服务端口

重启服务

ankisyncd 都安装并配置完毕后就可以重启服务了,以下操作都在 anki-sync-server-2.3.0 目录中进行。

启动 ankisyncd

1
2
3
4
# 添加用户,根据提示再输入密码,如果用户已经添加,则此步骤可以跳过
poetry run python src/ankisyncctl.py adduser [username]
# 启动 ankisyncd 服务
poetry run python -m ankisyncd

启动 nginx

由于 apt 安装的 nginx 会带有 systemd 的 service 配置,所以可以直接使用 systemd 启动。

1
systemctl start nginx

测试访问

使用浏览器打开 http://[服务器地址]:[端口] 如果可以正常显示 Anki Sync Server 表示已经运行成功。

Docker 镜像制作

由于 docker-compose 部署需要制作镜像,并且 anki-devops-services 里面提供的镜像版本有点旧,所以这里就先介绍一下镜像如何制作。

Dockerfile 编写

新建 ankisyncd 目录,并在其中准备以下文件

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
FROM ubuntu:20.04

ENV TZ=Asia/Shanghai

RUN apt update && \
DEBIAN_FRONTEND=noninteractive apt install -y wget unzip nginx python3 python3-pip && \
rm -rf /var/lib/apt/lists/* && \
apt-get clean

RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && dpkg-reconfigure --frontend noninteractive tzdata

RUN pip install poetry && \
mkdir /app && \
cd /app && \
wget https://github.com/ankicommunity/anki-sync-server/archive/refs/tags/v2.3.0.zip && \
unzip v2.3.0.zip && \
rm -rf v2.3.0.zip && \
cd /app/anki-sync-server-2.3.0 && \
poetry install --no-dev

WORKDIR /app/anki-sync-server-2.3.0

ADD ./start.sh /app/anki-sync-server-2.3.0/start.sh
ADD ./nginx.conf /etc/nginx/nginx.conf
ADD ./ankisyncd.conf /app/anki-sync-server-2.3.0/src/ankisyncd.conf

CMD bash start.sh

这里添加了 nginx.conf, ankisyncd.conf, start.sh 这三个文件,内容如下:

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## anki_sync_server;
worker_processes 1;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

# set client body size to 0, disable the body size check
client_max_body_size 0;

sendfile on;

keepalive_timeout 65;

server {
listen 80;
location / {
proxy_http_version 1.0;
proxy_pass http://127.0.0.1:27701;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

ankisyncd.conf

1
2
3
4
5
6
7
8
9
10
[sync_app]
# change to 127.0.0.1 if you don't want the server to be accessible from the internet
host = 0.0.0.0
port = 27701
data_root = /data/collections
base_url = /sync/
base_media_url = /msync/
auth_db_path = /data/auth.db
# optional, for session persistence between restarts
session_db_path = /data/session.db

start.sh

1
2
3
4
#!/bin/bash

nginx
exec poetry run python -m ankisyncd

进入 ankisyncd 中后执行如下命令,构建镜像

1
docker build -t ankisyncd .

这样就会在本地构建出一个 ankisyncd 镜像,也可以直接使用我构建的 ankisyncd 镜像 registry.cn-hangzhou.aliyuncs.com/chronos/ankisyncd:2.3.0

docker-compose 部署

docker-compose 部署方式需要事先准备一个 volume 或着指定数据文件存储的位置,比如我这里使用 /root/ankisyncd/data 做为数据目录

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
version: "3"

services:
ankisyncd:
image: registry.cn-hangzhou.aliyuncs.com/chronos/ankisyncd:2.3.0
ports:
- "80:80"
volumes:
- "/root/ankisyncd/data:/data"
restart: always

使用 docker-compose 启动服务,并对外监听 80 端口

1
docker-compose up -d

添加用户

1
docker-compose exec ankisyncd ./ankisyncctl.py adduser [username]

AnkiDroid 配置

设置 -> 高级设置 中填写自定义同步服务器,将 ankisyncd 的对外服务地址填写进去。然后再同步的时候输入对应的用户名密码就可以同步了。

Anki for windows 配置

点击 查看本地插件文件 ,在打开的文件夹中新建一个目录 ankisyncd ,里面添加一个 __init__.py

1
2
3
4
5
import os

addr = "http://[server address]:[port]/" # put your server address here
os.environ["SYNC_ENDPOINT"] = addr + "sync/"
os.environ["SYNC_ENDPOINT_MEDIA"] = addr + "msync/"

addr 地址填写 nginx 对外的服务地址

重启 Anki,点击同步,在打开的对话框中输入你在前面填写的用户名和密码就可以同步了

文章目录
  1. 1. Linux 系统下部署
    1. 1.1. 准备源码
    2. 1.2. 安装依赖
    3. 1.3. 安装 nginx
    4. 1.4. 重启服务
  2. 2. Docker 镜像制作
    1. 2.1. Dockerfile 编写
  3. 3. docker-compose 部署
  4. 4. AnkiDroid 配置
  5. 5. Anki for windows 配置