# cPanel Deployment Guide for Winslow Publishing Membership Portal

## Prerequisites

- cPanel hosting account with Node.js support
- MySQL database access
- Domain or subdomain configured in cPanel
- SSH access (recommended but not required)

---

## Step 1: Create MySQL Database

1. Log into your cPanel
2. Navigate to **Databases → MySQL Databases**
3. Create a new database:
   - Database name: `winslow_membership` (or your preferred name)
   - Click **Create Database**
4. Create a database user:
   - Username: `winslow_user` (or your preferred username)
   - Password: Generate a strong password and save it
   - Click **Create User**
5. Add user to database:
   - Select the user and database you just created
   - Grant **ALL PRIVILEGES**
   - Click **Add**
6. **Save these credentials** - you'll need them later:
   - Database name
   - Database username
   - Database password
   - Database host (usually `localhost`)

---

## Step 2: Set Up Node.js Application

1. In cPanel, go to **Software → Setup Node.js App**
2. Click **Create Application**
3. Configure the application:
   - **Node.js version**: Select 18.x or higher (20.x recommended)
   - **Application mode**: Production
   - **Application root**: `winslow-membership` (or your preferred folder)
   - **Application URL**: Select your domain/subdomain
   - **Application startup file**: `server/_core/index.ts`
   - **Passenger log file**: Leave default
4. Click **Create**
5. **Important**: Copy the command shown for entering the virtual environment

---

## Step 3: Upload Application Files

### Option A: Using File Manager (Easier)

1. Download the deployment package (winslow-membership.zip) provided
2. In cPanel, go to **Files → File Manager**
3. Navigate to your home directory
4. Upload `winslow-membership.zip`
5. Right-click the file and select **Extract**
6. Delete the zip file after extraction

### Option B: Using FTP/SFTP

1. Connect to your server using FTP client (FileZilla, etc.)
2. Upload the entire `winslow-membership` folder to your home directory

---

## Step 4: Configure Environment Variables

1. In **Setup Node.js App**, click on your application
2. Scroll to **Environment Variables** section
3. Add the following variables one by one:

### Required Database Variables:
```
DATABASE_URL = mysql://USERNAME:PASSWORD@localhost:3306/DATABASE_NAME
```
Replace:
- `USERNAME` with your database username
- `PASSWORD` with your database password
- `DATABASE_NAME` with your database name

### Required Application Variables:
```
NODE_ENV = production
PORT = (leave empty - cPanel assigns this automatically)
JWT_SECRET = (generate a random 32+ character string)
```

### Stripe Variables (if using payments):
```
STRIPE_SECRET_KEY = your_stripe_secret_key
STRIPE_WEBHOOK_SECRET = your_stripe_webhook_secret
VITE_STRIPE_PUBLISHABLE_KEY = your_stripe_publishable_key
```

### OAuth Variables (for Manus OAuth - optional):
```
OAUTH_SERVER_URL = https://api.manus.im
VITE_OAUTH_PORTAL_URL = https://portal.manus.im
VITE_APP_ID = your_app_id
OWNER_OPEN_ID = your_owner_open_id
OWNER_NAME = Your Name
```

4. Click **Save** after adding all variables

---

## Step 5: Install Dependencies

### Using cPanel Terminal:

1. In cPanel, go to **Advanced → Terminal**
2. Navigate to your application directory:
   ```bash
   cd winslow-membership
   ```
3. Enter the Node.js virtual environment (use the command from Step 2):
   ```bash
   source /home/USERNAME/nodevenv/winslow-membership/18/bin/activate
   ```
4. Install dependencies:
   ```bash
   npm install --production
   ```
5. Install pnpm globally (if not available):
   ```bash
   npm install -g pnpm
   ```
6. Install dependencies with pnpm:
   ```bash
   pnpm install --prod
   ```

---

## Step 6: Initialize Database

1. Still in the terminal, run the database migration:
   ```bash
   pnpm db:push
   ```
   This will create all necessary tables in your MySQL database.

2. Verify tables were created:
   - Go to **Databases → phpMyAdmin**
   - Select your database
   - You should see tables: users, comments, commentVotes, subscriptionPlans, etc.

---

## Step 7: Build the Application

1. In the terminal, build the frontend:
   ```bash
   pnpm run build
   ```
   This compiles the React frontend into static files.

2. Wait for the build to complete (may take 1-2 minutes)

---

## Step 8: Start the Application

1. Go back to **Setup Node.js App**
2. Click on your application
3. Click **Restart** button
4. Wait 10-20 seconds for the application to start
5. Click **Open** or visit your configured domain

---

## Step 9: Verify Deployment

1. Visit your website URL
2. You should see the landing page with "Master the Art of Parallel Building"
3. Test the following:
   - Click "Go to Dashboard" - should redirect to login
   - Check that pages load without errors
   - Open browser console (F12) - check for any errors

---

## Troubleshooting

### Application won't start:
- Check the Passenger log file (path shown in Node.js App settings)
- Verify all environment variables are set correctly
- Ensure DATABASE_URL format is correct
- Check that database user has proper privileges

### Database connection errors:
- Verify database credentials in DATABASE_URL
- Check that database user is added to the database with ALL PRIVILEGES
- Try connecting to database via phpMyAdmin to verify credentials

### "Module not found" errors:
- Ensure you ran `pnpm install --prod` in the virtual environment
- Try running `pnpm install` again
- Check that you're in the correct directory

### Build errors:
- Ensure Node.js version is 18.x or higher
- Clear node_modules and reinstall: `rm -rf node_modules && pnpm install`
- Check available disk space

### Application runs but pages show errors:
- Check browser console for JavaScript errors
- Verify environment variables are set
- Check Passenger log for backend errors

---

## Post-Deployment Configuration

### Set Up Stripe Webhooks (if using payments):

1. Go to Stripe Dashboard → Developers → Webhooks
2. Add endpoint: `https://yourdomain.com/api/stripe/webhook`
3. Select events: `customer.subscription.*`, `invoice.*`
4. Copy webhook secret and add to environment variables
5. Restart the application

### Configure Domain/SSL:

1. In cPanel, go to **Security → SSL/TLS Status**
2. Enable AutoSSL for your domain
3. Wait for SSL certificate to be issued (5-10 minutes)
4. Access your site via `https://`

### Set Up Cron Jobs (optional - for maintenance tasks):

1. Go to **Advanced → Cron Jobs**
2. Add any scheduled tasks if needed

---

## Updating the Application

To update the application in the future:

1. Upload new files via File Manager or FTP
2. In Terminal, navigate to app directory
3. Enter virtual environment
4. Run: `pnpm install --prod`
5. Run: `pnpm run build`
6. Run: `pnpm db:push` (if database changes)
7. Restart application in Node.js App manager

---

## Support

For technical issues with this deployment:
- Check cPanel documentation for Node.js apps
- Contact your hosting provider for cPanel-specific issues
- Review application logs in the Passenger log file

For application-specific issues:
- Check the application code in the `server/` and `client/` directories
- Review error messages in browser console and server logs
