# Deployment Guide - cPanel

This guide will help you deploy the Chocolate Ordering System to your cPanel hosting.

**📌 Important:** For detailed SSH access setup and quick deployment commands, see [SSH-DEPLOYMENT-GUIDE.md](SSH-DEPLOYMENT-GUIDE.md)

## Prerequisites

- cPanel account with SSH access
- Node.js installed on the server (via cPanel Node.js Selector or manual installation)
- Git installed on the server
- Your subdomain already created in cPanel

## Initial Setup (One-Time)

### 1. SSH into your cPanel server

```bash
ssh yourusername@yourdomain.com
```

### 2. Navigate to your subdomain's directory

```bash
# Usually something like:
cd ~/public_html/yoursubdomain
# or
cd ~/yoursubdomain.yourdomain.com
```

### 3. Clone the repository

```bash
git clone https://github.com/kevlad/site-comenzi-importuri-distributie.git .
```

**Note:** The `.` at the end clones into the current directory instead of creating a new folder.

### 4. Activate Node.js environment

**IMPORTANT**: If using cPanel Node.js, you must activate the environment before installing packages:

```bash
source ~/nodevenv/yoursubdomain.domain.com/22/bin/activate
```

This ensures npm installs packages to the correct location and uses the correct Node.js version.

### 5. Install dependencies

```bash
npm install
```

**Note**: On shared hosting, this can take **20-30 minutes** (254 packages). Be patient!

Expected output:
```
added 253 packages, and audited 254 packages in 25m
found 0 vulnerabilities
```

### 6. Pre-initialize the database

**CRITICAL STEP**: Create the database manually to avoid Passenger startup timeout:

```bash
# Start server briefly to initialize database
timeout 30 node server.js &

# Wait for initialization
sleep 10

# Stop the server
pkill -f "node server.js"

# Verify database was created
ls -lh database.db  # Should show ~64KB file
```

This creates `database.db` with all tables and default users without hitting Passenger's startup timeout.

### 7. Create required directories

```bash
mkdir -p uploads
mkdir -p public/images
mkdir -p public/catalogue
chmod 755 uploads public/images public/catalogue
```

### 8. Configure environment (if needed)

Create a `.env` file for production settings (optional):

```bash
nano .env
```

Add:
```
PORT=3000
NODE_ENV=production
```

### 9. Configure your subdomain in cPanel

**IMPORTANT**: cPanel uses Passenger, not PM2. No need to install PM2.

1. Go to cPanel → **Domains** → Select your subdomain
2. Set **Document Root** to the directory where you cloned the repo (e.g., `/home/username/yoursubdomain.domain.com`)
3. If using Node.js App in cPanel:
   - Go to **Setup Node.js App**
   - Set Application root to your directory
   - Set Application URL to your subdomain
   - Set Application startup file: `server.js`
   - Click "Create"

### 10. Setup reverse proxy (if needed)

If your app runs on port 3000 but needs to be accessible via port 80/443, you'll need a reverse proxy.

Add to your `.htaccess` file in the subdomain root:

```apache
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://localhost:3000/$1 [P,L]
```

Alternatively, if using cPanel's Node.js App manager, it handles this automatically.

## Deploying Updates (Every Time You Push to GitHub)

### Method 1: Manual via SSH

1. SSH into your server
2. Navigate to your app directory
3. Run the deployment script:

```bash
cd ~/yoursubdomain.domain.com
bash deploy.sh
```

### Method 2: Create a Webhook (Automated)

#### On your server:

1. Create a webhook script:

```bash
nano ~/webhook.sh
```

Add:
```bash
#!/bin/bash
cd /home/username/yoursubdomain.domain.com
bash deploy.sh
```

2. Make it executable:
```bash
chmod +x ~/webhook.sh
```

3. Create a simple webhook receiver using Node.js (optional) or use a webhook service.

#### On GitHub:

1. Go to your repository → **Settings** → **Webhooks** → **Add webhook**
2. Set Payload URL to your webhook endpoint
3. Set Content type to `application/json`
4. Select "Just the push event"
5. Save

### Method 3: Scheduled Auto-Deploy

