# Deploying API on cPanel with PostgreSQL Database

## 1. PostgreSQL Setup in cPanel

1. Access PostgreSQL Database Wizard in cPanel:
   - Log in to cPanel
   - Find "PostgreSQL Database Wizard" or "PostgreSQL Databases"
   - If not available, contact your hosting provider to enable PostgreSQL

2. Create PostgreSQL Database:
```bash
# Note these credentials
Database Name: your_cpanel_username_databasename
Username: your_cpanel_username_dbuser
Password: your_secure_password
Host: localhost
Port: 5432  # Default PostgreSQL port
```

## 2. Prepare API for Deployment

1. Update your `.env` file with PostgreSQL credentials:
```env
DATABASE_URL="postgresql://your_cpanel_username_dbuser:your_secure_password@localhost:5432/your_cpanel_username_databasename"
NODE_ENV=production
PORT=8080
# Add other environment variables
```

2. Update your `package.json` to include necessary PostgreSQL dependencies:
```json
{
  "dependencies": {
    "@prisma/client": "latest",
    "pg": "^8.11.3"
  }
}
```

3. Update Prisma schema (`prisma/schema.prisma`):
```prisma
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
```

4. Build the application:
```bash
npm run build
```

## 3. Prepare Deployment Package

1. Create deployment package:
```bash
zip -r api-build.zip \
    dist/ \
    package.json \
    package-lock.json \
    prisma/ \
    ecosystem.config.js \
    tsconfig.json \
    tsconfig.build.json \
    nest-cli.json
```

## 4. cPanel Node.js Setup

1. In cPanel, navigate to "Setup Node.js App"

2. Create new application:
   - Application name: lensseek-api
   - Node.js version: 16.x or higher
   - Application mode: Production
   - Application root: /home/username/api
   - Application URL: api.yourdomain.com
   - Application startup file: dist/src/main.js
   - Environment variables: (copy from your .env file)

## 5. File Upload and Installation

1. Upload files via File Manager:
   - Go to File Manager in cPanel
   - Navigate to your application root
   - Upload and extract api-build.zip

2. SSH into your server and run:
```bash
cd ~/api
npm install --production
npm i pg @prisma/client
npx prisma generate
```

## 6. Database Migration

1. Run Prisma migrations:
```bash
npx prisma migrate deploy
```

2. Verify database connection:
```bash
npx prisma db seed  # If you have seed data
```

## 7. Start the Application

1. Configure PM2:
```javascript
// ecosystem.config.js
module.exports = {
  apps: [{
    name: 'lensseek-api',
    script: 'dist/src/main.js',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'production',
      PORT: 8080,
      DATABASE_URL: 'postgresql://your_cpanel_username_dbuser:your_secure_password@localhost:5432/your_cpanel_username_databasename'
    }
  }]
}
```

2. Start the application:
```bash
pm2 start ecosystem.config.js
```

## 8. Configure Reverse Proxy

1. Create `.htaccess` file:
```apache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ http://localhost:8080/$1 [P,L]
</IfModule>

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type, Authorization"
</IfModule>
```

## 9. SSL Configuration

1. Install SSL in cPanel:
   - Go to "SSL/TLS" section
   - Install Let's Encrypt SSL
   - Force HTTPS redirect

## 10. Testing and Monitoring

1. Test the API:
```bash
curl https://api.yourdomain.com/health
```

2. Monitor logs:
```bash
pm2 logs lensseek-api
```

3. Monitor PostgreSQL:
```bash
# Via cPanel PostgreSQL interface
# Or via psql if available:
psql -U your_cpanel_username_dbuser -d your_cpanel_username_databasename
```

## Troubleshooting

1. Database Connection Issues:
```bash
# Test PostgreSQL connection
psql -U your_cpanel_username_dbuser -d your_cpanel_username_databasename -h localhost
```

2. Permission Issues:
```bash
chmod -R 755 ~/api
chmod 600 .env
```

3. Node.js/PM2 Issues:
```bash
pm2 delete all
pm2 start ecosystem.config.js
```

4. Check logs:
```bash
pm2 logs
tail -f ~/logs/node.error.log
```

## Maintenance

1. Regular backups:
```bash
# Backup PostgreSQL database
pg_dump -U your_cpanel_username_dbuser your_cpanel_username_databasename > backup.sql
```

2. Monitor disk space:
```bash
du -sh ~/api/*
```

3. Update dependencies:
```bash
npm audit
npm update
```
