การปรับใช้และการตั้งค่า
ในบทความก่อนหน้านี้ เราได้พูดถึงพื้นฐานของ เริ่มต้นใช้งาน 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หน่วยเป็นมิลลิวินาที ใช้เป็นสตริงตัวเลข (เช่น5000) หรือDISABLE_TIMEOUTสำหรับ PgBouncer / RDS Proxy ดู การตั้งค่า สำหรับรายละเอียดPORTพอร์ตที่ 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 โดยสมบูรณ์
ดูรายละเอียดเพิ่มเติมเกี่ยวกับตัวแปรสภาพแวดล้อมได้ที่ การตั้งค่า
เปิดใช้งาน Secret Vault
- หากต้องการใช้ Secret Vault คุณต้องตั้งค่า
SECRET_VAULT_KEKเป็นสตริง 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 ก็ได้ ดู การเปิดใช้งาน HTTPS สำหรับรายละเอียด
Reverse proxy
หากคุณต้องการใช้ reverse proxy บนเซิร์ฟเวอร์ของคุณ เช่น Nginx หรือ Apache คุณต้องแมปพอร์ต 3001 และ 3002 แยกกันใน proxy pass settings สมมติว่าคุณใช้ Nginx โดย Logto auth endpoint ทำงานที่พอร์ต 3001 และ Logto admin console ทำงานที่ 3002 ให้วาง config ต่อไปนี้ใน nginx.conf:
ตั้งค่า TRUST_PROXY_HEADER=1 เพื่อให้ Logto เชื่อ header ที่ถูกส่งต่อ ใช้ $http_host สำหรับทั้ง Host และ X-Forwarded-Host เพื่อรักษาพอร์ตที่ไม่มาตรฐานใน public URL
server {
listen 443 ssl;
server_name <your_endpoint_url>; // เช่น auth.your-domain.com
...
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_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 $scheme;
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 $http_host;
proxy_set_header X-Forwarded-Host $http_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 $scheme;
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 เพื่อไม่ให้การสมัครแอดมินครั้งแรกค้างที่การตรวจสอบ Have I Been Pwned ดู Seed สำหรับการปรับใช้แบบ air-gapped หรือออฟไลน์ สำหรับรายละเอียด
โฟลเดอร์ 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
ดู Database alteration สำหรับรายละเอียดเกี่ยวกับคำสั่ง alteration