Skip to content

Commit 022f6ef

Browse files
Add bare-metal / PM2 deployment support
Run OpenCoperLock natively on a dedicated Linux server under PM2, alongside the existing Docker path. - ecosystem.config.cjs: PM2 process definitions for the API (Fastify + the single-instance Remote-Upload worker) and the Next.js standalone web server. - scripts/deploy.sh: one-shot build + Prisma migrate + seed for bare metal. - scripts/assemble-standalone.mjs (web postbuild): copies static/public into the Next standalone output so `server.js` serves everything self-contained. - API auto-loads the repo-root .env (apps/api/src/config/dotenv.ts) so no secrets are embedded in the PM2 config; seed loads it too. - docs/DEPLOYMENT.md: full PM2 walkthrough; README quick-start; .env.example storage-path guidance for non-root deployments. - gitignore the auto-generated next-env.d.ts (rewritten on every next build).
1 parent 8d8f86c commit 022f6ef

15 files changed

Lines changed: 284 additions & 6 deletions

‎.env.example‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ ADMIN_PASSWORD=CHANGE_ME_strong_admin_password
3434
# ── Storage ──────────────────────────────────────────────────────────────────
3535
# Driver: `local` (default). S3 driver is a documented future option.
3636
STORAGE_DRIVER=local
37-
# Absolute path (inside the container) where encrypted blobs are stored.
37+
# Where encrypted blobs are stored.
38+
# - Docker: leave as-is (compose mounts the `storage` volume at /data).
39+
# - Bare-metal / PM2: set an absolute path your run-user can write, then create it,
40+
# e.g. sudo mkdir -p /var/lib/opencoperlock && sudo chown "$USER" /var/lib/opencoperlock
41+
# STORAGE_PATH=/var/lib/opencoperlock/storage
3842
STORAGE_PATH=/data/storage
3943
# Where quarantined (infected) files are moved.
4044
QUARANTINE_PATH=/data/quarantine

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ build/
99
.next/
1010
out/
1111
*.tsbuildinfo
12+
# Auto-generated by `next build`/`next dev` (re-adds a .next/ reference each build)
13+
next-env.d.ts
1214

