Skip to content

Commit 8986c85

Browse files
Add interactive Linux setup wizard (PM2 + nginx + certbot)
scripts/setup-wizard.sh: a guided installer for a dedicated Debian/Ubuntu server. It asks for topology (two subdomains / single domain+/api / local), domains, admin account, database, storage path and limits, then: - installs prerequisites (Node via NodeSource, pnpm, PM2, PostgreSQL, ClamAV, nginx, certbot) on request, - provisions a local PostgreSQL role + database (idempotent), - generates MASTER_KEY / SESSION_SECRET and writes a 0600 .env, - runs scripts/deploy.sh (install/build/migrate/seed), - writes and enables an nginx reverse-proxy vhost (large client_max_body_size, request buffering off for streaming uploads), - obtains a Let's Encrypt certificate via certbot --nginx, - starts and saves the PM2 processes. Hardening of the env plumbing: - deploy.sh no longer sources .env; it reads needed values literally, so secrets with shell metacharacters can't be executed. - the wizard writes .env via printf and double-quotes free-text secrets so dotenv preserves them verbatim (e.g. '#' is not treated as an inline comment). - ecosystem.config.cjs reads .env itself so WEB_PORT/WEB_HOST are honoured regardless of how PM2 is started (web binds 127.0.0.1 behind nginx). Docs: wizard documented as the recommended install path in README + DEPLOYMENT.
1 parent 022f6ef commit 8986c85

5 files changed

Lines changed: 511 additions & 15 deletions

File tree

‎README.md‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,21 @@ openssl rand -base64 32
8282
See [`docs/DEPLOYMENT.md`](./docs/DEPLOYMENT.md) for production setup behind a reverse
8383
proxy with TLS (e.g. `copper.forgenet.fr`).
8484

85-
### Quick start (bare-metal / PM2)
85+
### One-command install (dedicated server)
8686

87-
Prefer running it natively on a dedicated server? Provide your own PostgreSQL, then:
87+
On a fresh Debian/Ubuntu box, the interactive wizard installs prerequisites and sets up
88+
PostgreSQL, `.env` (with generated secrets), the build, PM2, an nginx reverse proxy and a
89+
Let's Encrypt certificate:
90+
91+
```bash
92+
git clone https://github.com/softpython2884/opencoperlock.git
93+
cd opencoperlock
94+
bash scripts/setup-wizard.sh
95+
```
96+
97+
### Quick start (bare-metal / PM2, manual)
98+
99+
Prefer to wire it up yourself? Provide your own PostgreSQL, then:
88100

