# 🏥 MediCare HMS - cPanel Deployment Guide

> **Complete Hospital, Dispensary & Online Doctor Management System**
> Version 1.0 | MySQL | Node.js | React

---

## 📋 System Requirements

| Requirement | Minimum | Recommended |
|-------------|---------|-------------|
| **cPanel Version** | v98+ (with Node.js Selector) | v110+ |
| **Node.js** | 18.x | 20.x LTS |
| **MySQL** | 5.7+ | 8.0+ |
| **PHP** | Not required | 8.x (optional) |
| **Disk Space** | 500 MB | 2 GB |
| **RAM** | 512 MB | 1 GB+ |
| **SSL Certificate** | Required for production | Let's Encrypt (free) |

---

## 📦 What's Included

```
cpanel-deploy/
├── backend/                    # Node.js backend (upload to cPanel)
│   ├── app.js                  # cPanel entry point
│   ├── .env.production         # Environment template
│   ├── package.json            # Dependencies
│   └── dist/                   # Compiled TypeScript
├── frontend/                   # React frontend (build & upload)
│   ├── dist/                   # Production build output
│   └── .htaccess               # Apache rewrite rules
├── database/
│   └── schema.sql              # MySQL full schema (69 tables)
├── scripts/
│   ├── build-backend.sh        # Build backend for production
│   ├── build-frontend.sh       # Build frontend for production
│   └── setup-cpanel.sh         # Automated cPanel setup
└── INSTALL.md                  # This guide
```

---

## 🚀 Deployment Steps (Detailed)

### Step 1: Prepare Your cPanel Environment

