210 lines
3.9 KiB
Markdown
210 lines
3.9 KiB
Markdown
# Deploy Production
|
||
|
||
Dokumen ini menyiapkan deploy production untuk:
|
||
|
||
- domain `abelbirdnest.id`
|
||
- reverse proxy `nginx`
|
||
- aplikasi Next.js di port `3007`
|
||
- database `PostgreSQL`
|
||
- source code dari git `https://git.iptek.co/wirabasalamah/AbelBirdNest-Stock.git`
|
||
- user service khusus `abelbirdnest`
|
||
|
||
## 1. Persiapan Server
|
||
|
||
Siapkan:
|
||
|
||
- Node.js LTS
|
||
- npm
|
||
- PostgreSQL
|
||
- nginx
|
||
- certbot / SSL Let’s Encrypt
|
||
|
||
Direktori contoh:
|
||
|
||
```bash
|
||
/var/www/abelbirdnest-web
|
||
```
|
||
|
||
## 2. Buat User Khusus Aplikasi
|
||
|
||
Jalankan sebagai `root` atau dengan `sudo`:
|
||
|
||
```bash
|
||
sudo useradd -r -m -d /var/www/abelbirdnest-web -s /bin/bash abelbirdnest
|
||
sudo mkdir -p /var/www/abelbirdnest-web
|
||
sudo chown -R abelbirdnest:abelbirdnest /var/www/abelbirdnest-web
|
||
```
|
||
|
||
Catatan:
|
||
|
||
- user `abelbirdnest` dipakai khusus untuk menjalankan service aplikasi
|
||
- jangan jalankan app production dengan user pribadi atau `root`
|
||
|
||
## 3. Clone Repo dari Git
|
||
|
||
Masuk sebagai user aplikasi:
|
||
|
||
```bash
|
||
sudo -u abelbirdnest -H bash
|
||
cd /var/www/abelbirdnest-web
|
||
git clone https://git.iptek.co/wirabasalamah/AbelBirdNest-Stock.git .
|
||
```
|
||
|
||
Kalau server butuh autentikasi git internal, siapkan credential sesuai kebijakan server Git Anda.
|
||
|
||
## 4. Environment Production
|
||
|
||
Salin `.env.production.example` menjadi `.env.production`, lalu isi nilainya.
|
||
|
||
Yang wajib:
|
||
|
||
```env
|
||
NODE_ENV=production
|
||
PORT=3007
|
||
APP_URL=https://abelbirdnest.id
|
||
DATABASE_URL=postgresql://...
|
||
AUTH_SECRET=...
|
||
AUTH_BOOTSTRAP=false
|
||
SMTP_HOST=...
|
||
SMTP_PORT=465
|
||
SMTP_SECURE=true
|
||
SMTP_USER=...
|
||
SMTP_PASSWORD=...
|
||
SMTP_FROM=...
|
||
```
|
||
|
||
Catatan:
|
||
|
||
- `AUTH_SECRET` harus random panjang.
|
||
- `AUTH_BOOTSTRAP=false` wajib untuk production.
|
||
- `APP_URL` harus domain production final.
|
||
|
||
## 5. Install Dependency, Database & Migration
|
||
|
||
Repo ini sudah disiapkan memakai migration Prisma.
|
||
|
||
Jalankan:
|
||
|
||
```bash
|
||
cd /var/www/abelbirdnest-web
|
||
npm install
|
||
npm run prisma:generate
|
||
npm run prisma:migrate:deploy
|
||
```
|
||
|
||
Kalau perlu isi master awal:
|
||
|
||
```bash
|
||
npm run seed:master
|
||
```
|
||
|
||
Data seed yang dibawa:
|
||
|
||
- grade
|
||
- bank
|
||
- currency
|
||
|
||
## 6. Build Production
|
||
|
||
```bash
|
||
npm run build
|
||
```
|
||
|
||
## 7. Jalankan App di Port 3007
|
||
|
||
Manual:
|
||
|
||
```bash
|
||
PORT=3007 npm run start
|
||
```
|
||
|
||
Atau gunakan `systemd` dari:
|
||
|
||
```bash
|
||
deploy/systemd/abelbirdnest-web.service
|
||
```
|
||
|
||
Contoh setup:
|
||
|
||
```bash
|
||
sudo cp deploy/systemd/abelbirdnest-web.service /etc/systemd/system/
|
||
sudo systemctl daemon-reload
|
||
sudo systemctl enable abelbirdnest-web
|
||
sudo systemctl start abelbirdnest-web
|
||
sudo systemctl status abelbirdnest-web
|
||
```
|
||
|
||
Autostart saat server restart terjadi karena service di-`enable`.
|
||
|
||
Untuk verifikasi:
|
||
|
||
```bash
|
||
sudo systemctl is-enabled abelbirdnest-web
|
||
```
|
||
|
||
## 8. Reverse Proxy Nginx
|
||
|
||
Gunakan file:
|
||
|
||
```bash
|
||
deploy/nginx/abelbirdnest.id.conf
|
||
```
|
||
|
||
Pasang:
|
||
|
||
```bash
|
||
sudo cp deploy/nginx/abelbirdnest.id.conf /etc/nginx/sites-available/abelbirdnest.id.conf
|
||
sudo ln -s /etc/nginx/sites-available/abelbirdnest.id.conf /etc/nginx/sites-enabled/abelbirdnest.id.conf
|
||
sudo nginx -t
|
||
sudo systemctl reload nginx
|
||
```
|
||
|
||
## 9. Health Check
|
||
|
||
Endpoint health:
|
||
|
||
```bash
|
||
GET /api/v1/health
|
||
```
|
||
|
||
Contoh:
|
||
|
||
```bash
|
||
curl https://abelbirdnest.id/api/v1/health
|
||
```
|
||
|
||
## 10. Update Deployment Berikutnya
|
||
|
||
Jika aplikasi sudah live dan ada update dari git:
|
||
|
||
```bash
|
||
cd /var/www/abelbirdnest-web
|
||
git pull origin main
|
||
npm install
|
||
npm run prisma:migrate:deploy
|
||
npm run build
|
||
sudo systemctl restart abelbirdnest-web
|
||
```
|
||
|
||
Jika branch utama nanti bukan `main`, sesuaikan perintah `git pull`.
|
||
|
||
## 11. Checklist Go-Live
|
||
|
||
- `AUTH_BOOTSTRAP=false`
|
||
- `AUTH_SECRET` sudah production-grade
|
||
- `APP_URL=https://abelbirdnest.id`
|
||
- SSL aktif
|
||
- database backup aktif
|
||
- `npm run build` lulus
|
||
- `npm run prisma:migrate:deploy` lulus
|
||
- `npm run seed:master` selesai jika dibutuhkan
|
||
- login, reset password, dan email verifikasi sudah dites
|
||
- create purchase, receipt, lot, sale sudah dites
|
||
|
||
## 12. Catatan Penting
|
||
|
||
- Jangan pakai `npm run db:push` untuk production.
|
||
- Jangan pakai akun default development.
|
||
- Jangan simpan `.env.production` di repo.
|
||
- Pastikan ownership file tetap `abelbirdnest:abelbirdnest`.
|