Skip to content

Part F. Install the hub, the research service and the tunnel on the server

Do this part only after the adapter pull requests (E.8) are merged and you have seen the E.6 test results. All commands run on the server over Tailscale SSH as your admin user, with sudo where shown. Never paste a secret into a chat; type or paste it only into the file on the server.

F.1 Install Node.js from the official download (checked by checksum)

Section titled “F.1 Install Node.js from the official download (checked by checksum)”

Debian 13’s own Node package is older than the adapter needs. We download the official build and check it, instead of piping a script into a shell.

  1. Open nodejs.org/en/download and read the version number of the current LTS release (Node 24 at the time of writing; Node 22 is the minimum). Write it into the first line below in place of the x.y.z.

    Terminal window
    uname -m
    NODE_VERSION=x.y.z
    cd /tmp
    curl -fsSLO https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz
    curl -fsSLO https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt
    grep " node-v$NODE_VERSION-linux-x64.tar.xz" SHASUMS256.txt | sha256sum -c -

VERIFY — you should see: x86_64 from uname, then “node-v…tar.xz: OK”. If uname prints aarch64, replace linux-x64 with linux-arm64 in all three lines. If the checksum line does not say OK, stop; delete the files and download again.

  1. Unpack it into /opt and link it:

    Terminal window
    sudo mkdir -p /opt/node
    sudo tar -xJf /tmp/node-v$NODE_VERSION-linux-x64.tar.xz -C /opt/node
    sudo ln -sfn /opt/node/node-v$NODE_VERSION-linux-x64 /opt/node/current
    sudo ln -sfn /opt/node/current/bin/node /usr/local/bin/node
    sudo ln -sfn /opt/node/current/bin/npm /usr/local/bin/npm
    node --version

VERIFY — you should see: the version you chose. To update Node later, repeat with the new number and re-point /opt/node/current; keep the old folder until the hub has run well on the new one.

F.2 Get the code (read-only key, fixed version)

Section titled “F.2 Get the code (read-only key, fixed version)”
  1. On the server make a read-only key for GitHub (no passphrase is acceptable here because the key can only read one repository and lives in a root-only folder):

    Terminal window
    sudo install -d -m 700 /root/.ssh
    sudo ssh-keygen -t ed25519 -N "" -C "bideward-lightsail-deploy" -f /root/.ssh/bideward_deploy
    sudo cat /root/.ssh/bideward_deploy.pub
  2. In GitHub open the bot-stats-server repository → Settings → Deploy keys → Add deploy key. Title “bideward-lightsail”; paste the public key (the line printed above, which is safe to paste); leave Allow write access unticked. Add key.

  3. Clone and pin to the release tag v1.0.0:

    Terminal window
    sudo GIT_SSH_COMMAND='ssh -i /root/.ssh/bideward_deploy -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new' git clone git@github.com:OWNER/bot-stats-server.git /opt/bideward/hub
    cd /opt/bideward/hub
    sudo git checkout v1.0.0
    sudo npm ci --omit=dev --ignore-scripts

OWNER is your GitHub user or organisation name. The folder stays owned by root, so the service user can read the code but never change it. –ignore-scripts stops any package from running code while installing.

  1. The research service lives in the same repository (research/). It uses the same checkout; it runs as its own user (F.6).
  2. To update later: sudo bideward-update NEWTAG (installed in F.5). It does, in order: fetch the tags with the read-only key; refuse a tag that does not exist or was never merged to main; maintenance mode on; check out the tag; npm ci --omit=dev --ignore-scripts; if the new release has database changes, take a dump first (a failed dump stops the update) and run the migrations as the database owner; restart the hub (and the research service when it changed); wait up to 60 seconds for the health answer ok; if it does not come, put the previous version back, restart, and send you a Telegram message; maintenance mode off. Every step is a line in /var/log/bideward-deploy.log and the result is in /var/lib/bideward-hub/last-deploy.json. sudo bideward-update --available names the newest tag on main that is not running. Never run git pull on a moving branch on this server. A rollback puts the old code back; database changes only go forward, and each one is written so the previous release still works with it.

F.3 The settings files (secrets live here and nowhere else)

Section titled “F.3 The settings files (secrets live here and nowhere else)”

Create each file empty with the right protection first, then fill it with nano. systemd reads these files as root before it hands the process to the limited user, so the service user never needs to read them.

Terminal window
sudo install -m 600 -o root -g root /dev/null /etc/bideward/hub.env
sudo install -m 600 -o root -g root /dev/null /etc/bideward/research.env
sudo install -m 600 -o root -g root /dev/null /etc/bideward/cloudflared.env
ls -l /etc/bideward

VERIFY — you should see: three files, mode -rw—–– owned by root.

