การปรับใช้และการตั้งค่า
ในบทความก่อนหน้านี้ เราได้พูดถึงพื้นฐานของ เริ่มต้นใช้งาน Logto อย่างรวดเร็ว บทความนี้จะลงลึกยิ่งขึ้น โดยเน้นแนวปฏิบัติที่ดีที่สุดและขั้นตอนการตั้งค่าอย่างละเอียดสำหรับการปรับใช้ Logto ในสภาพแวดล้อม production
ตัวแปรสภาพแวดล้อม (Environment variables)
เราใช้ชุดตัวแปรสภาพแวดล้อมที่สร้างขึ้นอัตโนมัติในเดโม (docker-compose.yml) ซึ่งคุณควรเปลี่ยนเป็นของคุณเองและรักษาความสอดคล้องกันระหว่าง Logto หลายอินสแตนซ์
คุณสามารถตั้งค่าตัวแปร env ได้โดยตรงหรือวางไฟล์ .env ไว้ใน root ของโปรเจกต์ Logto หากคุณกำลังทดสอบด้วย Docker ให้ตรวจสอบไฟล์ .env ที่ถูกสร้างในอิมเมจที่ /etc/logto
สิ่งจำเป็น (Essentials)
DB_URLPostgres DSN สำหรับฐานข้อมูล LogtoDATABASE_STATEMENT_TIMEOUT(v1.36.0+) PostgreSQLstatement_timeoutหน่วยเป็นมิลลิวินาที ใช้เป็น string ตัวเลข (เช่น5000) หรือDISABLE_TIMEOUTสำหรับ PgBouncer / RDS Proxy ดูรายละเอียดที่ ConfigurationPORTพอร์ตที่ Logto รับฟัง ค่าเริ่มต้น3001ENDPOINTคุณสามารถระบุ URL ที่เป็นโดเมนของคุณเองสำหรับ production (เช่นENDPOINT=https://logto.domain.com) ค่านี้จะมีผลกับ OIDC issuer identifier ด้วย
เปิดใช้งาน Admin Console
ADMIN_PORTพอร์ตที่ Logto Admin Console รับฟัง ค่าเริ่มต้น3002ADMIN_ENDPOINTคุณสามารถระบุ URL ที่เป็นโดเมนของคุณเองสำหรับ production (เช่นADMIN_ENDPOINT=https://admin.domain.com) ค่านี้จะมีผลกับค่า Admin Console Redirect URIs ด้วย
ปิดใช้งาน Admin Console
ADMIN_DISABLE_LOCALHOSTตั้งค่าเป็น1หรือtrueเพื่อปิดพอร์ตของ Admin Console หากไม่ได้ตั้งค่าADMIN_ENDPOINTจะปิด Admin Console โดยสมบูรณ์
ดูรายละเอียดเพิ่มเติมเกี่ยวกับตัวแปรสภาพแวดล้อมได้ที่ Configuration
เปิดใช้งาน Secret Vault
- หากต้องการใช้ Secret Vault คุณต้องตั้งค่า
SECRET_VAULT_KEKเป็น string ที่เข้ารหัส base64 ของ Key Encryption Key (KEK) ของคุณ ใช้สำหรับเข้ารหัส Data Encryption Keys (DEK) ใน Secret Vault แนะนำให้ใช้ AES-256 (32 ไบต์) ตัวอย่าง:crypto.randomBytes(32).toString('base64')
HTTPS
คุณสามารถใช้ Node.js เพื่อให้บริการ HTTPS โดยตรง หรือจะตั้งค่า proxy / balancer สำหรับ HTTPS ไว้หน้า Node.js ก็ได้ ดูรายละเอียดที่ Enabling HTTPS
Reverse proxy
หากคุณต้องการใช้ reverse proxy บนเซิร์ฟเวอร์ของคุณ เช่น Nginx หรือ Apache คุณต้องแมปพอร์ต 3001 และ 3002 แยกกันใน proxy pass สมมติว่าคุณใช้ Nginx โดย Logto auth endpoint ทำงานที่พอร์ต 3001 และ Logto admin console ทำงานที่ 3002 ให้วาง config ต่อไปนี้ใน nginx.conf:
server {
listen 443 ssl;
server_name <your_endpoint_url>; // เช่น auth.your-domain.com
...
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://127.0.0.1:3001;
}
ssl_certificate <path-to-your-certificate-for-auth-endpoint>;
ssl_certificate_key <path-to-your-certificate-key-for-auth-endpoint>
...
}
จากนั้นเพิ่ม config ที่คล้ายกันสำหรับ admin console ของคุณ:
server {
listen 443 ssl;
server_name <your_admin_endpoint_url>; // เช่น admin.your-domain.com
...
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://127.0.0.1:3002;
}
ssl_certificate <path-to-your-certificate-for-admin-endpoint>;
ssl_certificate_key <path-to-your-certificate-key-for-admin-endpoint>
...
}
รีโหลด config ของ Nginx เพื่อให้การเปลี่ยนแปลงมีผล:
nginx -s reload
เรียบร้อยแล้ว เปิดเบราว์เซอร์และไปที่ https://admin.your-domain.com คุณควรเห็นหน้า welcome ของ Logto
การทำให้เป็นคอนเทนเนอร์ (Containerization)
สำหรับ production คุณสามารถใช้ Docker เพื่อทำ Logto ให้เป็นคอนเทนเนอร์ได้ คุณจะพบ Dockerfile ใน root directory ของโปรเจกต์ หากคุณต้องการรัน Logto หลายอินสแตนซ์ เช่น ปรับใช้ Logto ใน Kubernetes cluster จะมีขั้นตอนเพิ่มเติมที่ต้องทำ
การตั้งค่าฐานข้อมูล (Database setup)
เตรียมฐานข้อมูล Postgres สำหรับ Logto ขอแนะนำอย่างยิ่งให้ใช้ Logto CLI เพื่อ initialize ฐานข้อมูล
npm run cli db seed -- --swe
ดูรายละเอียดเพิ่มเติมที่ Logto CLI
หากสภาพแวดล้อมที่คุณปรับใช้ไม่สามารถเข้าถึง api.pwnedpasswords.com ให้เพิ่ม --disable-admin-pwned-password-check ในคำสั่ง logto init หรือ npm run cli db seed เพื่อไม่ให้การสมัครแอดมินคนแรกค้างที่การตรวจสอบ breach ของ Have I Been Pwned ดูรายละเอียดที่ Seed for air-gapped or offline deployments
โฟลเดอร์ connectors ที่ใช้ร่วมกัน (Shared connectors folder)
โดยปกติ Logto จะสร้างโฟลเดอร์ connectors ใน root ของโฟลเดอร์ core เราแนะนำให้แชร์โฟลเดอร์นี้ระหว่าง Logto หลายอินสแตนซ์ คุณต้อง mount โฟลเดอร์ packages/core/connectors ไปยัง container และรัน npm run cli connector add -- --official เพื่อปรับใช้ connectors
ตัวอย่างขั้นต่ำ deployment สำหรับ Kubernetes:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: logto
namespace: default
spec:
template:
spec:
volumes:
- name: connectors
emptyDir: {}
initContainers:
- image: ghcr.io/logto-io/logto
command:
- /bin/sh
args:
- '-c'
- 'npm run cli connector add -- --official'
name: init
volumeMounts:
- name: connectors
mountPath: /etc/logto/packages/core/connectors
containers:
- image: ghcr.io/logto-io/logto
name: logto
volumeMounts:
- name: connectors
mountPath: /etc/logto/packages/core/connectors
ในตัวอย่างนี้ เราสร้างไดเรกทอรีว่างเป็น volume และ mount ไปยัง containers จากนั้นรัน npm run cli connector add -- --official ใน init container เพื่อดาวน์โหลด connectors สุดท้ายทุก container จะใช้โฟลเดอร์ connectors เดียวกันที่มี official connectors อยู่แล้ว
นี่เป็นตัวอย่าง yaml เท่านั้น ในการรัน Logto คุณต้องตั้งค่า envs ให้ถูกต้อง
สำหรับ production คุณสามารถเปลี่ยน volume แบบ "empty dir" เป็น persistent volume และทำ "init" job ด้วยวิธีของคุณเองได้ตามต้องการ
การเปลี่ยนแปลงฐานข้อมูล (Database alteration)
เช่นเดียวกับ connectors การเปลี่ยนแปลงฐานข้อมูลควรรันในอินสแตนซ์เดียว คุณสามารถใช้ job เพื่อรันสคริปต์ alteration ได้
ต้องตั้งค่าตัวแปรสภาพแวดล้อม CI=true เมื่อรัน alteration แบบ non-interactive
apiVersion: batch/v1
kind: Job
metadata:
name: alteration
spec:
template:
spec:
containers:
- name: alteration
image: ghcr.io/logto-io/logto
imagePullPolicy: Always
env:
- name: CI
value: 'true'
- name: DB_URL
value: postgresql://user:password@localhost:5432/logto
command:
- /bin/sh
args:
- '-c'
- 'npm run alteration deploy latest'
restartPolicy: Never
ดูรายละเอียดเกี่ยวกับคำสั่ง alteration ได้ที่ Database alteration