# 🚀 JobAlertIndia — Complete Deployment Guide
## Step-by-Step for Complete Beginners

---

## 📋 WHAT YOU NEED BEFORE STARTING
- A laptop/PC with internet
- ₹700-800/month budget
- 1-2 hours time for first setup

---

## PART 1 — BUY DOMAIN & HOSTING

### Step 1: Buy a Domain Name

1. Go to **https://namecheap.com**
2. Search for your domain (e.g., `jobalertindia.com`)
3. Add to cart → Checkout
4. Cost: ~$8-10/year (~₹700-800)

> 💡 Good domain ideas: jobalertindia.com, sarkarijobalert.com, freejobalert.in

---

### Step 2: Buy VPS Hosting (Required for Node.js + MySQL)

**Best Option for India: Hostinger VPS**

1. Go to **https://hostinger.in/vps-hosting**
2. Choose **KVM 1** plan (₹459/month) or **KVM 2** (₹899/month)
3. Select **Ubuntu 22.04** as the OS
4. Complete purchase
5. You'll get an **IP address** and **root password** by email

---

### Step 3: Connect Domain to Server

1. Login to Namecheap → Domain List → Click **Manage** on your domain
2. Go to **Advanced DNS**
3. Delete all existing records
4. Add these new records:

| Type | Host | Value | TTL |
|------|------|-------|-----|
| A Record | @ | YOUR_VPS_IP | Automatic |
| A Record | www | YOUR_VPS_IP | Automatic |

5. Wait 15-30 minutes for DNS to propagate

---

## PART 2 — SETUP YOUR SERVER

### Step 4: Connect to Server via SSH

**On Windows:**
1. Download **PuTTY**: https://putty.org
2. Open PuTTY
3. Host Name: YOUR_VPS_IP
4. Port: 22
5. Click Open
6. Login as: `root`
7. Password: (the one from Hostinger email)

**On Mac/Linux:**
Open Terminal and type:
```bash
ssh root@YOUR_VPS_IP
```
Type your password when asked.

---

### Step 5: Install Everything (Copy-paste each command)

```bash
# Update server
apt update && apt upgrade -y

# Install Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs

# Check Node.js installed
node --version
# Should show: v20.x.x

# Install MySQL
apt install -y mysql-server
mysql_secure_installation
```

**During mysql_secure_installation, answer:**
- Set root password: **YES** → Enter a strong password (save it!)
- Remove anonymous users: **YES**
- Disallow root login remotely: **YES**
- Remove test database: **YES**
- Reload privilege tables: **YES**

```bash
# Install Nginx (web server)
apt install -y nginx

# Install PM2 (keeps your app running 24/7)
npm install -g pm2

# Install Certbot (free SSL/HTTPS)
apt install -y certbot python3-certbot-nginx

# Install Git & Unzip
apt install -y git unzip
```

---

### Step 6: Create MySQL Database

```bash
# Login to MySQL
mysql -u root -p
# Enter the password you set above
```

Now paste these commands inside MySQL:
```sql
CREATE DATABASE jobalert_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'jobalert_user'@'localhost' IDENTIFIED BY 'MyStr0ngPass@2025';
GRANT ALL PRIVILEGES ON jobalert_db.* TO 'jobalert_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
```

> ⚠️ **Save these:** DB name: `jobalert_db`, DB user: `jobalert_user`, DB pass: `MyStr0ngPass@2025`

---

## PART 3 — UPLOAD & CONFIGURE YOUR PROJECT

### Step 7: Upload Your Project to Server

**Option A: Upload ZIP file (Easiest)**

1. Download **FileZilla**: https://filezilla-project.org
2. Open FileZilla
3. Fill top bar:
   - Host: `sftp://YOUR_VPS_IP`
   - Username: `root`
   - Password: your VPS password
   - Port: `22`
4. Click Quickconnect
5. On the right side (server), navigate to `/root/`
6. Drag your `jobalert-fullstack.zip` from left side to right side
7. Wait for upload to complete

**Then on your server terminal:**
```bash
cd /root
unzip jobalert-fullstack.zip
ls
# You should see: jobalert/
```

---

### Step 8: Configure Environment File

```bash
cd /root/jobalert/backend
cp .env.production .env
nano .env
```

This opens a text editor. Change these values:

```env
PORT=5000
NODE_ENV=production

DB_HOST=localhost
DB_PORT=3306
DB_USER=jobalert_user
DB_PASSWORD=MyStr0ngPass@2025
DB_NAME=jobalert_db

JWT_SECRET=PASTE_RANDOM_STRING_HERE
JWT_EXPIRES_IN=7d

FRONTEND_URL=https://www.yourdomain.com

ADMIN_EMAIL=admin@yourdomain.com
ADMIN_PASSWORD=Admin@SecurePass2025
```

**Generate a random JWT secret:**
```bash
# Open a new terminal tab, run this:
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
# Copy the output and paste as JWT_SECRET value
```

**Save the file:** Press `Ctrl+X` → `Y` → `Enter`

---

### Step 9: Install Dependencies & Setup Database

```bash
cd /root/jobalert/backend

# Install npm packages
npm install --production

# Create logs folder
mkdir -p logs

# Setup database (creates all tables + adds sample data + creates admin user)
npm run setup-db
```

You should see:
```
✅ Database schema created successfully
✅ Migration: Added education_level to jobs
✅ site_settings table ready
✅ Admin user: admin@yourdomain.com
✅ Sample data inserted
🎉 Setup complete!
```

---

### Step 10: Start Your App with PM2

