# MyTree Installation Guide

This guide explains how to install the MyTree database scaffold on a cPanel hosting account.

## Requirements

- cPanel hosting with MySQL or MariaDB.
- PHP with PDO MySQL enabled.
- Access to cPanel File Manager, FTP, or SSH.
- Access to phpMyAdmin or the browser-based installer.

## Included Files

- `database/mytree_schema.sql` - SQL schema for all MyTree tables.
- `config/database.php` - database connection configuration.
- `install_database.php` - one-time installer that creates tables automatically.
- `README_DATABASE_SETUP.md` - short database setup reference.

## Step 1: Create Database In cPanel

1. Log in to cPanel.
2. Open **MySQL Databases**.
3. Create a new database, for example:

   ```text
   cpaneluser_mytree
   ```

4. Create a new database user, for example:

   ```text
   cpaneluser_mytree
   ```

5. Assign the user to the database.
6. Grant **ALL PRIVILEGES**.
7. Save the database name, username, and password.

## Step 2: Upload Project Files

Upload the project files to your hosting account.

For a standard cPanel site, upload these files into your application directory:

```text
config/database.php
database/mytree_schema.sql
install_database.php
README_DATABASE_SETUP.md
INSTALLATION_GUIDE.md
```

If this is placed directly inside `public_html`, make sure you delete `install_database.php` after setup.

## Step 3: Configure Database Credentials

Open `config/database.php` and update these values:

```php
'database' => mytree_env('MYTREE_DB_NAME', 'cpaneluser_mytree'),
'username' => mytree_env('MYTREE_DB_USER', 'cpaneluser_mytree'),
'password' => mytree_env('MYTREE_DB_PASS', 'your_database_password'),
```

Usually, cPanel uses `localhost` as the database host:

```php
'host' => mytree_env('MYTREE_DB_HOST', 'localhost'),
```

Set a strong installer key before using the browser installer:

```php
'install_key' => mytree_env('MYTREE_INSTALL_KEY', 'replace-with-a-long-random-key'),
```

Example:

```php
'install_key' => mytree_env('MYTREE_INSTALL_KEY', '7d9f5a9e1f4b4c0a8e2d6c3b1a9f0e7d'),
```

## Step 4: Install The Database

You can install the database in either of these ways.

### Option A: Browser Installer

Visit this URL in your browser:

```text
https://your-domain.com/install_database.php?key=YOUR_INSTALL_KEY
```

Replace:

- `your-domain.com` with your actual domain.
- `YOUR_INSTALL_KEY` with the key you set in `config/database.php`.

If successful, you should see:

```text
MyTree database installation complete.
```

### Option B: phpMyAdmin Import

1. Open cPanel.
2. Open **phpMyAdmin**.
3. Select your MyTree database.
4. Click **Import**.
5. Upload `database/mytree_schema.sql`.
6. Click **Go**.

## Step 5: Verify Tables

After installation, open phpMyAdmin and confirm the database contains tables such as:

```text
mt_users
mt_tree_types
mt_trees
mt_tree_updates
mt_wallets
mt_wallet_transactions
mt_donations
mt_referrals
mt_notifications
mt_fraud_alerts
mt_admin_settings
```

The full schema currently creates 21 tables.

## Step 6: Secure The Installation

After successful installation:

1. Delete `install_database.php`.
2. Keep database passwords private.
3. Do not expose full KYC document numbers in the database.
4. Use HTTPS for the live domain.
5. Restrict file permissions where possible:

   ```text
   Files: 644
   Directories: 755
   ```

6. Keep uploaded user images outside sensitive system directories.

## Configuration With Environment Variables

If your hosting setup supports environment variables, you can avoid hardcoding credentials:

```text
MYTREE_DB_HOST=localhost
MYTREE_DB_PORT=3306
MYTREE_DB_NAME=cpaneluser_mytree
MYTREE_DB_USER=cpaneluser_mytree
MYTREE_DB_PASS=your_database_password
MYTREE_INSTALL_KEY=your_long_random_install_key
```

The app will use environment variables before falling back to the values in `config/database.php`.

## Troubleshooting

### Invalid Install Key

Check that the URL key exactly matches `install_key` in `config/database.php`.

### Access Denied For User

Confirm:

- Database username is correct.
- Database password is correct.
- The database user has been assigned to the database.
- The user has **ALL PRIVILEGES**.

### Unknown Database

Confirm the database name includes your cPanel prefix, for example:

```text
cpaneluser_mytree
```

### PDO Driver Error

Ask your hosting provider to enable PHP PDO MySQL. The PHP extension is usually named:

```text
pdo_mysql
```

### Schema Import Fails

Use a recent MySQL or MariaDB version. If your host uses an old database version that does not support `CHECK` constraints, import through the browser installer first. If it still fails, remove the `CHECK` constraint lines from `database/mytree_schema.sql`.

## Production Notes

- Use payment gateway webhooks for donation and withdrawal status updates.
- Store Aadhaar/PAN values as masked text plus hashes, not full raw numbers.
- Store image files on disk or object storage and save only paths/hashes in the database.
- Run fraud checks in application code before approving trees and weekly updates.
- Add scheduled jobs for weekly update reminders and unpaid withdrawal checks.
