# Comarnet Website - cPanel Deployment Guide

This guide walks you through deploying the Comarnet website on a cPanel server with Node.js and MySQL.

---

## Prerequisites

- cPanel hosting account with SSH access enabled
- MySQL database (create via cPanel)
- Node.js support (available on most modern cPanel servers)
- Domain: comarnet.com (already configured in cPanel)

---

## Step 1: Prepare Your cPanel Server

### 1.1 Create MySQL Database

1. Log in to cPanel
2. Go to **MySQL Databases**
3. Create a new database:
   - Database name: `comarnet_prod` (or your choice)
   - Username: `comarnet_user` (or your choice)
   - Password: Generate a strong password and save it
4. Click "Create Database"
5. Add all privileges to the user

**Save these credentials** — you'll need them in Step 3.

### 1.2 Enable SSH Access

1. In cPanel, go to **SSH Access**
2. If not already enabled, click "Manage SSH Keys"
3. Generate a new key pair or use existing one
4. Download the private key and save it securely

### 1.3 Check Node.js Availability

1. In cPanel, look for **Setup Node.js App** or **Node.js**
2. If available, note the Node.js version (should be 18+)
3. If not available, contact your hosting provider to enable it

---

## Step 2: Upload Project Files

### 2.1 Connect via SSH

```bash
ssh -i your-private-key.pem username@comarnet.com
```

### 2.2 Create Application Directory

```bash
cd public_html
mkdir comarnet-app
cd comarnet-app
```

### 2.3 Upload Project Files

You have two options:

**Option A: Using Git (Recommended)**

```bash
git clone https://your-repo-url.git .
```

**Option B: Using SFTP**

1. Use an SFTP client (FileZilla, WinSCP)
2. Connect to your server
3. Navigate to `public_html/comarnet-app`
4. Upload all files from the `dist` folder and `package.json`

### 2.4 Upload Build Files

The `dist` folder contains the production build:
- `dist/public/` — Frontend (HTML, CSS, JS)
- `dist/index.js` — Backend server

Copy these to your server:

```bash
# From your local machine
scp -r dist/* username@comarnet.com:public_html/comarnet-app/
scp package.json username@comarnet.com:public_html/comarnet-app/
scp pnpm-lock.yaml username@comarnet.com:public_html/comarnet-app/
```

---

## Step 3: Configure Environment Variables

### 3.1 Create .env.production File

```bash
cd ~/public_html/comarnet-app
nano .env.production
```

### 3.2 Fill in Configuration

Copy the template below and replace with your values:

```env
# Database Configuration
DATABASE_URL="mysql://comarnet_user:YOUR_PASSWORD@localhost:3306/comarnet_prod"

# Authentication & Security
JWT_SECRET="generate-a-random-32-character-string-here"

# App Configuration
VITE_APP_TITLE="Comarnet - Global Connectivity Solutions"
VITE_APP_LOGO="https://comarnet.com/logo.png"

# Node Environment
NODE_ENV="production"
PORT="3000"
```

### 3.3 Save File

Press `Ctrl+X`, then `Y`, then `Enter`

---

## Step 4: Install Dependencies

### 4.1 Install Node Modules

```bash
cd ~/public_html/comarnet-app
npm install
```

Or if using pnpm:

```bash
npm install -g pnpm
pnpm install
```

### 4.2 Setup Database

```bash
# Run database migrations
npm run db:push
```

This will create all necessary tables in your MySQL database.

---

## Step 5: Configure via cPanel Node.js Manager

### 5.1 Create Node.js Application

1. Log in to cPanel
2. Go to **Setup Node.js App**
3. Click **Create Application**
4. Fill in:
   - **Node.js version**: 18+ (or latest available)
   - **Application root**: `/home/username/public_html/comarnet-app`
   - **Application URL**: `comarnet.com`
   - **Application startup file**: `dist/index.js`
   - **Passenger log file**: Leave default
5. Click **Create**

### 5.2 Verify Application

1. After creation, you should see it in the list
2. Click the application name
3. Note the **Port** number (usually assigned automatically)
4. Click **Restart** to start the application

