# Deployment Setup Guide

## Prerequisites
- cPanel with SSH access and Node.js support (Passenger)
- GitHub repository with Actions enabled

## GitHub Secrets Configuration

Go to your GitHub repository → Settings → Secrets and variables → Actions

### Required Secrets (for each environment: DEV and PRD)

#### Development Environment (DEV)
- `CPANEL_HOST_DEV` - cPanel hostname (e.g., yourdomain.com)
- `CPANEL_USERNAME_DEV` - cPanel SSH username
- `CPANEL_SSH_KEY_DEV` - SSH private key for authentication
- `CPANEL_PORT_DEV` - SSH port (usually 22)
- `CPANEL_DEPLOY_PATH_DEV` - Deployment path (e.g., /home/username/public_html/dev)

#### Production Environment (PRD)
- `CPANEL_HOST_PRD` - cPanel hostname
- `CPANEL_USERNAME_PRD` - cPanel SSH username
- `CPANEL_SSH_KEY_PRD` - SSH private key for authentication
- `CPANEL_PORT_PRD` - SSH port (usually 22)
- `CPANEL_DEPLOY_PATH_PRD` - Deployment path (e.g., /home/username/public_html)

## GitHub Variables Configuration

Go to your GitHub repository → Settings → Secrets and variables → Actions → Variables tab

### Required Variables

#### Development Environment
- `NEXT_PUBLIC_API_URL_DEV` - Strapi API URL for development (e.g., https://dev-api.yourdomain.com)
- `MAX_OLD_SPACE_SIZE_DEV` - Node memory limit for build (e.g., 4096)

#### Production Environment
- `NEXT_PUBLIC_API_URL_PRD` - Strapi API URL for production (e.g., https://api.yourdomain.com)
- `MAX_OLD_SPACE_SIZE_PRD` - Node memory limit for build (e.g., 4096)

## SSH Key Setup

1. Generate SSH key pair (if you don't have one):
   ```bash
   ssh-keygen -t rsa -b 4096 -C "github-actions" -f ~/.ssh/github_deploy
   ```

2. Copy the public key to cPanel:
   ```bash
   ssh-copy-id -i ~/.ssh/github_deploy.pub username@yourdomain.com
   ```

3. Add the private key content to GitHub Secrets:
   ```bash
   cat ~/.ssh/github_deploy
   ```
   Copy the entire content (including BEGIN and END lines) to `CPANEL_SSH_KEY_DEV` or `CPANEL_SSH_KEY_PRD`

## cPanel Node.js Configuration

1. Login to cPanel
2. Go to "Setup Node.js App" or "Application Manager"
3. Create new Node.js application:
   - Node.js version: 20.x or later
   - Application mode: Production
   - Application root: Your deployment path
   - Application URL: Your domain/subdomain
   - Application startup file: `server.js`

## Deployment Workflow

### Automatic Deployment
- Push to `development` branch → Deploys to DEV environment
- Push to `main` branch → Deploys to PRD environment

### Manual Deployment
Go to Actions tab → Select "Deploy to cPanel" → Run workflow

## Troubleshooting

### Deployment fails with "Permission denied"
- Check SSH key is correctly added to cPanel
- Verify SSH port is correct (usually 22)
- Test SSH connection manually: `ssh -p PORT username@hostname`

### Application not starting
- Check cPanel error logs
- Verify Node.js version compatibility (20.x recommended)
- Check Passenger configuration in .htaccess
- Restart application: `touch tmp/restart.txt` in deployment directory

### Environment variables not working
- Ensure variables are prefixed with `NEXT_PUBLIC_` for client-side access
- Rebuild application after changing environment variables
- Check `.env.production` file exists in deployment directory

## Important Notes

1. **Node.js Version**: Ensure cPanel Node.js version matches development (20.x)
2. **Memory**: If build fails with OOM error, increase `MAX_OLD_SPACE_SIZE`
3. **First Deployment**: May take longer due to dependency installation
4. **Passenger**: Automatically restarts application when `tmp/restart.txt` is touched
5. **SEO**: SSR mode ensures all meta tags are rendered server-side for optimal SEO

## Testing After Deployment

1. Check application is running:
   ```bash
   curl https://yourdomain.com
   ```

2. Verify SEO meta tags:
   ```bash
   curl -s https://yourdomain.com | grep -i "meta"
   ```

3. Check application logs in cPanel under "Node.js" section

## Performance Optimization

- Enable caching in cPanel
- Configure CDN for static assets
- Monitor Passenger memory usage
- Set up automatic backups (configured in deploy script)