/etc/bideward/hub.env: one NAME=value per line, no quotes, no spaces around =. The hub refuses to start if one of these four is missing: DASHBOARD_KEY, CONTROL_PASSWORD, DASHBOARD_OWNER_USER, DASHBOARD_OWNER_PASSWORD. A name that looks like ours but is not one the hub knows is printed as a warning (a typo guard) and ignored. Two optional names switch on the check of the research page by Cloudflare Access: ACCESS_TEAM and ACCESS_AUD (the team name and the application audience come from the Access application of Part D, step 7 of the research-page Access setup; the start-up log says “the Access check on /research is off” until you do).

Name Value
HOST and PORT 127.0.0.1 and 8080
PGHOST, PGDATABASE, PGUSER /var/run/postgresql, bideward, bideward_hub (no password: peer login through the Unix socket)
DASHBOARD_OWNER_USER and DASHBOARD_OWNER_PASSWORD The owner sign-in you choose now; a long passphrase, at least 16 characters. Used to create or reset the owner account.
CONTROL_PASSWORD A different passphrase from the sign-in password.
TELEGRAM_TOKEN and TELEGRAM_CHAT_ID The same Telegram bot you use today. Take the token from your password manager (the G.2 inventory); Cloudflare never shows a Worker secret again once it is set. If you no longer have it, make a new token with BotFather and revoke the old one.
GITHUB_TOKEN, GITHUB_OWNER, GITHUB_REPO The existing read-only token for the private script repository, and the owner and name of that repository.
BACKUP_URL and BACKUP_TOKEN The Apps Script web-app address and its key used by the backup today.
DASHBOARD_KEY The posting key the bots send today (the same value, so no bot changes).
Any other name The full list of names the receiver reads, with which are required and which are optional, is in Audit E1.
NODE_ENV production

/etc/bideward/research.env: HOST=127.0.0.1, PORT=8787, PGHOST=/var/run/postgresql, PGDATABASE=bideward, PGUSER=bideward_research, plus the research service’s own keys from its settings today (names from E1).

/etc/bideward/cloudflared.env: one line, TUNNEL_TOKEN=<the token copied in D.3>. Nothing else.

To open a file: sudo nano /etc/bideward/hub.env. Paste or type the lines, press Ctrl+O, Enter, Ctrl+X. Then check nothing leaked into your shell history: you typed no secret in a command, only inside nano.

  1. Add Cloudflare’s package source and install (this is Cloudflare’s documented method for Debian):

    Terminal window
    sudo mkdir -p --mode=0755 /usr/share/keyrings
    curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
    echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
    sudo apt-get update
    sudo apt-get install -y cloudflared
    cloudflared --version

Do not run cloudflared service install; we use our own protected unit so the token stays in the root-only file and not on a command line.

  1. A limited user for it:

    Terminal window
    sudo adduser --system --group --no-create-home --shell /usr/sbin/nologin cloudflared
  2. Create the unit file: sudo nano /etc/systemd/system/cloudflared-bideward.service and paste:

    Show the code (30 lines)
    [Unit]
    Description=Cloudflare Tunnel for Bideward
    After=network-online.target
    Wants=network-online.target
    [Service]
    Type=notify
    User=cloudflared
    Group=cloudflared
    EnvironmentFile=/etc/bideward/cloudflared.env
    ExecStart=/usr/bin/cloudflared tunnel --no-autoupdate --metrics 127.0.0.1:20241 run
    Restart=always
    RestartSec=5
    TimeoutStartSec=30
    MemoryMax=200M
    NoNewPrivileges=true
    ProtectSystem=strict
    ProtectHome=true
    PrivateTmp=true
    PrivateDevices=true
    ProtectKernelTunables=true
    ProtectKernelModules=true
    ProtectControlGroups=true
    RestrictNamespaces=true
    RestrictSUIDSGID=true
    LockPersonality=true
    CapabilityBoundingSet=
    SystemCallArchitectures=native
    UMask=0077
    [Install]
    WantedBy=multi-user.target

The token is read from the TUNNEL_TOKEN variable, so it never appears in the process list. –no-autoupdate leaves updates to apt (see Part K, monthly check).

The unit files are in the repository (deploy/). One command copies all of them, the OnFailure drop-ins (K.4) and the helper scripts into place, and enables nothing:

Terminal window
sudo /opt/bideward/hub/deploy/install.sh

VERIFY — you should see: a line for each of 7 units, 5 drop-ins and 5 scripts, then systemd reloaded and installed. Nothing was enabled or started. You can run it again after every update; it never touches /etc/bideward. The hub unit it installs is shown here so you can read it (do not paste it; the repository file is the one that counts):

