# Install on Namecheap Shared Hosting

Follow these steps to deploy Prep on Namecheap shared hosting (cPanel).

---

## 1. Prerequisites

- A Namecheap shared hosting plan with **cPanel access**
- PHP 8.1 or 8.2 enabled in cPanel
- SSH access enabled (recommended) or use cPanel File Manager
- A domain or subdomain pointed to your hosting

---

## 2. Upload the project files

### Option A: Via SSH (recommended)

```bash
ssh youruser@yourserver.com
cd ~/public_html
git clone <your-repo-url> .
```

Or upload a ZIP and extract:

```bash
unzip prep.zip
mv prep/* prep/.* .  2>/dev/null
rmdir prep
```

### Option B: Via cPanel File Manager

1. Compress the project folder into a `.zip` file
2. In cPanel → **File Manager** → go to `public_html`
3. Click **Upload** → choose the `.zip`
4. After upload, right-click the `.zip` → **Extract**
5. Move all files out of the extracted folder into `public_html` if needed

---

## 3. Install PHP dependencies

### Via SSH

```bash
cd ~/public_html
composer install --no-dev --optimize-autoloader
```

### Without SSH

Run `composer install --no-dev` on your local machine, then upload the entire `vendor/` folder along with the project files. The `vendor/` folder must be included in your upload.

---

## 4. Create the .env file

Copy `.env.example` to `.env` and edit it:

```env
APP_ENV=production
APP_URL=https://yourdomain.com

DB_HOST=localhost
DB_NAME=yourcpuser_prep
DB_USER=yourcpuser_prepuser
DB_PASS=your-db-password

AI_PROVIDER=openai
AI_API_KEY=sk-your-key-here
AI_BASE_URL=https://api.openai.com/v1
AI_MODEL=gpt-4o
```

---

## 5. Create the database

1. In cPanel → **MySQL Databases**
2. Create a new database (e.g., `yourcpuser_prep`)
3. Create a new database user with a strong password
4. Add the user to the database with **ALL PRIVILEGES**
5. Update `.env` with the database name, user, and password

Then import the schema:

1. In cPanel → **phpMyAdmin**
2. Select your database from the left sidebar
3. Click the **Import** tab
4. Choose `database/schema.sql` from the project
5. Click **Go**

---

## 6. Configure the web root

Namecheap serves from `public_html/` by default. You need the web root to point to the `public/` folder.

### Option A: Modify the document root (recommended)

In cPanel, look for **Domains** → your domain → change the document root from `public_html` to `public_html/public`.

### Option B: Use .htaccess redirect

Create or edit `public_html/.htaccess`:

```apache
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
```

### Option C: Move files up

Move everything from `public/` into `public_html/`, and move `app/`, `storage/`, `vendor/` one level above `public_html/` (outside the web root). Update `config.php` paths accordingly. This is more secure but requires manual path changes.

---

## 7. Set directory permissions

### Via SSH

```bash
cd ~/public_html
chmod -R 755 storage
chmod -R 775 storage/uploads
chmod 640 .env
```

### Via cPanel File Manager

1. Right-click the `storage` folder → **Change Permissions**
2. Set to `755`
3. Right-click `storage/uploads` → set to `775`
4. Right-click `.env` → set to `640`

---

## 8. Configure PHP settings

In cPanel → **Select PHP Version**:

- Choose PHP 8.1 or 8.2
- Enable these extensions: `pdo_mysql`, `curl`, `fileinfo`, `mbstring`, `json`, `openssl`

In cPanel → **MultiPHP INI Editor** (select your domain), add:

```ini
display_errors = Off
log_errors = On
error_reporting = E_ALL
upload_max_filesize = 25M
post_max_size = 25M
max_execution_time = 120
```

---

## 9. Set up a cron job for cleanup

In cPanel → **Cron Jobs**, add:

```
Command: php /home/yourcpuser/public_html/app/helpers.php
```

Or create a small `cron.php` file:

```php
<?php
// Place this at public_html/cron.php
define('ROOT_DIR', dirname(__DIR__));
define('STORAGE_DIR', ROOT_DIR . '/storage');
define('UPLOAD_DIR', STORAGE_DIR . '/uploads');
require_once ROOT_DIR . '/app/helpers.php';
$removed = cleanup_old_uploads(24);
echo "Removed {$removed} old files.\n";
```

Cron command:

```
0 * * * * php /home/yourcpuser/public_html/cron.php
```

---

## 10. Verify the installation

1. Visit `https://yourdomain.com` — the upload form should appear
2. Upload a test PDF with a valid API key configured
3. Visit `https://yourdomain.com/result.php?id=1` — no raw errors
4. Check the error log in cPanel → **Errors** for any issues
5. View page source — no API keys, no file paths

---

## Troubleshooting

| Problem | Solution |
|---------|----------|
| 500 Internal Server Error | Check cPanel → **Errors** for the actual PHP error. Usually a missing extension or permission issue. |
| "Class not found" | Run `composer dump-autoload` or re-upload the `vendor/` folder |
| Database connection refused | Verify `.env` has the correct cPanel database credentials (user format: `cpaneluser_dbuser`) |
| Upload fails silently | Check `storage/uploads/` is writable (775) and `upload_max_filesize` in PHP settings |
| AI analysis fails | Verify `AI_API_KEY` is set and `AI_BASE_URL` is reachable from the server (Namecheap may block outbound connections on some plans — check with support) |
| Session errors | Namecheap shared hosting defaults may have short session timeouts. Add `session.gc_maxlifetime = 14400` to MultiPHP INI. |