---

## Step 6: Configure SSL Certificate

### 6.1 Auto SSL (Recommended)

1. In cPanel, go to **AutoSSL**
2. Select your domain `comarnet.com`
3. Click **Check for SSL**
4. Wait for certificate to be issued (usually 5-15 minutes)

### 6.2 Manual SSL (If AutoSSL fails)

1. Go to **SSL/TLS Manager**
2. Click **Generate, view, upload, or delete SSL certificates**
3. Generate a new certificate for `comarnet.com`
4. Follow the prompts

---

## Step 7: Configure Domain Routing

### 7.1 Set Up Reverse Proxy (If Needed)

If your Node.js app is on a different port, you may need to set up a reverse proxy:

1. In cPanel, go to **Addon Domains** or **Parked Domains**
2. Ensure `comarnet.com` is pointing to `/public_html/comarnet-app`
3. The Node.js manager should handle routing automatically

### 7.2 Test Access

Open your browser and visit:
- `https://comarnet.com` — Should show the Comarnet homepage
- `https://comarnet.com/login` — Should show the admin login page
- `https://comarnet.com/admin` — Should redirect to login if not authenticated

---

## Step 8: Verify Everything Works

### 8.1 Test Frontend

- Visit `https://comarnet.com`
- Check all pages load correctly
- Verify images and styling display properly

### 8.2 Test Admin Panel

- Go to `https://comarnet.com/login`
- Create a new admin account
- Log in and verify you can add/edit team members
- Check that changes appear on the About page

### 8.3 Check Logs

```bash
# View Node.js application logs
cd ~/public_html/comarnet-app
tail -f error.log
```

---

## Step 9: Maintenance & Updates

### 9.1 Restart Application

If you make changes, restart via cPanel:

1. Go to **Setup Node.js App**
2. Click your application
3. Click **Restart**

Or via SSH:

```bash
cd ~/public_html/comarnet-app
npm restart
```

### 9.2 Update Code

To deploy new code:

```bash
cd ~/public_html/comarnet-app
git pull origin main  # If using Git
npm run build
npm restart
```

### 9.3 Database Backups

Regularly backup your MySQL database:

1. In cPanel, go to **Backup**
2. Download database backups regularly
3. Store backups securely

---

## Troubleshooting

### Application Won't Start

1. Check logs: `tail -f ~/public_html/comarnet-app/error.log`
2. Verify `.env.production` has correct database credentials
3. Ensure database is created and accessible
4. Restart application from cPanel

### Database Connection Error

1. Verify MySQL credentials in `.env.production`
2. Test connection: `mysql -u comarnet_user -p -h localhost comarnet_prod`
3. Ensure database user has all privileges
4. Check firewall isn't blocking MySQL port

### SSL Certificate Issues

1. Go to cPanel **AutoSSL** and check status
2. Ensure domain is properly configured
3. Try manual SSL if AutoSSL fails
4. Contact hosting provider if issues persist

### Port Already in Use

1. Check what's using the port: `lsof -i :3000`
2. Kill the process if needed: `kill -9 PID`
3. Restart application from cPanel

---

## Security Checklist

- [ ] MySQL password is strong (16+ characters, mixed case, numbers, symbols)
- [ ] JWT_SECRET is randomized and strong
- [ ] `.env.production` is NOT committed to Git
- [ ] SSH keys are stored securely
- [ ] SSL certificate is active (HTTPS)
- [ ] Database backups are scheduled
- [ ] Admin accounts have strong passwords
- [ ] Firewall allows only necessary ports (80, 443)

---

## Support

For issues with:
- **cPanel**: Contact your hosting provider
- **Node.js**: Check Node.js documentation
- **MySQL**: Check MySQL documentation
- **Comarnet app**: Review application logs

---

## Next Steps

1. Monitor application performance
2. Set up regular database backups
3. Plan for scaling if traffic increases
4. Consider CDN for static assets
5. Implement monitoring/alerting

---

**Deployment completed!** Your Comarnet website is now live on comarnet.com.