89101
```bash
90102
cp .env.example .env # edit secrets + paths

‎docs/DEPLOYMENT.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ docker compose -f infra/docker-compose.yml up --build -d
8989
Migrations are applied automatically on API start (`prisma migrate deploy`). The seed is
9090
idempotent and never overwrites an existing admin.
9191

92+
## Guided install (recommended for a dedicated server)
93+
94+
The interactive wizard does the entire bare-metal install for you on Debian/Ubuntu —
95+
prerequisites, PostgreSQL, `.env` with generated secrets, build, PM2, nginx and TLS:
96+
97+
```bash
98+
bash scripts/setup-wizard.sh
99+
```
100+
101+
It asks for your domain(s), admin account, storage path and limits, then offers to
102+
install Node/pnpm/PM2/PostgreSQL/nginx/certbot as needed. Secrets (`MASTER_KEY`,
103+
`SESSION_SECRET`, DB password) are generated for you and written to a `0600` `.env`.
104+
Re-run it any time; it backs up an existing `.env` first. The sections below document
105+
the same steps manually.
106+
92107
## Bare-metal with PM2 (dedicated server)
93108

94109
For a dedicated Linux box you can run the two Node processes under

‎ecosystem.config.cjs‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,40 @@
66
* pm2 start ecosystem.config.cjs
77
* pm2 save && pm2 startup # persist across reboots
88
*
9-
* Secrets live in the repo-root `.env` (copied from `.env.example`). The API loads that
10-
* file itself (see apps/api/src/config/dotenv.ts), so no secrets are embedded here.
11-
* The web process only needs PORT/HOSTNAME — its API URL is baked at build time via
12-
* NEXT_PUBLIC_API_URL (see scripts/deploy.sh and docs/DEPLOYMENT.md).
9+
* Secrets live in the repo-root `.env` (copied from `.env.example`). The API process
10+
* loads that file itself (apps/api/src/config/dotenv.ts). For the web process we read a
11+
* couple of values here so PORT/HOSTNAME are honoured no matter how PM2 is invoked.
1312
*/
13+
const fs = require('node:fs');
1414
const path = require('node:path');
1515
const root = __dirname;
1616

17+
/** Minimal, dependency-free .env reader (KEY=VALUE, ignores comments/blank lines). */
18+
function readEnv(file) {
19+
const out = {};
20+
try {
21+
for (const raw of fs.readFileSync(file, 'utf8').split('\n')) {
22+
const line = raw.trim();
23+
if (!line || line.startsWith('#')) continue;
24+
const eq = line.indexOf('=');
25+
if (eq === -1) continue;
26+
let value = line.slice(eq + 1).trim();
27+
if (
28+
(value.startsWith('"') && value.endsWith('"')) ||
29+
(value.startsWith("'") && value.endsWith("'"))
30+
) {
31+
value = value.slice(1, -1);
32+
}
33+
out[line.slice(0, eq).trim()] = value;
34+
}
35+
} catch {
36+
/* no .env yet — fall back to defaults below */
37+
}
38+
return out;
39+
}
40+
41+
const env = readEnv(path.join(root, '.env'));
42+
1743
module.exports = {
1844
apps: [
1945
{
@@ -34,9 +60,9 @@ module.exports = {
3460
exec_mode: 'fork',
3561
env: {
3662
NODE_ENV: 'production',
37-
PORT: process.env.WEB_PORT || 3000,
38-
// Bind all interfaces by default; set WEB_HOST=127.0.0.1 when behind a reverse proxy.
39-
HOSTNAME: process.env.WEB_HOST || '0.0.0.0',
63+
PORT: env.WEB_PORT || process.env.WEB_PORT || 3000,
64+
// Bound to localhost when WEB_HOST=127.0.0.1 (set by the wizard when behind nginx).
65+
HOSTNAME: env.WEB_HOST || process.env.WEB_HOST || '0.0.0.0',
4066
},
4167
max_memory_restart: '512M',
4268
},

‎scripts/deploy.sh‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,27 @@ if [[ ! -f .env ]]; then
1717
exit 1
1818
fi
1919

20-
# Load .env so prisma (migrate) and the seed see DATABASE_URL, ADMIN_*, etc.
21-
set -a
22-
# shellcheck disable=SC1091
23-
. ./.env
24-
set +a
20+
# Read a single value from .env WITHOUT sourcing it, so secrets containing shell
21+
# metacharacters (spaces, $, backticks, quotes) are handled literally and safely.
22+
read_env() {
23+
local key="$1" line val
24+
line="$(grep -E "^${key}=" .env | tail -1 || true)"
25+
val="${line#*=}"
26+
# strip one layer of surrounding quotes if present
27+
[[ "$val" == \"*\" ]] && val="${val:1:${#val}-2}"
28+
[[ "$val" == \'*\' ]] && val="${val:1:${#val}-2}"
29+
printf '%s' "$val"
30+
}
31+
32+
# Export only what the build/migrate steps need (seed loads .env itself via dotenv).
33+
export DATABASE_URL="$(read_env DATABASE_URL)"
34+
export STORAGE_PATH="$(read_env STORAGE_PATH)"
35+
export QUARANTINE_PATH="$(read_env QUARANTINE_PATH)"
36+
[[ -n "$DATABASE_URL" ]] || { echo "ERROR: DATABASE_URL is not set in .env" >&2; exit 1; }
2537

2638
# The browser bundle needs the *public* API URL baked in at build time.
27-
: "${NEXT_PUBLIC_API_URL:=${APP_API_URL:-http://localhost:4000}}"
39+
NEXT_PUBLIC_API_URL="$(read_env NEXT_PUBLIC_API_URL)"
40+
: "${NEXT_PUBLIC_API_URL:=http://localhost:4000}"
2841
export NEXT_PUBLIC_API_URL
2942
echo "==> Building web with NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}"
3043

0 commit comments

Comments
 (0)