Files
api-btekno/DEPLOY_CORS_FIX.md

117 lines
2.8 KiB
Markdown
Raw Normal View History

# 🔧 Fix CORS Headers Tidak Muncul
## ⚠️ Masalah
CORS headers tidak muncul di HTTP response meskipun `php bin/test_cors.php` menunjukkan middleware sudah bekerja.
## ✅ Solusi Langkah Demi Langkah
### 1. Pull Code Terbaru di Server
```bash
cd /www/wwwroot/api.btekno.cloud/api
git pull origin main
composer dump-autoload --optimize
```
### 2. Verifikasi Code Sudah Ter-update
```bash
# Cek apakah CorsMiddleware ada
ls -la src/Middleware/CorsMiddleware.php
# Cek apakah Bootstrap sudah include CORS
grep -n "CorsMiddleware" src/Bootstrap/app.php
```
Harus muncul:
- File `src/Middleware/CorsMiddleware.php` ada
- `src/Bootstrap/app.php` berisi `CorsMiddleware`
### 3. Restart PHP-FPM (PENTING!)
```bash
# Cek PHP version
php -v
# Restart sesuai PHP version
systemctl reload php-fpm-83 # Untuk PHP 8.3
# atau
systemctl reload php-fpm-82 # Untuk PHP 8.2
```
**Via aaPanel:**
- Website → api.btekno.cloud → PHP → Service Management → Reload
### 4. Clear Opcache
```bash
# Via command
php -r "opcache_reset();"
```
**Via aaPanel:**
- Website → PHP → Opcache → Clear Cache
### 5. Test CORS
```bash
# Test dengan curl
curl -v -H "Origin: http://localhost/retribusi" https://api.btekno.cloud/health 2>&1 | grep -i "access-control"
```
**Harus muncul:**
```
< access-control-allow-origin: http://localhost/retribusi
< access-control-allow-credentials: true
< access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS
< access-control-allow-headers: Content-Type, Authorization, X-API-KEY, Accept, Origin
< access-control-max-age: 86400
```
### 6. Jika Masih Belum Muncul
**Cek error log:**
```bash
tail -f /www/wwwlogs/api.btekno.cloud.error.log
```
**Test middleware langsung:**
```bash
php bin/test_cors.php
```
Jika test script menunjukkan CORS headers muncul tapi HTTP tidak, kemungkinan:
1. PHP-FPM belum di-restart ✅
2. Opcache masih cache code lama ✅
3. Nginx mungkin menghapus headers (jarang terjadi)
**Cek nginx config:**
```bash
# Pastikan tidak ada proxy_hide_header atau add_header yang override
grep -i "access-control" /www/server/panel/vhost/nginx/api.btekno.cloud.conf
```
Jika ada, hapus atau comment out.
## 🎯 Quick Fix Command (All-in-One)
```bash
cd /www/wwwroot/api.btekno.cloud/api && \
git pull origin main && \
composer dump-autoload --optimize && \
php -r "opcache_reset();" && \
systemctl reload php-fpm-83 && \
echo "✅ Done! Test dengan: curl -I -H 'Origin: http://localhost/retribusi' https://api.btekno.cloud/health"
```
## 📝 Checklist
- [ ] Code sudah di-pull (`git pull origin main`)
- [ ] Autoloader sudah di-regenerate (`composer dump-autoload --optimize`)
- [ ] PHP-FPM sudah di-restart (`systemctl reload php-fpm-XX`)
- [ ] Opcache sudah di-clear (`php -r "opcache_reset();"`)
- [ ] Test CORS dengan curl menunjukkan headers muncul
- [ ] Test dari browser console tidak ada CORS error