# 🚀 সহজ Deployment Guide (SSH দিয়ে)

## ✅ **আপনাকে কি করতে হবে**

খুবই সহজ! মাত্র **৩টি command** run করতে হবে।

---

## 📋 **Prerequisites**

আপনার server এ:
- Ubuntu 20.04+ অথবা Debian 10+
- SSH access (root user)
- Minimum 1GB RAM

---

## 🎯 **Automated Setup (সবচেয়ে সহজ)**

### **Step 1: Server এ Login করুন**

```bash
# Windows PowerShell বা Command Prompt খুলুন
ssh root@your-server-ip

# Password দিন
```

### **Step 2: Backend Files Upload করুন**

**Option A: SCP দিয়ে (Local থেকে):**

```bash
# আপনার local computer এ (নতুন terminal window)
cd "C:\Users\sohag\Downloads\no update\75%"

# Backend folder upload করুন
scp -r backend root@your-server-ip:/var/www/nandailseba-backend
```

**Option B: Git দিয়ে:**

```bash
# Server এ
cd /var/www
git clone <your-github-repo-url> nandailseba-backend
```

### **Step 3: Automated Script Run করুন**

```bash
# Server এ
cd /var/www/nandailseba-backend/deploy
chmod +x auto-setup.sh
sudo bash auto-setup.sh
```

**এই script automatic করবে:**
- ✅ Node.js install
- ✅ MySQL install & configure
- ✅ Database create
- ✅ Backend dependencies install
- ✅ Environment setup
- ✅ Database migrations run
- ✅ PM2 দিয়ে app start
- ✅ Nginx configure
- ✅ Firewall setup

**Time:** ~5-10 minutes

### **Step 4: Firebase Credentials Upload করুন**

```bash
# Local থেকে
scp firebase-credentials.json root@your-server-ip:/var/www/nandailseba-backend/
```

### **Step 5: Firebase Project ID Update করুন**

```bash
# Server এ
nano /var/www/nandailseba-backend/.env

# এই line খুঁজুন:
FIREBASE_PROJECT_ID=your-project-id

# আপনার project ID দিয়ে replace করুন
FIREBASE_PROJECT_ID=nandail-seba-2024

# Save: Ctrl+X, Y, Enter
```

### **Step 6: App Restart করুন**

```bash
pm2 restart all
```

### **Step 7: Test করুন**

```bash
# Browser এ খুলুন:
http://your-server-ip/api/v1/health

# অথবা terminal এ:
curl http://localhost/api/v1/health
```

**দেখবেন:**
```json
{
  "success": true,
  "message": "Nandail Seba API is running"
}
```

---

## ✅ **সফল হলে:**

আপনার API এখন live:
```
API Base: http://your-server-ip/api/v1
Health: http://your-server-ip/api/v1/health
Service Posts: http://your-server-ip/api/v1/service-posts
Products: http://your-server-ip/api/v1/products
Image Upload: http://your-server-ip/api/v1/images/upload-image
```

---

## 📝 **Manual Setup (যদি automated script কাজ না করে)**

<details>
<summary>Click to expand manual steps</summary>

### **1. Node.js Install:**
```bash
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
node --version
```

### **2. MySQL Install:**
```bash
sudo apt-get install -y mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
```

### **3. MySQL Setup:**
```bash
sudo mysql

# MySQL console এ:
CREATE DATABASE nandailseba_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'nandailseba_user'@'localhost' IDENTIFIED BY 'YourPassword123';
GRANT ALL PRIVILEGES ON nandailseba_db.* TO 'nandailseba_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

### **4. Backend Setup:**
```bash
cd /var/www/nandailseba-backend
npm install
cp .env.example .env
nano .env  # Configure settings
npm run migrate
```

### **5. PM2 Install & Start:**
```bash
sudo npm install -g pm2
pm2 start src/index.js --name nandailseba-api
pm2 startup
pm2 save
```

### **6. Nginx Install:**
```bash
sudo apt-get install -y nginx

# Config file তৈরি করুন:
sudo nano /etc/nginx/sites-available/nandailseba

# এই content paste করুন:
```
```nginx
server {
    listen 80;
    server_name _;
    client_max_body_size 10M;

    location /api/ {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /uploads/ {
        alias /var/www/nandailseba-backend/storage/uploads/;
        expires 30d;
    }

    location / {
        proxy_pass http://localhost:3000;
    }
}
```

```bash
# Enable site
sudo ln -s /etc/nginx/sites-available/nandailseba /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx
```

</details>

---

## 🔧 **Useful Commands**

### **App Management:**
```bash
pm2 status              # Check status
pm2 logs                # View logs (real-time)
pm2 restart all         # Restart app
pm2 stop all            # Stop app
pm2 start all           # Start app
```

### **Database:**
```bash
# Login to MySQL
mysql -u nandailseba_user -p nandailseba_db

# Check tables
SHOW TABLES;
SELECT COUNT(*) FROM service_posts;
```

### **Logs:**
```bash
# PM2 logs
pm2 logs

# Nginx logs
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
```

### **Firewall:**
```bash
sudo ufw status
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
```

---

## 🔒 **SSL Certificate (Optional - পরে করবেন)**

```bash
# Certbot install
sudo apt-get install -y certbot python3-certbot-nginx

# SSL certificate পান (Free)
sudo certbot --nginx -d yourdomain.com

# Auto-renewal
sudo certbot renew --dry-run
```

---

## 📱 **Android App Update**

```kotlin
// Constants.kt
const val API_BASE_URL = "http://your-server-ip"

// অথবা domain থাকলে:
const val API_BASE_URL = "https://api.yourdomain.com"
```

---

## 🐛 **Troubleshooting**

### **Problem: PM2 app not starting**
```bash
pm2 logs  # Check error logs
nano /var/www/nandailseba-backend/.env  # Verify config
pm2 restart all
```

### **Problem: MySQL connection failed**
```bash
systemctl status mysql
sudo systemctl restart mysql
mysql -u nandailseba_user -p  # Test login
```

### **Problem: Nginx not working**
```bash
sudo nginx -t  # Test config
sudo systemctl status nginx
sudo systemctl restart nginx
tail -f /var/log/nginx/error.log
```

### **Problem: Port 3000 already in use**
```bash
# Kill process
sudo lsof -ti:3000 | xargs kill -9

# Or change port in .env
nano .env  # Change PORT=3001
pm2 restart all
```

---

## 📊 **Monitoring**

### **Server Resources:**
```bash
htop              # CPU/Memory usage
df -h             # Disk space
free -h           # Memory
```

### **App Performance:**
```bash
pm2 monit         # Real-time monitoring
pm2 status        # Quick status
```

---

## ✅ **Deployment Checklist**

- [ ] Server setup complete
- [ ] Backend deployed
- [ ] Database created
- [ ] Migrations run
- [ ] Firebase credentials uploaded
- [ ] Environment configured
- [ ] PM2 running
- [ ] Nginx configured
- [ ] Firewall enabled
- [ ] API tested
- [ ] Android app updated

---

## 📞 **Need Help?**

যদি কোনো সমস্যা হয়:

1. Check logs: `pm2 logs`
2. Verify environment: `cat .env`
3. Test database: `mysql -u nandailseba_user -p`
4. Check credentials: `cat /root/nandailseba-credentials.txt`

---

## 🎉 **Success!**

যদি সব কিছু ঠিকমতো কাজ করে:

✅ API running at: `http://your-server-ip/api/v1`
✅ Database connected
✅ Image upload working
✅ Firebase auth working
✅ Ready for production!

**Congratulations! Your backend is live! 🚀**
