前言

十一月的 Black Friday 以及接近年尾的各种购物活动,相信不少喜欢折腾服务器的读者都有所收获。如果你恰好购买了用于 Torrenting 的服务器,那么这篇文章可能会对你有所帮助。根据我个人的观察,qBittorrent 无论在 BT 还是 PT,使用率都至少超过了一半,考虑到一些站点对软件版本的限制,以及各 Linux 发行版官方源的软件版本,我无法做出适合所有人的推荐,下面只讲讲我自己的构建流程

环境介绍

OS: Debian 11 bullseye (stable)

1
2
qBittorrent/4.4.5 # Release Date Aug 30, 2022
libtorrent/1.2.17 # Release Date Jul 17, 2022

在本文写作之时,查看 qbittorrent - Debian Package Tracker,能使用的版本很有限

1
2
3
4
5
6
7
8
bullseye (stable)
4.2.5-0.1

bookworm (testing)
4.5.0-2

sid (unstable)
4.5.0-2

这样选择起来就很简单了,理由有三

  1. qBittorrent 4.2.5 WebUI 不支持 RSS
  2. qBittorrent 4.2.5 WebUI 无禁止连接到特权端口上的 peer 选项
  3. qBittorrent 4.5.0 使用 libtorrent 2.0.x

经过 #17545 的一些讨论后,官方在 qBittorrent 4.4.5 将所有系统的默认构建从 libtorrent 2.0.x (RC_2_0) 切换回了 libtorrent 1.2.x (RC_1_2)。服务器需要长时间的运行稳定,但我又想保持尽可能新,所以选择 qBittorrent 4.4.5 版本。关于 Docker,因为 Docker 会带来一些 I/O 性能损失,具体有多少我也没试过,而且多数 Dockerfile 使用 Alpine 我不爱用(其实就是我不咋会 Docker)

安装依赖

我的构建流程大部分参考官方文档,真的写的非常详细了,包括 CMake 的基础用法,都不需要自己去找来看。本文更多的意义是记录一些没有提到但有可能会有帮助的注意事项。先列一下前期参考,推荐有意向自己构建的先看完
qBittorrent Official Wiki - Compilation with CMake: Debian
qBittorrent Official Wiki - Compilation with CMake: common information
qBittorrent Official Dockerfile
Arch Linux - PKGBUILD
下面正式开始

1
2
3
4
5
apt install build-essential cmake ninja-build pkg-config libboost-dev libssl-dev zlib1g-dev
# 相比于官方文档,去掉了 libgl1-mesa-dev,这个包 Debian 定义为过渡哑包,而且我实测构建 nox 不需要
# 相比于官方文档,去掉了 git,获取源码 wget 就行

apt install --no-install-recommends qtbase5-dev qttools5-dev libqt5svg5-dev

构建 libtorrent

Debian stable 源版本太低用不了,故需要自己构建,下载源码包解压到 /usr/local/src

1
2
3
4
5
6
cmake -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=/usr/local

cmake --build build
# 最好别加 -j 参数,我试过一次构建进度会卡住

cmake --install build

安装完成后记得

1
2
ldconfig
# configure dynamic linker run-time bindings

不然后面运行 qbittorrent-nox 大概率会报 #8047#3622,这两种情况我都遇到过,最初是通过执行下面第一条命令解决的,但后面我实测只要执行过 ldconfig 就不需要下面两条命令了

1
2
3
export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}
# or
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

构建 qbittorrent-nox

下载源码包解压到 /usr/local/src

1
2
3
4
5
6
7
8
9
10
cmake -G "Ninja" -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=/usr/local -DGUI=OFF -DSYSTEMD=ON
# -DGUI=OFF 意为不启用 GUI,即构建 nox (without X support)
# -DSYSTEMD=ON 使用 systemd 管理进程
# -DCMAKE_BUILD_TYPE=RelWithDebInfo,可以参考 https://stackoverflow.com/questions/48754619/what-are-cmake-build-type-debug-release-relwithdebinfo-and-minsizerel
# 如果使用 Debug,会大幅加快构建速度并在终端打印更多日志,不推荐在生产环境使用,建议保持默认

cmake --build build
# 同样最好别加 -j 参数,而且 qbittorrent-nox 构建速度比 libtorrent 快得多

cmake --install build

至此构建工作全部完成

运行 qbittorrent-nox

先创建一个普通的低权限用户,试运行 qbittorrent-nox

1
2
3
4
5
6
7
8
9
adduser qbtuser

su qbtuser

qbittorrent-nox
# 看到提示信息,按 y 同意,之后结束进程

usermod -s /usr/sbin/nologin qbtuser
# 去掉 qbtuser 的 shell 权限

之后使用 systemd 管理进程,此处不多赘述

1
2
3
4
5
6
systemctl daemon-reload

systemctl start qbittorrent-nox@qbtuser
systemctl status qbittorrent-nox@qbtuser
systemctl restart qbittorrent-nox@qbtuser
systemctl stop qbittorrent-nox@qbtuser

在软件成功运行后,可以使用 curl 来获取日志等信息

1
2
3
4
5
6
7
8
9
curl -i --header 'Referer: http://localhost:8080' --data 'username=admin&password=adminadmin' http://localhost:8080/api/v2/auth/login
# 先登录,获取 cookie
# 下面的命令必须使用 cookie 登录,直接使用 password 登录会报 403 Forbidden

curl http://localhost:8080/api/v2/app/version --cookie "SID="
# 获取版本号,

curl 'http://localhost:8080/api/v2/log/main?normal=true&info=true&warning=true&critical=true&last_known_id=-1' --cookie "SID="
# 获取日志

一些有用的信息

按照以上流程构建的 qbittorrent-nox,在此列出关于页面中的 build info,仅供参考

1
2
3
4
5
6
qBittorrent 4.4.5
Qt 5.15.2
Libtorrent 1.2.17.0
Boost 1.74.0
OpenSSL 1.1.1n
zlib 1.2.11