Show the code (41 lines)
[Unit]
Description=Bideward hub (receiver and dashboard)
After=postgresql.service network-online.target
Wants=postgresql.service network-online.target
[Service]
Type=simple
User=bideward-hub
Group=bideward-hub
WorkingDirectory=/opt/bideward/hub
Environment=MAINTENANCE_FILE=/var/lib/bideward-hub/maintenance
EnvironmentFile=/etc/bideward/hub.env
ExecStart=/usr/local/bin/node --max-old-space-size=700 node/server.mjs
Restart=always
RestartSec=3
TimeoutStopSec=30
KillSignal=SIGTERM
MemoryMax=900M
LimitNOFILE=4096
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectControlGroups=true
ProtectClock=true
ProtectHostname=true
RestrictNamespaces=true
RestrictRealtime=true
RestrictSUIDSGID=true
LockPersonality=true
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
SystemCallFilter=@system-service
SystemCallArchitectures=native
CapabilityBoundingSet=
AmbientCapabilities=
ReadWritePaths=/var/lib/bideward-hub
UMask=0077
[Install]
WantedBy=multi-user.target

Why these lines: the hub can write only to /var/lib/bideward-hub; it has no extra privileges; it cannot touch the kernel or other users’ processes; its memory is capped so it can never starve PostgreSQL. The maintenance file named in the unit is the one bideward-maintenance on creates (Part O). Wants (not Requires) means a PostgreSQL restart does not kill the hub; the hub waits and reconnects. MemoryDenyWriteExecute is deliberately not set, because Node’s engine needs it. PrivateUsers is not set, because the database’s peer login depends on the real user id.

bideward-research.service is installed by the same install.sh command. It differs from the hub unit in: Description Bideward research service; User and Group bideward-research; EnvironmentFile /etc/bideward/research.env; ExecStart /usr/local/bin/node –max-old-space-size=300 research/main.mjs (the entry file found in the E1 audit); MemoryMax=400M; ReadWritePaths=/var/lib/bideward-research. Everything else is identical.

F.7 Create the tables, then start everything in order

Section titled “F.7 Create the tables, then start everything in order”
  1. Run the migrations as the database owner (your admin login is mapped to bideward_dba in B.4):

    Terminal window
    cd /opt/bideward/hub
    PGHOST=/var/run/postgresql PGDATABASE=bideward PGUSER=bideward_dba node db/migrate.mjs
    psql -U bideward_dba -d bideward -c "select id, applied_at from hub.schema_migrations order by id;"

VERIFY — you should see: applying 0001_layer1.sql, applying 0002_backups.sql, done: 2 applied, and the same two ids in the list. Running the command again prints nothing to apply (2 applied). If it says schema "hub" does not exist, the database setup of Part B (the create schema step) was not done.

  1. Load the services and start them one at a time, checking each:

    Terminal window
    sudo systemctl daemon-reload
    sudo systemctl enable --now bideward-hub
    sudo systemctl status bideward-hub --no-pager
    sudo journalctl -u bideward-hub -n 30 --no-pager
    curl -s http://127.0.0.1:8080/health

VERIFY — you should see: active (running), no error lines, a start-up line hub 1.0.0 listening on 127.0.0.1:8080, and the word ok from the health check. curl -s http://127.0.0.1:8080/health/detail (works only from the server itself) shows "status": "ok", "lock": {"held": true}, "database": {"ok": true} and an empty write queue. If the log says “the tables hub.kv / hub.alarms are missing”, step 1 was skipped; if it says the lock is held, a second hub is running (check systemctl status bideward-hub).

  1. Then the research service and the tunnel:

    Terminal window
    sudo systemctl enable --now bideward-research
    sudo systemctl enable --now cloudflared-bideward
    sudo systemctl status cloudflared-bideward --no-pager

VERIFY — you should see: the tunnel active, and in the Cloudflare dashboard (Zero Trust → Networks → Tunnels) bideward-lightsail turns Healthy.

  1. Open https://app.bideward.com in your browser. You should see the dashboard’s logo sign-in screen. Nobody can sign in yet if the owner variables are empty; with them set, sign in as the owner.

F.8 Ports and what listens where (check after every change)

Section titled “F.8 Ports and what listens where (check after every change)”
Port Who Reachable from
8080 bideward-hub 127.0.0.1 only; the tunnel connects here
8787 bideward-research 127.0.0.1 only; the hub forwards /research here
20241 cloudflared metrics 127.0.0.1 only
5432 PostgreSQL Nothing on TCP; Unix socket only
22 sshd The Tailscale interface only (ufw rule from A.9)
Anything else Nothing Must not exist

Check with: sudo ss -ltnup. Every listening line must be one of the rows above. Anything else is a mistake to fix before going on.


← Previous: Part E. The Node adapter: what will be built · Guide index · Next: Part G. Move the data from the Worker to the new database →