# cPanel Deployment Guide — Website Downloader

This guide explains how to deploy the Website Downloader application to cPanel.

## ⚠️ Important: Node.js Requirement

**This application requires Node.js 22+** — cPanel's traditional PHP hosting does NOT support Node.js applications directly.

You have three options:

### Option 1: cPanel with Node.js Addon (Recommended for cPanel)
If your cPanel host supports Node.js applications:
1. Go to **cPanel → Node.js Manager**
2. Create a new Node.js application
3. Follow the steps below

### Option 2: Separate Node.js Hosting (Easier)
Deploy to a Node.js-specific host instead:
- **Railway** ($5-20/mo) — Easiest, recommended
- **Render** (Free-$7/mo) — Also very easy
- **Fly.io** ($3-10/mo) — More control
- **DigitalOcean** ($5-12/mo) — Full VPS

### Option 3: Keep on Manus
The site is already running perfectly on Manus. Staying there avoids deployment complexity.

---

## Deployment Steps (Node.js on cPanel)

### 1. Extract the ZIP File

```bash
unzip website-downloader.zip
cd website-downloader
```

### 2. Install Dependencies

```bash
npm install
# or
pnpm install
```

### 3. Set Up Environment Variables

Create a `.env` file in the project root with these required variables:

```env
# Database
DATABASE_URL=mysql://username:password@localhost:3306/website_downloader

# JWT & Auth
JWT_SECRET=your-random-secret-key-here-min-32-chars
OAUTH_SERVER_URL=https://api.manus.im
VITE_APP_ID=your-manus-app-id
VITE_OAUTH_PORTAL_URL=https://portal.manus.im

# Stripe (Live Keys)
STRIPE_SECRET_KEY=sk_live_your_key_here
STRIPE_PUBLISHABLE_KEY=pk_live_your_key_here
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here
VITE_STRIPE_PUBLISHABLE_KEY=pk_live_your_key_here

# Owner Info
OWNER_NAME=Your Name
OWNER_OPEN_ID=your-open-id

# File Storage (S3-compatible)
# You'll need to set up AWS S3 or a compatible service
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_REGION=us-east-1
AWS_S3_BUCKET=your-bucket-name

# Analytics (optional)
VITE_ANALYTICS_ENDPOINT=https://your-analytics-endpoint
VITE_ANALYTICS_WEBSITE_ID=your-website-id

# App Configuration
VITE_APP_TITLE=Website Downloader
VITE_APP_LOGO=https://your-logo-url.png
```

### 4. Set Up MySQL Database

```bash
# Log into MySQL
mysql -u username -p

# Create database
CREATE DATABASE website_downloader;
USE website_downloader;

# Run the schema (from drizzle/migrations/)
# Copy the SQL from the latest migration file and paste it here
```

**Or use Drizzle migrations:**
```bash
npx drizzle-kit generate
npx drizzle-kit migrate
```

### 5. Build the Application

```bash
npm run build
# or
pnpm build
```

### 6. Start the Server

**Development:**
```bash
npm run dev
```

**Production:**
```bash
npm run start
```

Or use a process manager like **PM2**:
```bash
npm install -g pm2
pm2 start server/index.ts --name "website-downloader"
pm2 save
pm2 startup
```

### 7. Configure Your Domain

1. Point your domain nameservers to your cPanel host's nameservers
2. Create an A record pointing to your server's IP address
3. Wait 24-48 hours for DNS propagation

### 8. Set Up SSL Certificate

In cPanel:
1. Go to **AutoSSL** or **SSL/TLS**
2. Install a free Let's Encrypt certificate for your domain
3. Ensure HTTPS is enforced

---

## Required External Services

### 1. MySQL Database
- Local MySQL on cPanel, OR
- Managed MySQL (AWS RDS, DigitalOcean, etc.)

### 2. File Storage (S3)
The app stores downloaded ZIP files in S3. You need:
- **AWS S3 bucket** ($1-5/mo typical usage), OR
- **DigitalOcean Spaces** ($5/mo), OR
- **Wasabi** ($6/mo for 1TB)

Set up S3 credentials in your `.env` file.

### 3. Stripe (Already Configured)
Your Stripe keys are ready. Just ensure `STRIPE_WEBHOOK_SECRET` is set correctly.

### 4. Email Service (Optional)
For sending "download ready" notifications:
- **Zoho SMTP** (free), OR
- **SendGrid** ($20+/mo), OR
- **AWS SES** (pay-per-email)

---

## Troubleshooting

### "Cannot find module" errors
```bash
npm install
npm run build
```

### Database connection fails
- Check `DATABASE_URL` in `.env`
- Verify MySQL user has correct permissions
- Test connection: `mysql -u user -p -h host database_name`

### Port already in use
Change the port in `server/index.ts` or use:
```bash
PORT=3001 npm start
```

### Stripe webhook not working
1. Verify `STRIPE_WEBHOOK_SECRET` is correct
2. Check webhook endpoint URL in Stripe Dashboard
3. Test with: `curl -X POST http://localhost:3000/api/stripe/webhook`

### File uploads failing
- Verify S3 credentials in `.env`
- Check S3 bucket permissions
- Ensure bucket is in the correct region

---

## Recommended: Use Railway or Render Instead

If you encounter issues with cPanel, consider using **Railway** or **Render** instead. They handle:
- ✅ Automatic Node.js setup
- ✅ Built-in database hosting
- ✅ Automatic SSL
- ✅ Auto-scaling
- ✅ Easy environment variables
- ✅ Git-based deployments

**Railway deployment (5 minutes):**
1. Push code to GitHub
2. Go to railway.app
3. Connect GitHub repo
4. Set environment variables
5. Deploy

Cost: $5-20/month for your usage.

---

## Support

If you encounter issues:
1. Check the logs: `tail -f /var/log/nodejs/app.log`
2. Review `.env` configuration
3. Test database connection
4. Verify Stripe webhook secret
5. Check S3 bucket access

For Manus-specific issues, contact: https://help.manus.im

---

## Database Schema

The application uses Drizzle ORM with the following main tables:

- **users** — User accounts and authentication
- **jobs** — Download jobs (status, progress, ZIP file URL)
- **pending_jobs** — Jobs awaiting payment
- **contact_replacements** — Domain/phone replacements for downloads
- **stripe_customers** — Stripe customer mappings

Run migrations to create these tables:
```bash
npx drizzle-kit migrate
```

---

## File Structure

```
website-downloader/
├── client/              # React frontend
│   ├── src/
│   │   ├── pages/      # Page components
│   │   ├── components/ # Reusable UI components
│   │   └── lib/        # Utilities (tRPC client, etc.)
│   └── index.html
├── server/              # Express backend
│   ├── routers.ts      # tRPC procedures (API endpoints)
│   ├── db.ts           # Database queries
│   ├── jobProcessor.ts # Download job engine
│   └── contactReplacer.ts # Domain/phone replacement logic
├── drizzle/            # Database schema & migrations
├── storage/            # S3 file storage helpers
├── package.json
└── .env                # Environment variables (create this)
```

---

## Next Steps

1. **Extract the ZIP file** to your cPanel public_html or a Node.js app directory
2. **Create `.env` file** with your Stripe keys, database URL, and S3 credentials
3. **Install dependencies**: `npm install`
4. **Run migrations**: `npx drizzle-kit migrate`
5. **Start the server**: `npm start`
6. **Test the app** at your domain
7. **Set up SSL certificate** in cPanel

Good luck! 🚀
