Deployment Guide for ETemplate

# Deployment Guide for ETemplate

This guide provides instructions for deploying ETemplate in different environments.

## Table of Contents
1. [Local Development](#local-development)
2. [Production Deployment](#production-deployment)
3. [Database Setup](#database-setup)
4. [Environment Variables](#environment-variables)
5. [SSL Configuration](#ssl-configuration)
6. [Performance Optimization](#performance-optimization)

## Local Development

### Prerequisites
- Node.js 18.x or later
- MySQL Server 8.0 or later
- npm or yarn

### Steps
1. Clone the repository
2. Install dependencies:
   ```bash
   npm install
   ```

3. Set up the database:
   ```bash
   # Create MySQL database
   mysql -u root -p
   CREATE DATABASE etemplate_db;
   CREATE USER 'etemplate_user'@'localhost' IDENTIFIED BY 'ETemplate@2024';
   GRANT ALL PRIVILEGES ON etemplate_db.* TO 'etemplate_user'@'localhost';
   FLUSH PRIVILEGES;
   ```

4. Configure environment variables:
   ```bash
   cp .env.example .env
   # Edit .env with your configuration
   ```

5. Run database migrations:
   ```bash
   npm run migrate
   ```

6. Start development server:
   ```bash
   npm run dev
   ```

## Production Deployment

### Option 1: Vercel Deployment (Recommended)

1. Install Vercel CLI:
   ```bash
   npm install -g vercel
   ```

2. Login to Vercel:
   ```bash
   vercel login
   ```

3. Deploy:
   ```bash
   vercel
   ```

4. Configure environment variables in Vercel dashboard:
   - Go to Project Settings > Environment Variables
   - Add all variables from your .env file

5. Configure MySQL database:
   - Use a managed MySQL service (e.g., PlanetScale, AWS RDS)
   - Update DATABASE_URL in Vercel environment variables

### Option 2: Traditional Server Deployment

1. Server Requirements:
   - Ubuntu 20.04 LTS or later
   - Node.js 18.x
   - MySQL 8.0
   - Nginx
   - PM2 (for process management)

2. Server Setup:
   ```bash
   # Update system
   sudo apt update && sudo apt upgrade -y

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

   # Install MySQL
   sudo apt install mysql-server

   # Install Nginx
   sudo apt install nginx

   # Install PM2
   sudo npm install -g pm2
   ```

3. Configure MySQL:
   ```bash
   sudo mysql_secure_installation
   sudo mysql -u root -p
   CREATE DATABASE etemplate_db;
   CREATE USER 'etemplate_user'@'localhost' IDENTIFIED BY 'ETemplate@2024';
   GRANT ALL PRIVILEGES ON etemplate_db.* TO 'etemplate_user'@'localhost';
   FLUSH PRIVILEGES;
   ```

4. Deploy Application:
   ```bash
   # Clone repository
   git clone <repository-url>
   cd etemplate-clean

   # Install dependencies
   npm install

   # Build application
   npm run build

   # Run migrations
   npm run migrate

   # Start with PM2
   pm2 start npm --name "etemplate" -- start
   ```

5. Configure Nginx:
   ```bash
   sudo nano /etc/nginx/sites-available/etemplate
   ```

   Add the following configuration:
   ```nginx
   server {
       listen 80;
       server_name your-domain.com;

       location / {
           proxy_pass http://localhost:3000;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
       }
   }
   ```

   Enable the site:
   ```bash
   sudo ln -s /etc/nginx/sites-available/etemplate /etc/nginx/sites-enabled/
   sudo nginx -t
   sudo systemctl restart nginx
   ```

## Database Setup

### Production Database Considerations

1. Use a managed database service:
   - AWS RDS
   - Google Cloud SQL
   - PlanetScale
   - DigitalOcean Managed Databases

2. Database Backup:
   ```bash
   # Create backup
   mysqldump -u etemplate_user -p etemplate_db > backup.sql

   # Restore backup
   mysql -u etemplate_user -p etemplate_db < backup.sql
   ```

3. Regular Maintenance:
   ```bash
   # Optimize tables
   mysqlcheck -u etemplate_user -p --optimize etemplate_db

   # Analyze tables
   mysqlcheck -u etemplate_user -p --analyze etemplate_db
   ```

## Environment Variables

### Required Variables
```env
# App
NEXT_PUBLIC_APP_URL=https://your-domain.com

# Authentication
NEXTAUTH_URL=https://your-domain.com
NEXTAUTH_SECRET=your-secure-secret-key

# Database
DATABASE_URL="mysql://etemplate_user:ETemplate@2024@localhost:3306/etemplate_db"
```

### Optional Variables
```env
# Email (if using email features)
SMTP_HOST=
SMTP_PORT=
SMTP_USER=
SMTP_PASSWORD=

# Storage (if using cloud storage)
STORAGE_BUCKET=
STORAGE_REGION=
STORAGE_ACCESS_KEY=
STORAGE_SECRET_KEY=
```

## SSL Configuration

### Using Let's Encrypt

1. Install Certbot:
   ```bash
   sudo apt install certbot python3-certbot-nginx
   ```

2. Get SSL certificate:
   ```bash
   sudo certbot --nginx -d your-domain.com
   ```

3. Auto-renewal:
   ```bash
   sudo certbot renew --dry-run
   ```

## Performance Optimization

1. Enable caching:
   ```bash
   # Install Redis
   sudo apt install redis-server

   # Configure Next.js for Redis
   npm install @upstash/redis
   ```

2. Configure CDN:
   - Use Vercel's built-in CDN
   - Or configure Cloudflare

3. Database optimization:
   ```sql
   -- Add indexes for frequently queried columns
   ALTER TABLE users ADD INDEX idx_email (email);
   ALTER TABLE users ADD INDEX idx_status (status);
   ```

4. Image optimization:
   - Use Next.js Image component
   - Configure image domains in next.config.mjs

## Monitoring and Maintenance

1. Set up monitoring:
   ```bash
   # Install monitoring tools
   npm install -g clinic
   ```

2. Regular maintenance tasks:
   ```bash
   # Update dependencies
   npm update

   # Check for security vulnerabilities
   npm audit

   # Run database maintenance
   npm run db:maintain
   ```

3. Backup strategy:
   - Daily database backups
   - Weekly full system backups
   - Monthly archive backups

## Troubleshooting

Common issues and solutions:

1. Database connection issues:
   - Check firewall settings
   - Verify database credentials
   - Ensure database is running

2. Application errors:
   - Check logs: `pm2 logs`
   - Verify environment variables
   - Check Node.js version

3. Performance issues:
   - Monitor server resources
   - Check database indexes
   - Review application logs

## Support

For additional support:
- Check the documentation
- Open an issue on GitHub
- Contact the development team


Was this article helpful?