1315
# Environment & secrets
1416
.env

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ 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)
86+
87+
Prefer running it natively on a dedicated server? Provide your own PostgreSQL, then:
88+
89+
```bash
90+
cp .env.example .env # edit secrets + paths
91+
./scripts/deploy.sh # build, migrate, seed
92+
pm2 start ecosystem.config.cjs
93+
pm2 save && pm2 startup # restart on reboot
94+
```
95+
96+
Full PM2 walkthrough in [`docs/DEPLOYMENT.md`](./docs/DEPLOYMENT.md#bare-metal-with-pm2-dedicated-server).
97+
8598
---
8699

87100
## 🧑‍💻 Local development

‎apps/api/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@opencoperlock/shared": "workspace:*",
2828
"@prisma/client": "^6.1.0",
2929
"argon2": "^0.41.1",
30+
"dotenv": "^16.4.7",
3031
"fastify": "^5.2.0",
3132
"fastify-plugin": "^5.0.1",
3233
"pino": "^9.5.0",

‎apps/api/prisma/seed.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Idempotent seed: ensures the first admin account and the global Setting row exist.
33
* Safe to run on every deploy — it never overwrites an existing admin's password.
44
*/
5+
import '../src/config/dotenv.js';
56
import { PrismaClient } from '@prisma/client';
67
import argon2 from 'argon2';
78

‎apps/api/src/config/dotenv.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Load environment variables from `.env` files for bare-metal / PM2 deployments.
3+
*
4+
* Resolution order (first definition wins, matching dotenv's no-override default):
5+
* 1. variables already in the process environment (e.g. set by PM2 or systemd),
6+
* 2. `apps/api/.env` (local override),
7+
* 3. the repository-root `.env` (the one in `.env.example`).
8+
*
9+
* In Docker we pass env directly, so the files simply won't exist and this is a no-op.
10+
* Must be imported before any module that reads `process.env` (see index.ts).
11+
*/
12+
import { config } from 'dotenv';
13+
import { existsSync } from 'node:fs';
14+
import { resolve } from 'node:path';
15+
16+
const candidates = [
17+
resolve(process.cwd(), '.env'), // when cwd is apps/api
18+
resolve(process.cwd(), '../../.env'), // repo root from apps/api
19+
resolve(process.cwd(), 'apps/api/.env'), // when cwd is repo root
20+
];
21+
22+
for (const path of candidates) {
23+
if (existsSync(path)) config({ path });
24+
}

‎apps/api/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Side-effect import: must come first so .env is loaded before anything reads process.env.
2+
import './config/dotenv.js';
13
import { mkdir } from 'node:fs/promises';
24
import { loadEnv } from './env.js';
35
import { createContext } from './context.js';

‎apps/web/next-env.d.ts‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎apps/web/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"scripts": {
66
"dev": "next dev -p ${WEB_PORT:-3000}",
77
"build": "next build",
8+
"postbuild": "node ../../scripts/assemble-standalone.mjs",
89
"start": "next start -p ${WEB_PORT:-3000}",
10+
"start:standalone": "node .next/standalone/apps/web/server.js",
911
"typecheck": "tsc --noEmit",
1012
"lint": "ESLINT_USE_FLAT_CONFIG=false next lint --max-warnings 0"
1113
},

‎docs/DEPLOYMENT.md‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,86 @@ 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+
## Bare-metal with PM2 (dedicated server)
93+
94+
For a dedicated Linux box you can run the two Node processes under
95+
[PM2](https://pm2.keymetrics.io/) instead of Docker. You provide Postgres (and,
96+
optionally, ClamAV) yourself.
97+
98+
### 1. Prerequisites
99+
100+
```bash
101+
# Node 20+ and pnpm
102+
corepack enable
103+
# PM2
104+
npm i -g pm2
105+
# PostgreSQL running locally, plus a database + user for OpenCoperLock.
106+
# (optional) clamav-daemon if you want antivirus scanning.
107+
```
108+
109+
### 2. Configure
110+
111+
```bash
112+
git clone https://github.com/softpython2884/opencoperlock.git
113+
cd opencoperlock
114+
cp .env.example .env
115+
```
116+
117+
Edit `.env`:
118+
119+
- `DATABASE_URL` — point at your local Postgres.
120+
- `MASTER_KEY`, `SESSION_SECRET` — `openssl rand -base64 32` each.
121+
- `ADMIN_EMAIL` / `ADMIN_PASSWORD`.
122+
- `APP_URL` — your public site URL, e.g. `https://copper.forgenet.fr`.
123+
- `NEXT_PUBLIC_API_URL` — the public URL the **browser** uses to reach the API
124+
(baked into the build), e.g. `https://api.copper.forgenet.fr`.
125+
- `STORAGE_PATH` / `QUARANTINE_PATH` — absolute, writable paths (see `.env.example`).
126+
- `CLAMAV_ENABLED` — `false` if you're not running clamd.
127+
128+
### 3. Build, migrate, seed
129+
130+
```bash
131+
./scripts/deploy.sh
132+
```
133+
134+
This installs deps, builds `shared` + `api` + the web standalone bundle, applies Prisma
135+
migrations, seeds the first admin, and creates the storage directories.
136+
137+
### 4. Start under PM2
138+
139+
```bash
140+
pm2 start ecosystem.config.cjs
141+
pm2 save # remember the process list
142+
pm2 startup # generate & install the systemd unit so PM2 restarts on boot
143+
```
144+
145+
This launches `opencoperlock-api` (Fastify + the Remote-Upload worker, single instance)
146+
and `opencoperlock-web` (the Next.js standalone server). Useful commands:
147+
148+
```bash
149+
pm2 status
150+
pm2 logs opencoperlock-api
151+
pm2 reload ecosystem.config.cjs # zero-downtime reload after a redeploy
152+
```
153+
154+
> The API loads the repo-root `.env` itself, so no secrets are stored in the PM2 config.
155+
> Keep `opencoperlock-api` at a single instance — the Remote-Upload worker must not run in
156+
> parallel copies.
157+
158+
### 5. Redeploying
159+
160+
```bash
161+
git pull
162+
./scripts/deploy.sh
163+
pm2 reload ecosystem.config.cjs
164+
```
165+
166+
### 6. Reverse proxy
167+
168+
Put both processes behind your proxy exactly as in the Docker section above (web on
169+
:3000, API on :4000), terminating TLS there. Set `WEB_HOST=127.0.0.1` in the environment
170+
if you want the web process to bind only to localhost behind the proxy.
171+
92172
## Running without Docker (dev)
93173

94174
```bash

0 commit comments

Comments
 (0)