# cPanel Deployment Guide — Photo QR Cloud Server

Deploy the cloud server to `photo.velanhousing.com` using cPanel's Python App feature.

---

## Step 1: Create the Python App in cPanel

1. **Log in** to your cPanel at your GoViral hosting panel
2. Navigate to **"Setup Python App"** (under Software section)
3. Click **"Create Application"**
4. Fill in these settings:

| Setting | Value |
|---------|-------|
| **Python version** | `3.9` or higher (whatever is available) |
| **Application root** | `photo_qr` (this creates `~/photo_qr/` on the server) |
| **Application URL** | `photo.velanhousing.com` |
| **Application startup file** | `passenger_wsgi.py` |
| **Application Entry point** | `application` |

5. Click **"Create"**

> [!IMPORTANT]
> After creating, cPanel will show a **virtual environment activation command** at the top. It looks like:
> ```
> source /home/velanho1/virtualenv/photo_qr/3.9/bin/activate
> ```
> **Copy this command** — you'll need it in Step 3.

---

## Step 2: Upload Files via cPanel File Manager

1. Open **File Manager** in cPanel
2. Navigate to `/home/velanho1/photo_qr/` (your Application root)
3. **Upload** these files from your `cloud-server/` folder:

```
photo_qr/
├── app.py                  ← Main FastAPI server
├── passenger_wsgi.py       ← Passenger WSGI bridge
├── requirements.txt        ← Python dependencies
├── .env                    ← Environment config
└── templates/
    └── download.html       ← Mobile download page
```

**How to upload:**
- Click **"Upload"** button in File Manager
- Drag & drop `app.py`, `passenger_wsgi.py`, `requirements.txt`, `.env`
- Create a folder called `templates` → upload `download.html` inside it
- Create a folder called `uploads` (this is where photos will be stored)

> [!TIP]
> **Alternative:** Use the **Terminal** in cPanel (if available) with `git clone` or use **FTP** with FileZilla.

---

## Step 3: Install Dependencies via Terminal

1. Open **Terminal** in cPanel (or use SSH)
2. **Activate** the virtual environment (paste the command from Step 1):

```bash
source /home/velanho1/virtualenv/photo_qr/3.9/bin/activate
```

3. Navigate to your app directory:

```bash
cd ~/photo_qr
```

4. **Install** the Python packages:

```bash
pip install -r requirements.txt
```

Wait for all packages to install successfully.

---

## Step 4: Create the Uploads Directory

Still in the terminal:

```bash
mkdir -p ~/photo_qr/uploads
chmod 755 ~/photo_qr/uploads
```

---

## Step 5: Verify the `.env` File

Make sure your `.env` file has the correct values:

```bash
cat ~/photo_qr/.env
```

It should show:

```
DB_HOST=ns3.goviralhost.org
DB_USER=velanho1_mukeshbooth
DB_NAME=velanho1_booth
DB_PASSWORD=Mukesh@227
DB_PORT=3306
API_KEY=pqr-8x4Km9Wv2nLbT7jR5hYdFcA3eZsU
UPLOAD_DIR=uploads
BASE_URL=https://photo.velanhousing.com
```

---

## Step 6: Restart the Application

1. Go back to **"Setup Python App"** in cPanel
2. Find your `photo_qr` application
3. Click **"Restart"** button

---

## Step 7: Test the Deployment

### Check health endpoint:
Open in your browser:
```
https://photo.velanhousing.com/health
```

You should see:
```json
{"status": "healthy", "database": "connected"}
```

### Test with a photo upload (from terminal/command prompt):
```bash
curl -X POST https://photo.velanhousing.com/upload \
  -H "X-API-Key: pqr-8x4Km9Wv2nLbT7jR5hYdFcA3eZsU" \
  -F "file=@test_photo.jpg"
```

---

## Step 8: DNS Setup (if not already done)

Make sure `photo.velanhousing.com` points to your hosting:

1. In cPanel → **Subdomains** (or your domain registrar's DNS)
2. Create subdomain `photo` pointing to your hosting's IP
3. If your main domain `velanhousing.com` is already on this cPanel, the subdomain should auto-resolve

---

## Troubleshooting

| Issue | Fix |
|-------|-----|
| **500 Internal Server Error** | Check `~/photo_qr/stderr.log` for errors |
| **Database connection failed** | Verify MariaDB credentials in `.env`. Make sure remote MySQL is enabled for the DB host |
| **Module not found** | Re-run `pip install -r requirements.txt` inside the virtualenv |
| **Permission denied on uploads** | Run `chmod 755 ~/photo_qr/uploads` |
| **App not starting** | Verify `passenger_wsgi.py` exists and `Application Entry point` is set to `application` |

### View error logs:
```bash
cat ~/photo_qr/stderr.log
```

---

## Optional: Auto-Cleanup Cron Job

Set up a cron job to clean expired photos automatically:

1. In cPanel → **Cron Jobs**
2. Add a new cron job running **every hour**:

```
0 * * * * curl -s -X DELETE https://photo.velanhousing.com/cleanup -H "X-API-Key: pqr-8x4Km9Wv2nLbT7jR5hYdFcA3eZsU" > /dev/null 2>&1
```

This deletes expired photos (older than 3 hours) from disk every hour.
