# Master Guide: Setting Up Secure FTP Deployments

This guide lists the exact steps and code structures needed to set up automated FTP/FTPS deployment in any of Madhav's 5 portals.

---

## Part 1: cPanel FTP Account Setup (Do this first)
For every portal, create two dedicated, restricted FTP accounts in cPanel -> **FTP Accounts**:

1.  **Staging Deployer Account:**
    *   **Log in:** `deployer_[portal_name]_staging`
    *   **Domain:** `sevenpointfour.in`
    *   **Password:** Generate a secure password.
    *   **Directory:** Lock it strictly to the staging directory, e.g., `nodeapps/staging_[portal_name]`
2.  **Production Deployer Account:**
    *   **Log in:** `deployer_[portal_name]_prod`
    *   **Domain:** `sevenpointfour.in`
    *   **Password:** Generate a secure password.
    *   **Directory:** Lock it strictly to the production directory, e.g., `nodeapps/[portal_name]`

---

## Part 2: Local Project Setup (In the new portal)

### Step 1: Install `basic-ftp` Dependency
Run this command in the portal's terminal:
```bash
npm install basic-ftp
```

### Step 2: Create local `.env.staging` and `.env.production` files
Create these files in the root folder of the new portal, pre-configured with the locked path (`FTP_PATH=/`):

#### **`.env.staging`**
```env
FTP_HOST=148.113.26.247
FTP_PORT=21
FTP_USER=deployer_[portal_name]_staging@sevenpointfour.in
FTP_PASS=[Staging_Password]
FTP_PATH=/
APP_URL=[Staging_Website_URL]
```

#### **`.env.production`**
```env
FTP_HOST=148.113.26.247
FTP_PORT=21
FTP_USER=deployer_[portal_name]_prod@sevenpointfour.in
FTP_PASS=[Production_Password]
FTP_PATH=/
APP_URL=[Production_Website_URL]
```

### Step 3: Update `.gitignore`
Add these lines to the project's `.gitignore` file to protect your credentials and log history from Git:
```text
.env.staging
.env.production
deploy_history.log
deploy.lock
```

### Step 4: Add Deployment Script (`scripts/deploy.mjs`)
Create the deployment controller script at `scripts/deploy.mjs` inside the project. Copy the verified implementation from the Consultation portal.

### Step 5: Add NPM shortcuts to `package.json`
Add these lines inside the `"scripts"` block in the project's `package.json` file:
```json
"deploy-staging": "node scripts/deploy.mjs staging",
"deploy-production": "node scripts/deploy.mjs production"
```

---

## Part 3: Running Deployments & Testing

*   **To Test (Dry Run):**
    ```powershell
    npm run deploy-staging -- --dry-run
    ```
*   **To Deploy to Staging:**
    ```powershell
    npm run deploy-staging
    ```
*   **To Deploy to Production:**
    ```powershell
    npm run deploy-production
    ```
