Anchr 安装:Ubuntu 22.04 源码构建与 systemd 服务化
Anchr 3.5.2 在 Ubuntu 22.04 的源码构建部署实录:Node.js 22 + MongoDB 6.0 环境、Bower/Grunt 前端构建、.env 初始化与 systemd 服务化,含 MongoDB 安装假死等排障实录。
Anchr 安装:Ubuntu 22.04 源码构建与 systemd 服务化
实测基线:Ubuntu 22.04.5 LTS(jammy)+ Anchr 3.5.2 + Node.js v22.23.4 + MongoDB 6.0.29,所有命令与回显均来自 2026-10-28 的真实部署过程。官方 Releases 无预编译二进制(assets 为 0),本篇走 README "Run Natively" 的源码构建路线。
一、安装前规划
1.1 环境要求与端口规划
| 项 | 要求 | 本篇取值 |
|---|---|---|
| 操作系统 | Ubuntu 22.04 LTS(推荐 22.04.5) | Ubuntu 22.04.5 LTS |
| 硬件 | 2 核 / 2GB 内存 / 10GB 磁盘起 | 2C4G |
| Node.js | 3.5.2 要求 >= 21,生产建议 22 LTS | v22.23.4(NodeSource) |
| 数据库 | MongoDB >= 6.x | 6.0.29(官方 apt 仓库) |
| 应用端口 | 默认 3000,可改 | 8149 |
| 安装目录 | 任意,建议 /opt/anchr | /opt/anchr |
| 附件目录 | 必须存在,需写入权限 | /var/data/anchr |
| 日志目录 | 必须存在 | /var/log/anchr |
部署前先确认目标端口未被占用:
ss -ltn | grep 8149
预期结果:无任何输出(端口空闲)。若有输出说明已被占用,请更换 PORT 取值。
1.2 安装路径说明
Anchr 3.5.2 的官方原生运行方式只有一条:克隆源码 → 安装依赖(npm + Bower)→ 构建前端(Grunt)→ NODE_ENV=production node app.js。整条链路没有任何编译型构建,装完即可运行;本篇在最后把它交给 systemd 托管,实现开机自启与崩溃自动拉起。
二、安装系统依赖
2.1 基础工具
apt update
apt install -y curl git ca-certificates gnupg openssl
实测回显(节选):
Setting up git (1:2.34.1-1ubuntu1.15) ...
验证:
git --version
git version 2.34.1
2.2 安装 Node.js 22 LTS(NodeSource 源)
curl -fsSL https://deb.nodesource.com/setup_22.x -o /tmp/nodesetup.sh
bash /tmp/nodesetup.sh
apt install -y nodejs
node -v && npm -v
实测回显:
Unpacking nodejs (22.23.4-1nodesource1) ...
Setting up nodejs (22.23.4-1nodesource1) ...
v22.23.4
10.9.9
排障实录:如果机器访问 deb.nodesource.com 缓慢,可为 apt/curl 配置
http_proxy/https_proxy环境变量后重试;NodeSource 脚本本身只写 apt 源,不下载大文件。
2.3 安装 MongoDB 6.0(官方 apt 仓库)
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | gpg --dearmor -o /usr/share/keyrings/mongodb-server-6.0.gpg
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-6.0.list
export DEBIAN_FRONTEND=noninteractive
apt update && apt install -y mongodb-org
mongod --version | head -2
实测回显:
Setting up mongodb-org (6.0.29) ...
db version v6.0.29
Build Info: {
排障实录 ①(必看):交互式安装 MongoDB 时,tzdata 包的 debconf 前端会卡在时区询问上导致安装"假死"(
/usr/bin/perl debconf/frontend tzdata.config configure挂住不动)。上面命令中的export DEBIAN_FRONTEND=noninteractive就是解法;如果已经卡住,找到挂起的tzdata.config进程kill -9后执行dpkg --configure -a补完配置即可。排障实录 ②:若 mongod 启动报
status=217/USER(Failed to determine user credentials),说明 mongodb-org 包只下载了一半、mongodb系统用户未创建。执行dpkg --configure -a让 postinst 跑完,确认getent passwd mongodb有输出后再启动服务。
2.4 启动 MongoDB 并创建应用账号
systemctl enable --now mongod
systemctl is-active mongod
active
mongosh --quiet --eval '
db = db.getSiblingDB("anchr");
db.createUser({user:"anchr", pwd:"q1w2e3r4", roles:[{role:"dbOwner", db:"anchr"}]});
db.getUsers().users.forEach(u => print("user:", u.user));
'
实测回显:
user: anchr
q1w2e3r4仅为示例口令,生产环境请替换为强口令并妥善保管。Anchr 应用侧将使用这对凭据连接数据库。
三、获取源码并构建
3.1 克隆 3.5.2 源码
git clone --depth 1 --branch 3.5.2 https://github.com/muety/anchr.git /opt/anchr
cd /opt/anchr && ls
实测回显:
Dockerfile LICENSE README.md app app.js config env.bat env.sh
lib package.json public scripts telegram testing utils.js webext yarn.lock
校验版本(提交 7138354 即 3.5.2 的构建):
cd /opt/anchr && git log --oneline -1
7138354 fix: collection items count with cache (resolve #88)
3.2 安装后端与构建依赖
cd /opt/anchr && npm install 2>&1 | tail -3
实测回显(依赖数十至上百个包,耗时约 1~3 分钟):
added 233 packages, and audited 234 packages in 47s
Run `npm audit` for details.
3.3 安装 Bower 并拉取前端依赖
前端依赖由 Bower 管理,其中 bootstrap-material-design 的源地址是 git:// 协议,而代码托管平台已停用该明文协议,直接安装会失败。先做协议改写再安装:
npm install -g bower
git config --global url."https://".insteadOf "git://"
cd /opt/anchr/public && bower install --allow-root 2>&1 | tail -4
实测回显:
added 1 package in 4s
└── angular#1.7.9
jquery#2.1.4 bower_components/jquery
clipboard#1.5.16 bower_components/clipboard
排障实录 ③:若跳过协议改写,Bower 会在拉取
bootstrap-material-design时长时间无响应或报 git 连接错误;git config --global url."https://".insteadOf "git://"一行即可根治。--allow-root是因为生产部署以 root 或系统用户执行,Bower 默认拒绝 root 运行。
3.4 构建前端生产包
cd /opt/anchr && npm run build 2>&1 | tail -5
实测回显:
loading grunt-useminify 12ms ▇ 2%
concat:generated 11ms ▇ 2%
copy:dist 8ms ▇ 1%
usemin:html 21ms ▇ 3%
Total 703ms
构建产物在 /opt/anchr/public/dist/,生产模式下应用直接以该目录作为站点根:
ls /opt/anchr/public/dist/
404.html fonts images index.html robots.txt scripts styles views
四、初始化运行环境
4.1 创建目录与 .env 配置
mkdir -p /var/data/anchr /var/log/anchr
cat > /opt/anchr/.env <<EOF
PORT=8149
LISTEN_ADDR=0.0.0.0
ANCHR_PUBLIC_URL=http://<服务器IP>:8149
ANCHR_DB_USER=anchr
ANCHR_DB_PASSWORD=q1w2e3r4
ANCHR_DB_HOST=localhost
ANCHR_DB_PORT=27017
ANCHR_DB_NAME=anchr
ANCHR_UPLOAD_DIR=/var/data/anchr
ANCHR_SECRET=<openssl rand -hex 16 生成的随机串>
ANCHR_ALLOW_SIGNUP=true
ANCHR_VERIFY_USERS=false
ANCHR_EXPOSE_METRICS=false
EOF
.env每个变量的含义在 C1 篇逐项详解;ANCHR_SECRET生成方式:openssl rand -hex 16。ANCHR_PUBLIC_URL必须写成浏览器可访问的完整地址(不带结尾斜杠),否则短链、图片的返回链接会指向错误域名。
4.2 首次前台启动验证
cd /opt/anchr && source env.sh && NODE_ENV=production node app.js
实测回显:
[WARN] Disabling safe browse lookups for shortlinks as "ANCHR_GOOGLE_API_KEY" config variable is missing.
Successfully established database connection.
Express server listening on 0.0.0.0:8149
Updating UBlockBadwareChecker
Updating PhishingArmyChecker
另开终端验证首页与 API:
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1:8149/
HTTP 200
两条 WARN 提示属正常现象:未配置 Google API Key 时恶意链接检测自动降级关闭。验证通过后按
Ctrl+C停止前台进程,进入服务化步骤。
五、systemd 服务化与开机自启
5.1 创建专用系统用户
useradd -r -M -s /usr/sbin/nologin -d /opt/anchr anchr
chown -R anchr:anchr /opt/anchr /var/data/anchr /var/log/anchr
5.2 安装服务单元
cat > /etc/systemd/system/anchr.service <<'EOF'
[Unit]
Description=Anchr self-hosted bookmarks toolbox
After=network-online.target mongod.service
Wants=network-online.target
[Service]
Type=simple
User=anchr
Group=anchr
WorkingDirectory=/opt/anchr
Environment=NODE_ENV=production
EnvironmentFile=/opt/anchr/.env
ExecStart=/usr/bin/node app.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now anchr
实测回显:
Created symlink /etc/systemd/system/multi-user.target.wants/anchr.service → /etc/systemd/system/anchr.service.
5.3 验证服务状态
systemctl is-active anchr
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://127.0.0.1:8149/
active
HTTP 200
浏览器访问 http://<服务器IP>:8149,看到 "Anchr.io - Open-Source Bookmarks Manager" 标题的 Angular 页面即安装成功。
六、注册首个账号
页面右上角 Sign Up,或直接调用注册接口(邮箱 + 口令本地注册):
curl -s -X POST http://127.0.0.1:8149/api/auth/signup \
-H 'Content-Type: application/json' \
-d '{"email":"admin@example.com","password":"q1w2e3r4"}' \
-w '\nHTTP %{http_code}\n'
实测回显:
HTTP 201
示例账号统一为
admin@example.com/q1w2e3r4。注册成功即可在页面登录;登录态为 JWT(有效期 7 天,由ANCHR_SECRET签名)。
七、安装后检查清单
| 检查项 | 命令 | 预期 |
|---|---|---|
| MongoDB | systemctl is-active mongod | active |
| Anchr 服务 | systemctl is-active anchr | active |
| 首页 | curl -I http://127.0.0.1:8149/ | HTTP/1.1 200 OK |
| 数据库连接 | `journalctl -u anchr -n 20 | grep "database connection"` |
| 注册登录 | 页面 Sign Up → Login | 进入工作台 |
后续动作:
.env全参数逐项说明见 《Anchr 配置:.env 全参数与 systemd 单元详解》;- 日常启停、备份恢复、版本升级见 《Anchr 命令运维:服务管理、备份恢复与版本升级》;
- 还在犹豫要不要装?先看 《Anchr 认知选型:三合一轻量书签工具箱的定位、活跃度与替代方案》。