Add a cron job in cPanel to pull updates every hour:

1. Go to cPanel → **Cron Jobs**
2. Add:
   - Minute: `0`
   - Hour: `*`
   - Day: `*`
   - Month: `*`
   - Weekday: `*`
   - Command: `cd /home/username/yoursubdomain.domain.com && git pull origin main && pm2 restart chocolate-ordering`

## Common Commands (Passenger)

### Restart app:
```bash
cd ~/yoursubdomain.domain.com
touch tmp/restart.txt
```

### Check if site is responding:
```bash
curl -I http://yoursubdomain.domain.com
# Should return HTTP 200 OK
```

### View error logs:
```bash
tail -50 ~/logs/yoursubdomain.domain.com.error_log
```

### Check database exists:
```bash
ls -lh ~/yoursubdomain.domain.com/database.db
```

### Update from GitHub manually:
```bash
cd ~/yoursubdomain.domain.com
git pull origin main
source ~/nodevenv/yoursubdomain.domain.com/22/bin/activate
npm install
touch tmp/restart.txt
```

## Troubleshooting

### Site shows 500 error:

**Step 1**: Check if dependencies are installed:
```bash
ls -la node_modules/ | wc -l
# Should show 250+ packages
```

If not installed:
```bash
source ~/nodevenv/yoursubdomain.domain.com/22/bin/activate
npm install
```

**Step 2**: Check if database exists:
```bash
ls -lh database.db
# Should show ~64KB file
```

If missing, initialize it:
```bash
timeout 30 node server.js &
sleep 10
pkill -f "node server.js"
```

**Step 3**: Restart Passenger:
```bash
touch tmp/restart.txt
```

**Step 4**: Test the site:
```bash
curl -I http://yoursubdomain.domain.com
# Should return HTTP 200 OK
```

### npm install taking too long:
On shared hosting, `npm install` can take **20-30 minutes** for 254 packages. This is normal. Be patient and don't interrupt it.

### Passenger startup timeout:
If you see "Web application could not be started", the database initialization is timing out. Pre-initialize it manually (see Step 2 above).

### Database not found:
The SQLite database (`database.db`) will be created automatically on first run. Make sure the directory is writable:
```bash
chmod 755 .
```

### File upload not working:
Check permissions on the uploads directory:
```bash
chmod 755 uploads
chmod 755 public/images
```

### Can't access via subdomain:
1. Check that the subdomain points to the correct directory in cPanel
2. Check that `.htaccess` exists and contains Passenger configuration
3. Check Node.js version in cPanel matches `nodevenv` path
4. Try restarting Passenger: `touch tmp/restart.txt`

## Security Notes

1. **Never commit sensitive data** - The `.gitignore` already excludes:
   - `database.db` (your production database)
   - `uploads/` (user uploads)
   - `.env` (environment variables)

2. **Default credentials**:
   - Admin: office@methexis.eu / Methexis1234
   - Customer: customer@example.com / customer123

3. **Database backups** - Set up regular backups:
```bash
# Add to cron job
0 2 * * * cp /home/username/yoursubdomain.domain.com/database.db /home/username/backups/database-$(date +\%Y\%m\%d).db
```

## Production Checklist

- [ ] Clone repository to server
- [ ] Install dependencies
- [ ] Create required directories
- [ ] Install and configure PM2
- [ ] Set up subdomain in cPanel
- [ ] Configure reverse proxy (if needed)
- [ ] Test the application
- [ ] Set up automatic backups
- [ ] Set up deployment script
- [ ] Configure SSL certificate (Let's Encrypt via cPanel)
- [ ] Test deployment script

## SSL Certificate (HTTPS)

cPanel usually provides free Let's Encrypt SSL:

1. Go to cPanel → **SSL/TLS Status**
2. Find your subdomain
3. Click **Run AutoSSL**
4. Wait for certificate to be issued

Your site will now be accessible via `https://yoursubdomain.domain.com`

## Need Help?

- Check PM2 logs: `pm2 logs chocolate-ordering`
- Check Node.js version: `node --version` (should be v14 or higher)
- Check npm version: `npm --version`
- Verify git is pulling: `git pull origin main --dry-run`