```bash
cd /root/jobalert/backend

# Start the application
pm2 start ecosystem.config.js --env production

# Check it's running
pm2 status
# Should show: jobalert | online

# Save PM2 config (so app restarts if server reboots)
pm2 save

# Setup auto-start on reboot
pm2 startup
# Copy and run the command it shows you
```

**Test it works:**
```bash
curl http://localhost:5000/api/health
# Should return: {"success":true,"message":"JobAlert API is running!"}
```

---

## PART 4 — SETUP NGINX & SSL

### Step 11: Configure Nginx

```bash
# Copy the nginx config
cp /root/jobalert/nginx.conf /etc/nginx/sites-available/jobalert

# Edit it — replace yourdomain.com with YOUR actual domain
nano /etc/nginx/sites-available/jobalert
```

Find every `yourdomain.com` and replace with your actual domain (e.g., `jobalertindia.com`).

There are 4 places to change:
- `server_name yourdomain.com www.yourdomain.com;` (appears twice)
- `ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;`
- `ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;`

**Save:** Press `Ctrl+X` → `Y` → `Enter`

```bash
# Enable the site
ln -s /etc/nginx/sites-available/jobalert /etc/nginx/sites-enabled/

# Disable default nginx page
rm -f /etc/nginx/sites-enabled/default

# Test nginx config
nginx -t
# Should say: syntax is ok / test is successful

# Reload nginx
systemctl reload nginx
```

---

### Step 12: Get FREE SSL Certificate (HTTPS)

```bash
# Get SSL certificate
certbot --nginx -d yourdomain.com -d www.yourdomain.com
```

When it asks:
- Enter your email → type your email
- Agree to terms → type `Y`
- Share email with EFF → type `N`
- Select redirect option → type `2` (Redirect HTTP to HTTPS)

**Done!** Your site is now live at: `https://yourdomain.com`

---

## PART 5 — ACCESS YOUR SITE

### Website URLs

| Page | URL |
|------|-----|
| Homepage | https://yourdomain.com |
| Jobs Page | https://yourdomain.com/jobs.html |
| Admin Panel | https://yourdomain.com/admin.html |
| State Jobs | https://yourdomain.com/state-jobs.html |
| Education Jobs | https://yourdomain.com/education-jobs.html |

### Admin Panel Login

- **URL:** https://yourdomain.com/admin.html
- **Email:** admin@yourdomain.com (from your .env file)
- **Password:** Admin@SecurePass2025 (from your .env file)

> 🔐 Change your admin password after first login!

---

## PART 6 — VERIFY EVERYTHING WORKS

### Checklist After Deployment

```
✅ Homepage loads at https://yourdomain.com
✅ HTTPS padlock visible in browser
✅ Jobs page loads with sample data
✅ Admin panel opens at /admin.html
✅ Admin login works
✅ Can add a new job from admin panel
✅ New job appears on frontend
✅ Mobile version looks correct
✅ Dark mode toggle works
```

---

## PART 7 — DAILY MAINTENANCE

### Useful PM2 Commands

```bash
pm2 status              # Check if app is running
pm2 logs jobalert       # View recent logs
pm2 restart jobalert    # Restart the app
pm2 stop jobalert       # Stop the app
pm2 start jobalert      # Start the app
```

### Update Your Code (After Changes)

```bash
# Upload new zip via FileZilla, then:
cd /root/jobalert/backend
npm install --production
pm2 restart jobalert
echo "✅ Updated!"
```

### Setup Daily Backup

```bash
# Create backup script
cat > /root/backup.sh << 'EOF'
#!/bin/bash
DATE=$(date +%Y%m%d)
mkdir -p /root/backups
mysqldump -u jobalert_user -p'MyStr0ngPass@2025' jobalert_db | gzip > /root/backups/db_$DATE.sql.gz
find /root/backups -mtime +30 -delete
echo "Backup done: $DATE"
EOF

chmod +x /root/backup.sh

# Schedule to run every day at 2 AM
crontab -e
# Add this line at the bottom:
# 0 2 * * * /root/backup.sh
```

### Setup Firewall

```bash
ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw enable
# Type y when asked
ufw status
```

---

## TROUBLESHOOTING

### Problem: Site not opening
```bash
# Check if app is running
pm2 status
# If stopped: pm2 start jobalert

# Check nginx
systemctl status nginx
# If not running: systemctl start nginx

# Check logs for errors
pm2 logs jobalert --lines 50
```

### Problem: Admin login not working
```bash
# Check your .env file has correct values
cat /root/jobalert/backend/.env

# Re-run setup to create admin user
cd /root/jobalert/backend
npm run setup-db
```

### Problem: Database connection error
```bash
# Check MySQL is running
systemctl status mysql
# Start if stopped: systemctl start mysql

# Test database connection
mysql -u jobalert_user -p'MyStr0ngPass@2025' jobalert_db -e "SHOW TABLES;"
```

### Problem: SSL certificate error
```bash
# Renew certificate manually
certbot renew
systemctl reload nginx
```

---

## COST SUMMARY

| Item | Cost |
|------|------|
| Domain (.com) | ₹800/year (~₹67/month) |
| Hostinger KVM 1 VPS | ₹459/month |
| SSL Certificate | FREE |
| **Total** | **~₹530/month** |

---

## QUICK REFERENCE

| What | Command/URL |
|------|-------------|
| SSH into server | `ssh root@YOUR_VPS_IP` |
| Check app status | `pm2 status` |
| View logs | `pm2 logs jobalert` |
| Restart app | `pm2 restart jobalert` |
| Admin panel | https://yourdomain.com/admin.html |
| Nginx config | `/etc/nginx/sites-available/jobalert` |
| App folder | `/root/jobalert/backend/` |
| Env file | `/root/jobalert/backend/.env` |
