# 🚀 PharmaSFA Pro — Step-by-Step Deployment Guide
## For: https://sfa.apansewa.com (Database: apansewa_sfa)

> **No programming knowledge needed.** Just follow each step in order.
> Each step has a ✅ so you know when it's done.

---

## 📋 BEFORE YOU START — What you need

1. ✅ Your cPanel login (username: likely `apansewa`, password: your cPanel password)
2. ✅ The database `apansewa_sfa` already created with `schema.sql` imported (you did this earlier)
3. ✅ The deployment file `pharmasfa-pro-deploy.zip` downloaded (from the Preview Panel)

**How to download the deployment file:**
- In the Preview Panel (right side of your screen), open the app
- Visit this URL in the preview: `https://preview-chat-066ce573-9caa-48fb-9baf-f54c2e9f4c21.space-z.ai/pharmasfa-pro-deploy.zip`
- Or click "Open in New Tab" above the Preview Panel, then add `/pharmasfa-pro-deploy.zip` to the URL
- The file (32 MB) will download to your computer

---

## STEP 1: Create a Database User (if not done yet)

Your database `apansewa_sfa` needs a **user with a password** so the app can connect to it.

1. **Log into cPanel** → go to: https://apansewa.com/cpanel (or your hosting provider's cPanel URL)
2. Scroll down to the **"Databases"** section
3. Click **"MySQL Database Wizard"**
4. **Step 1 — New Database:**
   - You already have `apansewa_sfa` — so just click **"Go Back"**
   - Instead, click **"MySQL Databases"** (not the wizard)
5. **In "MySQL Databases" page, scroll to "Add New User":**
   - Username: type `pharma` (it becomes `apansewa_pharma`)
   - Password: make a strong password and **WRITE IT DOWN** (you need it later)
   - Click **"Create User"**
   - ✅ You see "You have successfully created a user"

6. **Scroll to "Add User to Database":**
   - User: select `apansewa_pharma`
   - Database: select `apansewa_sfa`
   - Click **"Add"**
7. **On the next page:**
   - Check the box at top: **"ALL PRIVILEGES"** (this checks all boxes)
   - Click **"Make Changes"**
   - ✅ You see "User apansewa_pharma was successfully added to the database"

**📝 Write down these 3 things (you need them in Step 6):**
```
Database Name: apansewa_sfa
Database User: apansewa_pharma
Database Password: ___________ (the password you just made)
```

---

## STEP 2: Open cPanel File Manager

1. In cPanel, scroll to the **"Files"** section
2. Click **"File Manager"**
3. A popup appears — select **"Web Root (public_html)"** → click **"Go"**
4. You now see your `public_html` folder contents

✅ You're in File Manager

---

## STEP 3: Create a Folder for the App

1. In File Manager, click the **"+ Folder"** button (top menu)
2. **New Folder Name:** type `sfa`
3. Click **"Create New Folder"**
4. You now see a folder named `sfa` in the list
5. **Double-click the `sfa` folder** to open it

✅ You're inside the `sfa` folder (the address bar shows `/public_html/sfa`)

---

## STEP 4: Upload the Deployment File

1. Inside the `sfa` folder, click the **"Upload"** button (top menu)
2. A new tab opens for uploading
3. Click **"Select File"** or drag-and-drop the `pharmasfa-pro-deploy.zip` file
4. Wait for the upload to complete (32 MB — takes 2-5 minutes)
5. ✅ You see a green checkmark or "Upload Complete"
6. **Go back to File Manager** (close the upload tab, or click "Go Back to /home/apansewa/public_html/sfa")
7. You now see `pharmasfa-pro-deploy.zip` in the `sfa` folder

---

## STEP 5: Extract the ZIP File

1. **Right-click** on `pharmasfa-pro-deploy.zip`
2. Click **"Extract"**
3. A popup asks "Extract to: /public_html/sfa/" — change it to `/public_html/sfa/` if not already
4. Click **"Extract File(s)"**
5. ✅ You see a list of extracted files
6. Click **"Close"**
7. You now see:
   - A folder named `deploy-package`
   - The original zip file (you can delete the zip now — right-click → Delete)

