source: Klonkt/scripts/install.sh@ 83088b66

main
Last change on this file since 83088b66 was 83088b66, checked in by Robin Genis <roboburr@…>, 3 months ago

feat: install.sh ready for public repo (roboburr/klonkt)

  • real GitHub URL filled in (was OWNER placeholder)
  • outdated WebSocket/Upgrade proxy hints removed (Prutter/WS has been removed)

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 10.6 KB
Line 
1#!/usr/bin/env bash
2#
3# Klonkt — installer voor een Debian/Ubuntu VPS.
4# Installeert Node 20, Caddy (automatische HTTPS) en Klonkt als systemd-service.
5#
6# Veilig op een server die AL iets draait: upgradet je systeem-Node niet,
7# kiest automatisch een vrije poort, en slaat Caddy over als er al een
8# webserver/reverse-proxy op poort 80/443 luistert (dan krijg je instructies
9# om Klonkt achter je eigen proxy te zetten).
10#
11# Gebruik (als root), niet-interactief:
12# curl -fsSL https://raw.githubusercontent.com/roboburr/klonkt/main/scripts/install.sh \
13# | sudo bash -s -- --domain klonkt.voorbeeld.nl
14# Of interactief vanaf een gedownload bestand:
15# sudo bash install.sh
16#
17# Opnieuw draaien op dezelfde server = bijwerken (git pull + herstart).
18# Volledig geïsoleerd alternatief: Docker (zie docker-compose.yml in de repo).
19#
20set -euo pipefail
21
22# ── Instellingen (override via env-variabele of vlag) ──────────────────────
23KLONKT_REPO="${KLONKT_REPO:-https://github.com/roboburr/klonkt.git}" # TODO: echte GitHub-URL
24KLONKT_BRANCH="${KLONKT_BRANCH:-main}"
25KLONKT_DIR="${KLONKT_DIR:-/opt/klonkt}"
26KLONKT_USER="${KLONKT_USER:-klonkt}"
27KLONKT_PORT="${KLONKT_PORT:-3000}"
28KLONKT_DOMAIN="${KLONKT_DOMAIN:-}"
29KLONKT_LANG="${KLONKT_DEFAULT_LANG:-}"
30NODE_MAJOR="${NODE_MAJOR:-20}"
31NO_CADDY="${KLONKT_NO_CADDY:-}" # zet op 1 om Caddy NOOIT te installeren (eigen proxy)
32NODE_FORCE="${NODE_FORCE:-}" # zet op 1 om systeem-Node tóch te (her)installeren
33PORT_EXPLICIT=0
34
35while [ $# -gt 0 ]; do
36 case "$1" in
37 --domain) KLONKT_DOMAIN="$2"; shift 2;;
38 --repo) KLONKT_REPO="$2"; shift 2;;
39 --branch) KLONKT_BRANCH="$2"; shift 2;;
40 --dir) KLONKT_DIR="$2"; shift 2;;
41 --port) KLONKT_PORT="$2"; PORT_EXPLICIT=1; shift 2;;
42 --lang) KLONKT_LANG="$2"; shift 2;;
43 --no-caddy) NO_CADDY=1; shift;;
44 --force-node) NODE_FORCE=1; shift;;
45 -h|--help) grep '^#' "$0" | sed 's/^# \{0,1\}//'; exit 0;;
46 *) echo "Onbekende optie: $1" >&2; exit 1;;
47 esac
48done
49
50log() { printf '\n\033[1;33m▸ %s\033[0m\n' "$*"; }
51ok() { printf '\033[1;32m ✓ %s\033[0m\n' "$*"; }
52warn() { printf '\033[1;33m ! %s\033[0m\n' "$*"; }
53die() { printf '\033[1;31m✗ %s\033[0m\n' "$*" >&2; exit 1; }
54as_klonkt() { runuser -u "$KLONKT_USER" -- env HOME="$KLONKT_DIR" "$@"; }
55port_busy() { ss -ltnH 2>/dev/null | awk '{print $4}' | grep -qE "[:.]${1}$"; }
56
57[ "$(id -u)" = 0 ] || die "Draai dit als root (sudo bash install.sh)."
58command -v apt-get >/dev/null || die "Alleen Debian/Ubuntu (apt). Gebruik op andere systemen de Docker-route."
59
60if [ -z "$KLONKT_DOMAIN" ]; then
61 read -rp "Domein voor Klonkt (bv. klonkt.voorbeeld.nl): " KLONKT_DOMAIN </dev/tty || true
62fi
63[ -n "$KLONKT_DOMAIN" ] || die "Geen domein opgegeven (--domain of KLONKT_DOMAIN)."
64case "$KLONKT_REPO" in
65 *OWNER/*) die "Zet eerst de echte repo-URL: --repo https://github.com/<jij>/klonkt.git (of KLONKT_REPO=...).";;
66esac
67
68export DEBIAN_FRONTEND=noninteractive
69
70# ── Preflight: kijk wat er al draait, pas je aan i.p.v. clobberen ──────────
71log "Preflight (wat draait er al?)…"
72apt-get update -y >/dev/null
73apt-get install -y iproute2 >/dev/null 2>&1 || true
74
75# Poort: bezet? Bij --port → fout. Anders automatisch een vrije kiezen.
76if port_busy "$KLONKT_PORT"; then
77 if [ "$PORT_EXPLICIT" = 1 ]; then
78 die "Poort ${KLONKT_PORT} is al in gebruik. Kies een vrije poort met --port."
79 fi
80 picked=""
81 for p in $(seq "$KLONKT_PORT" $((KLONKT_PORT+30))); do
82 port_busy "$p" || { picked="$p"; break; }
83 done
84 [ -n "$picked" ] || die "Geen vrije poort gevonden rond ${KLONKT_PORT}. Geef er een met --port."
85 warn "poort ${KLONKT_PORT} bezet → Klonkt gebruikt ${picked}"
86 KLONKT_PORT="$picked"
87else
88 ok "poort ${KLONKT_PORT} vrij"
89fi
90
91# Webserver op 80/443 die niet Caddy is? → Caddy overslaan, eigen-proxy-modus.
92FOREIGN_PROXY=0
93if [ -z "$NO_CADDY" ] && command -v ss >/dev/null 2>&1; then
94 if ss -ltnpH 2>/dev/null | grep -E '[:.](80|443) ' | grep -viq 'caddy'; then
95 NO_CADDY=1; FOREIGN_PROXY=1
96 warn "er luistert al iets op poort 80/443 (geen Caddy) → ik installeer Caddy NIET en geef je proxy-instructies"
97 fi
98fi
99
100# ── Node: bestaande versie respecteren, niet stilletjes upgraden ──────────
101log "Node ${NODE_MAJOR}.x…"
102if command -v node >/dev/null 2>&1 && [ -z "$NODE_FORCE" ]; then
103 CUR="$(node -v | sed 's/v//;s/\..*//')"
104 if [ "$CUR" -lt "$NODE_MAJOR" ]; then
105 die "Er staat al Node $(node -v) op deze server; Klonkt heeft ≥${NODE_MAJOR} nodig.
106 Ik upgrade je systeem-Node NIET automatisch — dat kan andere apps breken.
107 Opties: (a) gebruik de Docker-route (eigen Node, raakt niets aan), of
108 (b) upgrade Node zelf, of (c) forceer met NODE_FORCE=1 (eigen risico)."
109 fi
110 ok "bestaande node $(node -v) wordt gebruikt"
111else
112 curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash -
113 apt-get install -y nodejs
114 ok "node $(node -v) geïnstalleerd"
115fi
116
117log "Overige pakketten…"
118apt-get install -y curl ca-certificates git gnupg openssl build-essential python3
119apt-get install -y webp >/dev/null 2>&1 || true # cwebp = afbeelding→WebP (optioneel)
120ok "basis-pakketten"
121
122if [ -z "$NO_CADDY" ]; then
123 log "Caddy (reverse proxy + auto-HTTPS)…"
124 if ! command -v caddy >/dev/null 2>&1; then
125 curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
126 curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' > /etc/apt/sources.list.d/caddy-stable.list
127 apt-get update -y
128 apt-get install -y caddy
129 fi
130 ok "caddy aanwezig"
131fi
132
133log "Service-gebruiker '${KLONKT_USER}'…"
134id -u "$KLONKT_USER" >/dev/null 2>&1 || useradd --system --home-dir "$KLONKT_DIR" --shell /usr/sbin/nologin "$KLONKT_USER"
135ok "gebruiker"
136
137log "Klonkt-broncode ophalen…"
138if [ -d "$KLONKT_DIR/.git" ]; then
139 git -C "$KLONKT_DIR" remote set-url origin "$KLONKT_REPO"
140 git -C "$KLONKT_DIR" fetch --depth 1 origin "$KLONKT_BRANCH"
141 git -C "$KLONKT_DIR" reset --hard "origin/$KLONKT_BRANCH"
142else
143 [ -e "$KLONKT_DIR" ] && [ -n "$(ls -A "$KLONKT_DIR" 2>/dev/null)" ] && die "$KLONKT_DIR bestaat al en is geen git-checkout. Kies --dir, of ruim 'm op."
144 mkdir -p "$KLONKT_DIR"
145 git clone --depth 1 --branch "$KLONKT_BRANCH" "$KLONKT_REPO" "$KLONKT_DIR"
146fi
147mkdir -p "$KLONKT_DIR/storage/media" "$KLONKT_DIR/storage/audio"
148chown -R "$KLONKT_USER:$KLONKT_USER" "$KLONKT_DIR"
149ok "code in $KLONKT_DIR"
150
151log "Dependencies installeren (npm ci)…"
152as_klonkt bash -c "cd '$KLONKT_DIR' && npm ci --omit=dev"
153ok "node_modules"
154
155log ".env…"
156ENV="$KLONKT_DIR/.env"
157if [ ! -f "$ENV" ]; then
158 SECRET="$(openssl rand -hex 32)"
159 {
160 echo "NODE_ENV=production"
161 echo "PORT=${KLONKT_PORT}"
162 echo "SESSION_SECRET=${SECRET}"
163 echo "DATABASE_PATH=./storage/database.sqlite"
164 echo "MEDIA_PATH=./storage/media"
165 echo "AUDIO_PATH=./storage/audio"
166 echo "PUBLIC_BASE_URL=https://${KLONKT_DOMAIN}"
167 [ -n "$KLONKT_LANG" ] && echo "KLONKT_DEFAULT_LANG=${KLONKT_LANG}"
168 } > "$ENV"
169 chown "$KLONKT_USER:$KLONKT_USER" "$ENV"; chmod 600 "$ENV"
170 ok "nieuwe .env (willekeurige SESSION_SECRET)"
171else
172 # poort in bestaande .env synchroniseren met de gekozen poort
173 if grep -q '^PORT=' "$ENV"; then sed -i "s/^PORT=.*/PORT=${KLONKT_PORT}/" "$ENV"; fi
174 ok "bestaande .env behouden (poort gesynchroniseerd)"
175fi
176
177log "systemd-service…"
178NODE_BIN="$(command -v node)"
179cat > /etc/systemd/system/klonkt.service <<EOF
180[Unit]
181Description=Klonkt
182After=network-online.target
183Wants=network-online.target
184
185[Service]
186Type=simple
187User=${KLONKT_USER}
188WorkingDirectory=${KLONKT_DIR}
189ExecStart=${NODE_BIN} src/server.js
190Environment=NODE_ENV=production
191Restart=always
192RestartSec=3
193NoNewPrivileges=true
194ProtectSystem=full
195PrivateTmp=true
196
197[Install]
198WantedBy=multi-user.target
199EOF
200systemctl daemon-reload
201systemctl enable --now klonkt
202ok "klonkt.service draait op 127.0.0.1:${KLONKT_PORT}"
203
204if [ -z "$NO_CADDY" ]; then
205 log "Caddy-config voor ${KLONKT_DOMAIN}…"
206 CADDY=/etc/caddy/Caddyfile
207 SITE_BLOCK="${KLONKT_DOMAIN} {
208 reverse_proxy 127.0.0.1:${KLONKT_PORT}
209 encode gzip zstd
210}"
211 touch "$CADDY"
212 if grep -q '/usr/share/caddy' "$CADDY"; then
213 cp "$CADDY" "${CADDY}.bak.$(date +%s)"
214 printf '%s\n' "$SITE_BLOCK" > "$CADDY"
215 elif ! grep -q "^${KLONKT_DOMAIN} {" "$CADDY"; then
216 printf '\n%s\n' "$SITE_BLOCK" >> "$CADDY"
217 fi
218 caddy validate --config "$CADDY" --adapter caddyfile >/dev/null 2>&1 || die "Caddy-config ongeldig — controleer $CADDY"
219 systemctl reload caddy 2>/dev/null || systemctl restart caddy
220 ok "caddy serveert ${KLONKT_DOMAIN}"
221fi
222
223log "Update-commando 'klonkt-update'…"
224cat > /usr/local/bin/klonkt-update <<EOF
225#!/usr/bin/env bash
226set -euo pipefail
227D="${KLONKT_DIR}"
228B=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD 2>/dev/null || true)
229runuser -u ${KLONKT_USER} -- git -C "\$D" fetch --depth 1 origin ${KLONKT_BRANCH}
230runuser -u ${KLONKT_USER} -- git -C "\$D" reset --hard origin/${KLONKT_BRANCH}
231A=\$(runuser -u ${KLONKT_USER} -- git -C "\$D" rev-parse HEAD)
232if ! runuser -u ${KLONKT_USER} -- git -C "\$D" diff --quiet "\$B" "\$A" -- package-lock.json 2>/dev/null; then
233 runuser -u ${KLONKT_USER} -- env HOME="\$D" bash -c "cd '\$D' && npm ci --omit=dev"
234fi
235systemctl restart klonkt
236echo "Klonkt bijgewerkt (\$A) + herstart."
237EOF
238chmod +x /usr/local/bin/klonkt-update
239ok "klonkt-update"
240
241echo
242echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
243echo " Klonkt draait! 🎉"
244echo
245if [ -n "$NO_CADDY" ]; then
246 echo " Klonkt luistert op: http://127.0.0.1:${KLONKT_PORT}"
247 if [ "$FOREIGN_PROXY" = 1 ]; then
248 echo " Er draait al een webserver op 80/443 — zet Klonkt erachter."
249 fi
250 echo " Voorbeeld nginx:"
251 echo " location / { proxy_pass http://127.0.0.1:${KLONKT_PORT}; proxy_set_header Host \$host;"
252 echo " proxy_set_header X-Forwarded-Proto \$scheme; }"
253 echo " Voorbeeld Caddy:"
254 echo " ${KLONKT_DOMAIN} { reverse_proxy 127.0.0.1:${KLONKT_PORT} }"
255else
256 echo " • Open je site: https://${KLONKT_DOMAIN}"
257fi
258echo " • Eerste keer: ga naar /auth/register en maak je beheerdersaccount aan."
259echo
260echo " Beheer: systemctl status klonkt · journalctl -u klonkt -f · klonkt-update"
261echo " Wachtwoord kwijt: cd ${KLONKT_DIR} && runuser -u ${KLONKT_USER} -- env HOME=${KLONKT_DIR} npm run reset-admin"
262echo
263echo " DNS: zorg dat A + AAAA van ${KLONKT_DOMAIN} naar deze server wijzen."
264echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
Note: See TracBrowser for help on using the repository browser.