%PDF- %PDF-
| Direktori : /home2/rezakur1/api.rezakurnia.com/zh-router/ |
| Current File : /home2/rezakur1/api.rezakurnia.com/zh-router/DEPLOY_CPANEL.md |
# Deploy ke cPanel — KurniaDev AI Gateway
Panduan langkah demi langkah untuk deploy backend (Node.js) + frontend (Vite/React)
ke cPanel. Arsitektur: **2 subdomain** — frontend & backend terpisah.
| Komponen | Target |
|---|---|
| Frontend (SPA) | `https://ai.rezakurnia.com` |
| Backend (Node) | `https://api.rezakurnia.com` |
| Database | MySQL cPanel |
---
## 0. Prasyarat
- cPanel dengan fitur **Setup Node.js App** (Software → Setup Node.js App).
- Subdomain `api.rezakurnia.com` sudah dibuat + AutoSSL aktif (HTTPS).
- Akses Terminal cPanel (atau SSH) untuk `npm install`.
- Akses MySQL cPanel (buat DB + user).
---
## 1. Database (MySQL cPanel)
1. **Dump dari lokal** (di komputer Anda):
```bash
cd zh-router-backend
npm run db:dump
```
Menghasilkan `kurniadev_ai_dump_<tanggal>.sql` (berisi schema + semua data,
termasuk migrasi terbaru seperti `duration_hours`).
2. **Di cPanel → MySQL Databases**:
- Buat database baru, mis. `kurniadev_db`.
- Buat user + beri **ALL PRIVILEGES** ke database itu.
3. **Import** dump:
- Lewat **phpMyAdmin** (tab Import), atau
- Terminal: `mysql -h localhost -u <USER> -p <NAMA_DB> < kurniadev_ai_dump_*.sql`
---
## 2. Backend (Setup Node.js App)
1. **cPanel → Setup Node.js App** → Create Application:
- Node.js version: **20.x atau lebih baru** (backend butuh `>=20`).
- Application root: `api.rezakurnia.com` (subdomain).
- Application startup file: `src/server.js`
- **Application URL**: `https://api.rezakurnia.com`
- Simpan — cPanel akan membuat folder (mis. `~/nodeapps/api_rezakurnia_com`).
2. **Upload kode**:
- Upload isi folder `zh-router-backend` (kecuali `node_modules`) ke folder app.
- Terminal cPanel di folder app:
```bash
npm install --omit=dev
```
> ⚠️ **Catatan QRIS dinamis:** pastikan `npm install` berhasil menginstall dependensi baru
> (`jsqr`, `jimp`) — keduanya murni JavaScript, tanpa build native.
3. **Migrasi database**: jalankan SQL baru untuk tabel `qris_settings` & kolom
`topup_orders.dynamic_payload`:
```bash
mysql -h localhost -u <USER_DB_CPANEL> -p <NAMA_DB> < database/migration_add_qris.sql
```
3. **File `.env`** (buat di root app) — sesuaikan:
```bash
NODE_ENV=production
# Cookie Secure otomatis true saat production. Override eksplisit bila perlu:
# SECURE_COOKIES=false (mis. dev/LAN HTTP — JANGAN dipakai produksi)
SECURE_COOKIES=true
# Di cPanel (Setup Node.js App), biarkan HOST=127.0.0.1; PORT diatur platform.
HOST=127.0.0.1
PORT=3000
# Wajib true jika backend di belakang proxy/SSL cPanel (agar cookie & IP benar).
TRUST_PROXY=true
# CORS kini TIDAK membatasi origin (semua origin dicerminkan). Variabel ini
# dipertahankan hanya untuk kompatibilitas; tidak lagi dipakai untuk memblokir.
# APP_ORIGINS=https://ai.rezakurnia.com
DB_HOST=localhost
DB_PORT=3306
DB_NAME=kurniadev_db
DB_USER=<USER_DB_CPANEL>
DB_PASSWORD=<PASSWORD_DB_CPANEL>
BOZROUTER_BASE_URL=https://api.bozrouter.com/v1
BOZROUTER_API_KEY=replace-with-provider-key
MAX_BODY_SIZE=20971520
# URL gambar QRIS statis milik merchant (top-up). Kosongkan jika belum ada.
# Contoh: https://cdn.example.com/qris/merchant-qris.png
QRIS_IMAGE_URL=https://qris.interactive.co.id/homepage/images/assets/pay/harga/csan-qr-a.jpg
# Rewrite respons model (paralel: REWRITE_FROM[i] -> REWRITE_TO[i]).
# Kosongkan REWRITE_FROM untuk menonaktifkan rewriting.
REWRITE_FROM=BOZROUTER,bozrouter.com
REWRITE_TO=KURNIADEV,rezakurnia.com
GOOGLE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=xxx
# Dev: http://localhost:5173/auth/google/callback
# Produksi: https://ai.rezakurnia.com/auth/google/callback
# REDIRECT URI HARUS menunjuk ke HALAMAN CALLBACK FRONTEND — halaman itu yang
# menerima ?code=&state= lalu POST ke /api/auth/google/callback. Jangan memakai
# https://api.rezakurnia.com/... (endpoint backend dipanggil oleh frontend).
# (Update juga di Google Cloud Console → Credentials → OAuth → redirect URIs)
GOOGLE_REDIRECT_URI=https://ai.rezakurnia.com/auth/google/callback
```
4. **Beri proxy dari subdomain ke app**. Di root dokumen `api.rezakurnia.com`:
- Hapus `.htaccess` lama yang bukan untuk Node (mis. template hardening PHP berisi
`RewriteRule ^ - [L,F]` — ini membuat 403 untuk SEMUA path selain `/`).
- Simpan `.htaccess` proxy (file `deploy/api.htaccess.proxy` di repo ini):
```apache
<IfModule mod_rewrite.c>
RewriteEngine On
# Teruskan SEMUA request ke backend Fastify (port 3000)
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]
</IfModule>
```
- Alternatif (lebih disukai di CyberPanel): pakai proxy `context /` di `vhost.conf`
(bukan .htaccess) — satu mekanisme saja, jangan keduanya sekaligus.
5. **Restart / Stop & Start** app di cPanel (untuk memuat `.env` & kode baru), lalu
**Restart LiteSpeed** (CyberPanel → Services), untuk memuat perubahan proxy.
6. **Verifikasi**:
```bash
curl https://api.rezakurnia.com/health
# → {"status":"ok","service":"KurniaDev AI Gateway"}
curl https://api.rezakurnia.com/api/auth/google/config
# → {"enabled":true}
```
> ⚠️ **Penting**: Update **GOOGLE_REDIRECT_URI** juga di
> [Google Cloud Console](https://console.cloud.google.com) → Credentials → OAuth 2.0
> Client → tambahkan `https://ai.rezakurnia.com/auth/google/callback`.
---
## 3. Frontend (SPA Vite)
1. **Build produksi** (di komputer Anda), arahkan API ke backend:
```bash
cd zh-router-frontend
API_URL=https://api.rezakurnia.com ./build-prod.sh
```
- Hasil: folder `dist/` (termasuk `.htaccess`).
- Bila tidak set `API_URL`, frontend akan memanggil `/api` di domain yang sama
(cocok bila Anda pasang reverse-proxy `/api` di domain yang sama).
2. **Upload `dist/`** ke `public_html` (atau subfolder sesuai subdomain frontend).
3. **`.htaccess`** sudah ikut ter-upload (dari `public/.htaccess` → `dist/`).
Fungsinya: rewrite semua route SPA ke `index.html` (anti-404 saat refresh),
cache aset statis, + header keamanan dasar.
4. **Uji**:
- Buka `https://ai.rezakurnia.com` → login.
- Refresh di halaman dalam (mis. `/admin/users`) → tetap tampil (uji `.htaccess`).
- Cek login + top-up berfungsi (uji CORS + cookie Secure).
- Tombol "Lanjutkan dengan Google" muncul bila `/api/auth/google/config` → `{"enabled":true}`.
---
## 4. Checklist verifikasi akhir
- [ ] `https://api.rezakurnia.com/health` → `{"status":"ok"}`
- [ ] `https://api.rezakurnia.com/api/auth/google/config` → `{"enabled":true}`
- [ ] Login real di `https://ai.rezakurnia.com` → dashboard tampil
- [ ] Refresh halaman dalam → tidak 404 (`.htaccess` bekerja)
- [ ] API Keys: buat/revoke/hapus & pagination normal
- [ ] Top-up: buat QRIS → verifikasi admin → saldo naik
- [ ] Google OAuth masuk (redirect URI sudah diupdate di Console Google)
- [ ] Kasus: user dengan order pending top-up → tombol buat QRIS terkunci (409)
---
## 5. Troubleshooting umum
| Gejala | Kemungkinan penyebab / solusi |
|---|---|
| `curl /health` timeout | App belum di-*Start*; atau Node < 20; cek log app di cPanel |
| Login berhasil tapi session hilang | `SECURE_COOKIES` tidak cocok dengan HTTPS (harus `true` di production); atau `TRUST_PROXY` masih false |
| CORS / fetch ditolak | (Lama) `APP_ORIGINS` belum memuat URL frontend. Kini CORS terbuka (origin dicerminkan) — cek versi kode yang berjalan sudah di-deploy |
| Semua path selain `/` → 403 (halaman web server) | Vhost/docroot API hanya me-proxy root, atau ada `.htaccess` deny `[F]` (template PHP/SPA) di docroot API. Ganti dengan `RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]` / `context /`; restart LiteSpeed |
| 403 masih muncul walau rule proxy sudah benar | Cek **ModSecurity** di vhost (CyberPanel → Websites → Manage → ModSecurity → Off untuk verifikasi) |
| Tombol Google tidak muncul | `.env` server belum set `GOOGLE_CLIENT_ID/SECRET/REDIRECT_URI` (atau redirect URI memakai URL api) → `{"enabled":false}`; restart Node app |
| Refresh halaman 404 | `.htaccess` tidak ter-upload atau mod_rewrite nonaktif |
| Google login error redirect_uri | `GOOGLE_REDIRECT_URI` (harus URL callback frontend `https://ai.rezakurnia.com/auth/google/callback`) ≠ yang didaftarkan di Google Console |
| DB koneksi gagal | Pastikan user DB cPanel punya grant ke database; host `localhost` (bukan `127.0.0.1` pada beberapa cPanel) |