8. **Double-click the `deploy-package` folder** to open it
9. You see: `app` (folder), `DEPLOY.md`, `.env.example`, `schema.sql`, `start.sh`, etc.

10. **Select ALL files** inside `deploy-package`:
    - Click the first file (`app`)
    - Hold **Shift** and click the last file
    - All files should be highlighted

11. **Right-click** any highlighted file → **"Move"**
12. In the popup, change the path to: `/public_html/sfa/`
13. Click **"Move File(s)"**
14. ✅ All files are now in `/public_html/sfa/`
15. **Go up one level** (click `↑` or the `sfa` breadcrumb) — delete the now-empty `deploy-package` folder

✅ Your `sfa` folder now contains: `app`, `.env.example`, `DEPLOY.md`, `schema.sql`, `start.sh`

---

## STEP 6: Create Your .env File (THE MOST IMPORTANT STEP)

1. Inside the `sfa` folder, find the file named **`.env.example`**
2. **Right-click** on `.env.example` → click **"Copy"**
3. In the popup, the destination is `/public_html/sfa/.env.example.copy` — change it to:
   ```
   /public_html/sfa/.env
   ```
   (remove `.example.copy` and just type `.env`)
4. Click **"Copy File(s)"**
5. ✅ You now see a file named `.env` (it might be hidden — click "Settings" → check "Show Hidden Files" if you don't see it)

6. **Right-click** on the new `.env` file → click **"Edit"**
7. A popup asks about encoding — click **"Edit"**
8. The file opens in a text editor. You see:

```
DATABASE_URL="mysql://apansewa_pharma:YOUR_DB_PASSWORD@localhost:3306/apansewa_sfa"
NEXTAUTH_SECRET="REPLACE_WITH_40_CHAR_RANDOM_STRING_FROM_ABOVE_WEBSITE"
NEXTAUTH_URL="https://sfa.apansewa.com"
NODE_ENV="production"
PORT=3000
```

9. **Make these 2 changes:**

   **Change 1:** Replace `YOUR_DB_PASSWORD` with the database password you wrote down in Step 1.
   Example: if your password is `MyPass123!`, the line becomes:
   ```
   DATABASE_URL="mysql://apansewa_pharma:MyPass123!@localhost:3306/apansewa_sfa"
   ```

   **Change 2:** Replace `REPLACE_WITH_40_CHAR_RANDOM_STRING_FROM_ABOVE_WEBSITE` with a random string:
   - Open a new browser tab → go to: https://generate-random.org/api/random-string?length=40
   - Copy the 40-character string it shows
   - Paste it between the quotes. Example:
   ```
   NEXTAUTH_SECRET="k7mX9pQ2vR8wY3tF6nB1cL5jH4gZ0aD8sE2"
   ```

10. **Click "Save Changes"** (top right) → then **"Close"**

✅ Your `.env` file is saved with real credentials

---

## STEP 7: Set Up the Node.js App (THE KEY STEP)

This step makes your app "live" on the internet.

1. Go back to cPanel main page (click "cPanel Home" or your hosting dashboard)
2. Scroll to the **"Software"** section
3. Click **"Setup Node.js App"**

   > ⚠️ **Don't see "Setup Node.js App"?**
   > Your hosting plan doesn't support Node.js apps. You need to either:
   > - Ask your hosting provider to enable Node.js for your account, OR
   > - Upgrade to a plan that supports Node.js, OR
   > - Use a different host like Vercel (free) or Render (free)

4. Click the **"Create Application"** button (top right)
5. Fill in the form exactly like this:

   | Field | What to type/select |
   |-------|-------------------|
   | **Node.js version** | Select `18.19.0` (or the highest 18.x or 20.x available) |
   | **Application mode** | Select `Production` |
   | **Application root** | Type `sfa/app` (this points to `/public_html/sfa/app`) |
   | **Application URL** | Select your domain: `sfa.apansewa.com` (if it's a subdomain) OR your main domain |
   | **Application startup file** | Type `server.js` |
   | **Passenger log file** | Leave as default |

6. Scroll down — find the **"Environment variables"** section
7. Click **"+ Add Variable"** and add these 4 variables (one at a time):

   | Variable name | Variable value |
   |---------------|----------------|
   | `DATABASE_URL` | `mysql://apansewa_pharma:YOUR_DB_PASSWORD@localhost:3306/apansewa_sfa` (use your real password) |
   | `NEXTAUTH_SECRET` | (the 40-char random string from Step 6) |
   | `NEXTAUTH_URL` | `https://sfa.apansewa.com` |
   | `NODE_ENV` | `production` |

8. Click **"Create"** (or "Save") at the bottom

✅ You see "Application created successfully" and a green "Running" indicator

---

## STEP 8: Install Dependencies (Run NPM Install)

1. On the same "Setup Node.js App" page, find your app in the list
2. Click the **pencil icon (Edit)** next to your app
3. Scroll down to find the **"Run NPM Install"** button → click it
4. Wait 1-2 minutes — you'll see output in the box below
5. ✅ You see something like "added 350 packages in 45s"

> If you see errors here, don't panic. Scroll down to the next step.

---

## STEP 9: Generate the Prisma Database Client (IMPORTANT!)

This step connects your app to the MySQL database. Without it, the app won't read/write data.

1. On the same "Setup Node.js App" edit page, find the **"Run script"** field
2. In the box next to "Run script", type:
   ```
   npx prisma generate
   ```
3. Click the **"Run script"** button
4. Wait 30 seconds — you'll see output like:
   ```
   ✔ Generated Prisma Client (v6.19.2) to ./node_modules/@prisma/client
   ```
5. ✅ You see "Generated Prisma Client"

> **If this fails** with an error about `prisma/schema.prisma`:
> - Open cPanel File Manager → go to `/public_html/sfa/app/`
> - Check if the `prisma` folder is there (it should contain `schema.prisma`)
> - If missing, re-extract the zip and copy the `app/prisma` folder

---

## STEP 10: Restart the App

1. On the "Setup Node.js App" page, find your app
2. Click the **"Restart"** button (circular arrow icon)
3. Wait 10 seconds
4. ✅ You see "Running" with a green dot

---

## STEP 11: Test Your Website! 🎉

1. Open a new browser tab
2. Type your URL: **https://sfa.apansewa.com**
3. Press Enter
4. ✅ You should see the **PharmaSFA Pro login page** (green/teal design with a doctor icon)

5. **Log in:**
   - Username: `admin`
   - Password: `admin@123`
   - Click "Continue"
   - MFA screen: type any 6 digits (like `123456`) → click "Verify & Login"

6. ✅ You see the **Dashboard** with charts, KPIs, and menus

7. **Test the database connection:**
   - Click **"Database (MSfa_db)"** in the left sidebar (under "System")
   - You should see your real database tables and the connection info showing `apansewa_sfa`

8. **Test that data is real:**
   - Click **"Doctors"** in the left sidebar
   - You should see the 10 doctors you imported from `schema.sql`

🎉 **CONGRATULATIONS! Your app is LIVE!**

---

## STEP 12: Enable SSL (HTTPS — the lock icon)

1. Go back to cPanel main page
2. Scroll to the **"Security"** section
3. Click **"SSL/TLS Status"**
4. Click **"Run AutoSSL"** (top right)
5. Wait 2-5 minutes — it generates free SSL certificates
6. ✅ You see green locks next to your domains
7. Visit `https://sfa.apansewa.com` — you should see a **🔒 lock icon** in the browser address bar

---

## 🔐 STEP 13: Change the Admin Password (VERY IMPORTANT!)

The password `admin@123` is publicly known. Change it now:

1. Go to cPanel → **"phpMyAdmin"** (in the Databases section)
2. Click on **`apansewa_sfa`** in the left sidebar
3. Click on the **`users`** table
4. Click **"Edit"** (pencil icon) next to the `admin` user
5. Find the `password_hash` field
6. Delete the existing long string and replace it with a new bcrypt hash:
   - Open https://bcrypt-generator.com/ in a new tab
   - Type your new password (e.g., `MySecurePass2025!`)
   - Click "Generate"
   - Copy the result (starts with `$2b$12$...`)
   - Paste it into the `password_hash` field in phpMyAdmin
7. Click **"Go"** (or "Save") at the bottom
8. ✅ Password updated

Now log out and log back in with your new password.

---

## 🆘 TROUBLESHOOTING — Common Problems

### Problem 1: "This site can't be reached" or "502 Bad Gateway"
**Solution:**
- Go to cPanel → "Setup Node.js App"
- Check if your app shows "Running" (green dot). If red, click "Restart"
- Wait 30 seconds, try again

### Problem 2: "503 Service Unavailable"
**Solution:**
- The Node.js app crashed. Click "Restart" in Setup Node.js App
- Check the "Passenger log" for errors (cPanel → "Terminal" → run `tail -50 /home/apansewa/logs/passenger.log`)

### Problem 3: Website loads but shows "demo mode" (not real database data)
**Solution:**
- Visit `https://sfa.apansewa.com/api/health` in your browser
- If it says `"db":"disconnected"`, your `DATABASE_URL` is wrong
- Go back to Step 6 and fix the `.env` file:
  - Check the database name is `apansewa_sfa` (not `MSfa_db`)
  - Check the username is `apansewa_pharma`
  - Check the password is correct
- Then restart the app (Step 10)

### Problem 4: "Cannot find module '@prisma/client'"
**Solution:**
- Go to cPanel → "Setup Node.js App" → Edit your app
- Click "Run NPM Install" again
- Then run `npx prisma generate` in the "Run script" box
- Restart the app

### Problem 5: "Loading chunk app/page failed" error in browser
**Solution:**
- This is a browser cache issue. Do a **hard refresh**:
  - Windows: Press `Ctrl + Shift + R`
  - Mac: Press `Cmd + Shift + R`
- Or clear your browser cache completely

### Problem 6: Charts don't show / blank page
**Solution:**
- Open browser DevTools (press `F12`)
- Click "Console" tab — look for red error messages
- Most common fix: restart the app in cPanel

### Problem 7: Can't find "Setup Node.js App" in cPanel
**Solution:**
- Your hosting plan doesn't support Node.js
- Contact your hosting provider and ask: "How do I run a Node.js app?"
- Or switch to a Node-friendly host:
  - **Vercel** (free, easiest — https://vercel.com)
  - **Render** (free tier — https://render.com)
  - **Railway** (free trial — https://railway.app)

---

## 📞 Need Help?

If you get stuck on any step:

1. **Take a screenshot** of where you are
2. **Note the step number** where you're stuck
3. **Copy any error message** you see
4. Ask for help with: "I'm stuck on Step X of the PharmaSFA deployment. Here's the error: [paste error]"

---

## ✅ Quick Reference Card

| Item | Value |
|------|-------|
| **Website URL** | https://sfa.apansewa.com |
| **Database Name** | apansewa_sfa |
| **Database User** | apansewa_pharma |
| **Database Password** | (the one you created in Step 1) |
| **App Folder** | /public_html/sfa/app |
| **Startup File** | server.js |
| **Default Login** | admin / admin@123 (change in Step 13!) |
| **Health Check URL** | https://sfa.apansewa.com/api/health |
| **Schema Download** | https://sfa.apansewa.com/schema.sql |

---

## 🎯 Summary of All Steps

| Step | What you do | Time |
|------|-------------|------|
| 1 | Create database user `apansewa_pharma` | 2 min |
| 2 | Open cPanel File Manager | 1 min |
| 3 | Create folder `sfa` | 1 min |
| 4 | Upload `pharmasfa-pro-deploy.zip` | 5 min |
| 5 | Extract the zip | 2 min |
| 6 | Create and edit `.env` file | 5 min |
| 7 | Set up Node.js App in cPanel | 5 min |
| 8 | Run NPM Install | 2 min |
| 9 | Run `npx prisma generate` | 1 min |
| 10 | Restart the app | 1 min |
| 11 | Test your website! 🎉 | 2 min |
| 12 | Enable SSL (HTTPS) | 5 min |
| 13 | Change admin password | 3 min |

**Total time: ~35 minutes**

---

**Good luck! Your PharmaSFA Pro will be live at https://sfa.apansewa.com** 🚀
