Files
AbelBirdNest-Stock/docs/deploy-production.md

7.0 KiB
Raw Blame History

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 Lets Encrypt

Direktori contoh:

/var/www/abelbirdnest-web

2. Buat User Khusus Aplikasi

Jalankan sebagai root atau dengan sudo:

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:

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.

Catatan:

  • perintah di atas hanya benar jika /var/www/abelbirdnest-web masih kosong
  • jika Anda sudah menjalankan clone biasa dan hasilnya menjadi: /var/www/abelbirdnest-web/AbelBirdNest-Stock maka lanjutkan semua perintah deploy dari folder itu:
cd /var/www/abelbirdnest-web/AbelBirdNest-Stock
  • alternatifnya, jika ingin struktur tanpa subfolder tambahan, hapus isi folder tujuan lalu clone ulang dengan titik:
rm -rf /var/www/abelbirdnest-web/AbelBirdNest-Stock
cd /var/www/abelbirdnest-web
git clone https://git.iptek.co/wirabasalamah/AbelBirdNest-Stock.git .

4. Environment Production

Salin .env.production.example menjadi .env.production, lalu isi nilainya.

Yang wajib:

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. Inisialisasi Database PostgreSQL

Contoh di bawah memakai:

  • database: abelbirdnest_prod
  • database user: abelbirdnest_app
  • host: 127.0.0.1
  • port: 5432

Masuk ke PostgreSQL sebagai superuser:

sudo -u postgres psql

Buat user database:

CREATE USER abelbirdnest_app WITH PASSWORD 'ganti-dengan-password-yang-kuat';

Buat database production:

CREATE DATABASE abelbirdnest_prod OWNER abelbirdnest_app;

Pastikan owner database benar:

ALTER DATABASE abelbirdnest_prod OWNER TO abelbirdnest_app;

Opsional tapi disarankan, kunci privilege default:

REVOKE ALL ON DATABASE abelbirdnest_prod FROM PUBLIC;
GRANT ALL PRIVILEGES ON DATABASE abelbirdnest_prod TO abelbirdnest_app;

Keluar dari psql:

\q

Tes koneksi:

psql "postgresql://abelbirdnest_app:ganti-dengan-password-yang-kuat@127.0.0.1:5432/abelbirdnest_prod"

Jika koneksi berhasil, pakai URL itu di .env.production:

DATABASE_URL="postgresql://abelbirdnest_app:ganti-dengan-password-yang-kuat@127.0.0.1:5432/abelbirdnest_prod?schema=public"

Catatan:

  • untuk psql, jangan tambahkan ?schema=public
  • untuk Prisma DATABASE_URL, tetap gunakan ?schema=public

6. Install Dependency, Database & Migration

Repo ini sudah disiapkan memakai migration Prisma.

Jalankan:

cd /var/www/abelbirdnest-web/AbelBirdNest-Stock
npm install
npm run prisma:generate
npm run prisma:migrate:deploy

Kalau perlu isi master awal:

npm run seed:master

Data seed yang dibawa:

  • grade
  • bank
  • currency

Khusus Seed Grade

Seed grade membutuhkan file sumber Grade.xls.

Sebelum menjalankan:

npm run seed:master

unggah dulu file Grade.xls ke server, misalnya ke:

/var/www/abelbirdnest-web/AbelBirdNest-Stock/scripts/data/Grade.xls

Contoh dari laptop lokal:

scp "Grade.xls" user@server:/var/www/abelbirdnest-web/AbelBirdNest-Stock/scripts/data/Grade.xls

Lalu jalankan:

cd /var/www/abelbirdnest-web/AbelBirdNest-Stock
npm run seed:grades

Alternatif jika file ada di lokasi lain:

node scripts/seed-grades-from-xls.mjs /path/ke/Grade.xls

atau:

GRADE_XLS_PATH=/path/ke/Grade.xls npm run seed:grades

Urutan pertama kali untuk fresh database:

cd /var/www/abelbirdnest-web/AbelBirdNest-Stock
npm install
npm run prisma:generate
npm run prisma:migrate:deploy
npm run seed:banks
npm run seed:currencies
npm run seed:grades

Catatan:

  • prisma:migrate:deploy akan membuat seluruh tabel dari migration yang ada di repo
  • seed:banks dan seed:currencies bisa langsung dijalankan
  • seed:grades butuh file Grade.xls lebih dulu
  • user login production tetap harus dibuat terpisah, jangan mengandalkan akun dev/default

7. Build Production

cd /var/www/abelbirdnest-web/AbelBirdNest-Stock
npm run build

8. Jalankan App di Port 3007

Manual:

PORT=3007 npm run start

Atau gunakan systemd dari:

deploy/systemd/abelbirdnest-web.service

Contoh setup:

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:

sudo systemctl is-enabled abelbirdnest-web

9. Reverse Proxy Nginx

Gunakan file:

deploy/nginx/abelbirdnest.id.conf

Pasang:

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

10. Health Check

Endpoint health:

GET /api/v1/health

Contoh:

curl https://abelbirdnest.id/api/v1/health

11. Update Deployment Berikutnya

Jika aplikasi sudah live dan ada update dari git:

cd /var/www/abelbirdnest-web/AbelBirdNest-Stock
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.

12. 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

13. 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.