1. **Log into cPanel** (https://yourdomain.com/cpanel)
2. Go to **Setup Node.js App** (in Software section)
3. Note the available Node.js versions - ensure 18.x or 20.x is available

### Step 2: Create MySQL Database

1. In cPanel, go to **MySQL Databases**
2. Create a new database: `medicare_hms`
3. Create a database user with a strong password
4. Add the user to the database with **ALL PRIVILEGES**
5. Save the credentials somewhere safe

### Step 3: Import Database Schema

**Option A - Using phpMyAdmin:**
1. Go to **phpMyAdmin** in cPanel
2. Select your `medicare_hms` database
3. Click **Import** tab
4. Choose `database/schema.sql` from this package
5. Click **Go** - wait for import to finish (69 tables will be created)

**Option B - Using MySQL command line:**
```bash
mysql -u username -p medicare_hms < database/schema.sql
```

### Step 4: Upload Backend Files

1. In cPanel, go to **File Manager**
2. Navigate to your home directory (usually `/home/username/`)
3. Create a folder: `backend`
4. Upload ALL contents of `cpanel-deploy/backend/` into this folder
5. Also upload `cpanel-deploy/database/` folder (optional, for reference)

### Step 5: Configure Backend Environment

1. In File Manager, navigate to `backend/`
2. Copy `.env.production` to `.env`
3. Edit `.env` and update:
   ```
   DATABASE_URL="mysql://db_user:db_password@localhost/medicare_hms"
   DB_USER=your_database_user
   DB_PASSWORD=your_database_password
   JWT_SECRET=your-random-64-char-string  (generate at: https://randomkeygen.com)
   JWT_REFRESH_SECRET=another-random-string
   APP_URL=https://yourdomain.com
   CORS_ORIGIN=https://yourdomain.com
   SMTP_HOST=mail.yourdomain.com
   SMTP_USER=noreply@yourdomain.com
   SMTP_PASS=your-email-password
   ```

### Step 6: Set Up Node.js App (cPanel)

1. Go to **Setup Node.js App**
2. Click **Create Application**
3. Fill in:
   - **App path**: `backend` (relative to home directory)
   - **App entry point**: `app.js`
   - **App port**: `5000`
   - **App mode**: `Production`
   - **Node.js version**: Select 20.x or 18.x
4. Click **Create**

### Step 7: Install Dependencies & Build

1. After creating the app, you'll see **Run NPM Install** button
2. Click it and wait for installation to complete
3. If NPM Install fails via UI, use the **Terminal** in cPanel:

```bash
cd ~/backend
npm install --omit=dev
npx prisma generate
npx prisma db push
npx prisma db seed
```

### Step 8: Start Node.js App

1. In **Setup Node.js App**, find your app
2. Click **Start App** (or **Restart** if already running)
3. Verify the app shows **Running** status

### Step 9: Build & Upload Frontend

**On your local machine:**
```bash
cd frontend
npm install
npm run build
```

**Upload to cPanel:**
1. In File Manager, navigate to `public_html/`
2. Upload all contents from `frontend/dist/` into `public_html/`
3. Upload the `.htaccess` file from `cpanel-deploy/frontend/` into `public_html/`

### Step 10: Create Uploads Directory

```bash
cd ~/backend
mkdir -p uploads
chmod 755 uploads
```

---

## ✅ Post-Installation Checklist

- [ ] Node.js app shows **Running** status
- [ ] Visit `https://yourdomain.com` - login page loads
- [ ] Visit `https://yourdomain.com/api/health` - returns JSON with status "ok"
- [ ] Login as admin: `admin@medicare.com` / `admin123`
- [ ] Change default passwords immediately
- [ ] Set up SSL (Let's Encrypt) in cPanel
- [ ] Configure cron jobs for scheduled tasks (see below)

---

## 🔧 Configuration

### Cron Jobs (for automatic tasks)

Set these up in cPanel's **Cron Jobs** section:

```bash
# Daily midnight - clean expired sessions
0 0 * * * cd ~/backend && node dist/cron/cleanup.js

# Daily 6 AM - expiry alerts for medicines
0 6 * * * cd ~/backend && node dist/cron/expiry-alerts.js

# Every 30 minutes - send pending notifications
*/30 * * * * cd ~/backend && node dist/cron/notifications.js

# Weekly Sunday midnight - database backup
0 0 * * 0 mysqldump -u user -ppass medicare_hms > ~/backups/medicare_$(date +\%Y\%m\%d).sql
```

### SSL Certificate

1. In cPanel, go to **SSL/TLS Status**
2. Run **AutoSSL** (powered by cPanel) or
3. Use **Let's Encrypt SSL** from the Software section

---

## 🔐 Default Login Credentials

| Role | Email | Password | Action Required |
|------|-------|----------|-----------------|
| **Super Admin** | `admin@medicare.com` | `admin123` | 🔴 CHANGE NOW |
| **Doctor** | `doctor@medicare.com` | `doctor123` | 🔴 CHANGE NOW |
| **Receptionist** | `reception@medicare.com` | `reception123` | 🔴 CHANGE NOW |

> **⚠️ IMPORTANT**: Change all default passwords immediately after first login!

---

## 🔍 Troubleshooting

### "Cannot GET /" or 404 on frontend
→ Ensure `.htaccess` is present in `public_html/` with SPA rewrite rules

### "502 Bad Gateway" on API calls
→ Node.js app may be stopped. Restart it from cPanel's **Setup Node.js App**

### "ECONNREFUSED :5000"
→ Node.js app isn't running. Check app status and start it

### "Unknown database 'medicare_hms'"
→ Database not created. Go to **MySQL Databases** and create it

### "Client does not support authentication"
→ Update MySQL auth plugin:
```sql
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
```

### NPM install fails
→ Try via SSH/Terminal instead of cPanel UI:
```bash
cd ~/backend
npm cache clean --force
npm install --omit=dev
```

---

## 📱 Mobile App

The React Native mobile apps are in the `mobile/` directory. To deploy:

1. Build with Expo: `npx eas build --platform android`
2. Upload APK/AAB to Google Play Store
3. Update API URL in `mobile/src/services/api.ts` to your domain

---

## 🔄 Updating

```bash
# Backend update
cd ~/backend
npm install
npx prisma generate
npx prisma db push
npx prisma db seed
# Restart app from cPanel

# Frontend update
cd frontend  # on local machine
npm install
npm run build
# Upload dist/* to public_html/
```

---

## 🆘 Support

For issues and support:
- GitHub Issues: https://github.com/medicare-hms/issues
- Documentation: https://medicare-hms.com/docs
- Email: support@medicare-hms.com

---

## 📄 License

Proprietary - All Rights Reserved
© 2026 MediCare Health Systems
