# Deployment Guide — CMS Butonkab ke cPanel

## Prasyarat

- cPanel dengan **PHP 8.2+** (PHP Selector)
- cPanel dengan **Node.js 18+** (Node.js Selector / Setup Node.js App)
- MySQL 8 / MariaDB 10.6
- SSL (AutoSSL/Let's Encrypt) aktif
- Akses Cron Job
- Minimal 2 GB RAM (disarankan VPS)

---

## 1. Struktur Domain/Subdomain

| Subdomain | Fungsi | Deploy |
|---|---|---|
| `butonkab.go.id` | Frontend Publik (Next.js SSR) | Node.js App |
| `api.butonkab.go.id` | Backend Laravel API | PHP App |
| `cms.butonkab.go.id` | Frontend Admin (React SPA) | Static files |
| `ws.butonkab.go.id` | Realtime Service (Socket.io) | Node.js App |
| `{opd}.butonkab.go.id` | Website OPD (multi-tenant) | Ditangani Next.js |

---

## 2. Backend Laravel API

### Upload
1. Upload folder `backend/` ke `/home/user/laravel_app/` (di luar `public_html`)
2. Buat MySQL database & user via cPanel → MySQL Databases
3. Isi `.env` dengan kredensial database

### Setup
```bash
cd /home/user/laravel_app
composer install --no-dev --optimize-autoloader
php artisan key:generate
php artisan migrate --force
php artisan db:seed --force
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

### Document Root
Di cPanel → **Subdomains** → Buat `api.butonkab.go.id`
- Document Root: `/home/user/laravel_app/public`

### Cron Job
```
* * * * * cd /home/user/laravel_app && php artisan schedule:run >> /dev/null 2>&1
```

### Queue Worker
```bash
php artisan queue:work --stop-when-empty
```
Atau tambah cron per 5 menit:
```
*/5 * * * * cd /home/user/laravel_app && php artisan queue:work --stop-when-empty >> /dev/null 2>&1
```

---

## 3. Frontend Admin (React SPA)

### Build
```bash
cd frontend-admin
npm install
npm run build
```

### Upload
1. Upload isi folder `dist/` ke `/home/user/public_html/cms/`
2. Buat subdomain `cms.butonkab.go.id` → Document Root: `/home/user/public_html/cms/`

### .htaccess untuk SPA
Buat file `.htaccess` di folder `cms/`:
```apache
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>
```

---

## 4. Frontend Publik (Next.js SSR)

### Build
```bash
cd frontend-public
npm install
npm run build
```

### Deploy via Node.js Selector
1. Di cPanel → **Setup Node.js App**
2. Pilih Node.js 18/20
3. Application root: `frontend-public`
4. Startup file: `server.js` (custom server, atau gunakan `node_modules/next/dist/bin/next`)
5. Set environment variables:
   - `NEXT_PUBLIC_API_URL=https://api.butonkab.go.id/api`
   - `NEXT_PUBLIC_SITE_SLUG=butonkab`
   - `NODE_ENV=production`
6. Klik **Start App**

### Custom Server (opsional)
Buat file `server.js` di root `frontend-public/`:
```javascript
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  createServer((req, res) => {
    handle(req, res, parse(req.url, true))
  }).listen(3000, () => {
    console.log('> Ready on http://localhost:3000')
  })
})
```

---

## 5. Node.js Realtime Service

### Deploy via Node.js Selector
1. Di cPanel → **Setup Node.js App** (kedua)
2. Application root: `node-realtime`
3. Startup file: `server.js`
4. Set environment variables:
   - `PORT=4000`
   - `CORS_ORIGINS=https://butonkab.go.id,https://cms.butonkab.go.id`
   - `LARAVEL_API_URL=https://api.butonkab.go.id/api`
5. Start App

### WebSocket Proxy
Pastikan `mod_proxy_wstunnel` aktif di Apache. Tambahkan di `.htaccess` atau VirtualHost:
```apache
RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule /ws/(.*) ws://localhost:4000/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /ws/(.*) http://localhost:4000/$1 [P]
```

---

## 6. Post-Deployment Checklist

- [ ] SSL aktif di semua subdomain
- [ ] CORS configured di Laravel `.env`
- [ ] `SANCTUM_STATEFUL_DOMAINS` mencakup semua domain
- [ ] Queue worker berjalan
- [ ] Cron job aktif
- [ ] Backup otomatis terjadwal
- [ ] `php artisan route:cache` dan `config:cache` dijalankan
- [ ] Frontend build menggunakan production mode
- [ ] Node.js service berjalan via Passenger
- [ ] Test login admin di `cms.butonkab.go.id`
- [ ] Test API di `api.butonkab.go.id/api/documentation`

---

## 7. Monitoring

- Health check realtime: `ws.butonkab.go.id/health`
- API docs: `api.butonkab.go.id/api/documentation`
- Laravel Telescope (opsional): `api.butonkab.go.id/telescope`

---

## Troubleshooting

### Node.js App Tidak Start di cPanel
- Pastikan Node.js version ≥ 18 di Node.js Selector
- Cek log error di `/home/user/node-realtime/logs/`
- Pastikan `node_modules` sudah terinstall (`npm install`)

### Laravel 500 Error
- Cek log: `storage/logs/laravel.log`
- Pastikan `.env` terisi dengan benar
- Jalankan `php artisan config:clear && php artisan route:clear`

### WebSocket Tidak Connect
- Pastikan `mod_proxy_wstunnel` aktif
- Cek apakah port WebSocket tidak diblokir firewall hosting
- Fallback: gunakan polling HTTP jika WebSocket tidak tersedia
