# cPanel Setup Quick Reference

## Pre-Deployment Checklist

- [ ] Files uploaded to ~/apps/inventory (or your chosen path)
- [ ] PostgreSQL database created in cPanel
- [ ] PostgreSQL user created and assigned to database
- [ ] .htaccess file is in application root
- [ ] All files have .py, .html, .css, .js extensions (no hidden files)

## Environment Variables to Set in cPanel

Copy these into Setup Python App → Environment Variables:

```
DATABASE_URL=postgresql://DB_USER:DB_PASSWORD@localhost:5432/DB_NAME
SECRET_KEY=<generate: python -c "import os; print(os.urandom(32).hex())">
ADMIN_USERNAME=admin
ADMIN_PASSWORD_HASH=<generate: python -c "import bcrypt; print(bcrypt.hashpw(b'yourpassword', bcrypt.gensalt()).decode())">
```

Optional (leave empty to skip):
```
CLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
```

## Database Setup Steps

1. **Create Database**: cPanel → PostgreSQL® Databases → Add Database
2. **Create User**: cPanel → PostgreSQL® Databases → Add User  
3. **Assign User**: cPanel → PostgreSQL® Databases → Manage Database → Add User
4. **Get Credentials**: Username and password shown during creation
5. **Build CONNECTION_URL**: `postgresql://username:password@localhost:5432/dbname`

Example:
```
DATABASE_URL=postgresql://myuser:MySecurePass123@localhost:5432/mydb_inventory
```

## Installation in cPanel

1. **Setup Python App**
   - Click "Create Application"
   - Python version: 3.9 or higher
   - Application root: /home/username/apps/inventory
   - Startup file: passenger_wsgi.py
   - Entry point: application
   - Click "Create"

2. **Install Dependencies**
   - Run Pip Install
   - Wait for completion

3. **Set Environment Variables**
   - Add all variables from section above

4. **Create Upload Directory**
   - In Terminal: `mkdir -p ~/apps/inventory/static/uploads`
   - Set permissions: `chmod 755 ~/apps/inventory/static/uploads`

5. **Restart Application**
   - Click "Restart" button

## Verify Installation

1. Visit `yourdomain.com/health`
   - Should return: `{"status": "healthy", "database": "connected", "cloudinary": "disabled"}`

2. Visit `yourdomain.com/login`
   - Should show login page

3. Login with
   - Username: admin
   - Password: whatever you hashed for ADMIN_PASSWORD_HASH

## If Something Goes Wrong

1. **Check Logs**: Setup Python App → View Error Logs
2. **Test Database Connection**: Run `/health` endpoint
3. **Check .htaccess**: Ensure it's in application root
4. **Verify Permissions**: `ls -la` on static/ and static/uploads/
5. **Test Upload**: Try uploading an image in admin panel
6. **Email cPanel Support**: Provide error log excerpts if stuck

## Common Error Solutions

| Error | Solution |
|-------|----------|
| "Database connection refused" | Check DATABASE_URL, verify PostgreSQL running |
| "Internal Server Error" | Check error logs in Setup Python App |
| "CSS/JS not loading" | Verify .htaccess in root, check mod_rewrite enabled |
| "Can't upload images" | Check static/uploads/ exists and is writable |
| "Login doesn't work" | Verify ADMIN_PASSWORD_HASH was set correctly |

## Backup & Maintenance

**Daily Backups**:
- cPanel → Backup Wizard → Backup Home Directory

**Database Backups**:
- cPanel → PostgreSQL® Databases → Manage Database → Backup

**Check Health Weekly**:
- Visit `/health` endpoint
- Check error logs in Setup Python App

## Key Files to Know

| File | Purpose |
|------|---------|
| passenger_wsgi.py | Entry point for Passenger (don't modify) |
| config.py | Configuration settings |
| app.py | Main Flask application |
| .htaccess | URL routing and static file handling |
| requirements.txt | Python dependencies |
| static/ | CSS, JS, images |
| static/uploads/ | User-uploaded images |
| templates/ | HTML files |

## Migration Tip: Importing Old Database

If migrating from Fly.io/Supabase:

```bash
# Download dump from old database
pg_dump "old_database_url" > dump.sql

# Import via cPanel Terminal
cd ~/apps/inventory
psql "postgresql://newuser:newpass@localhost:5432/newdb" < dump.sql
```

## Performance Tips

1. Clear browser cache if CSS/JS changes don't show
2. Restart app if environment variables change
3. Monitor database usage in cPanel
4. Delete old uploaded images periodically
5. Keep dependencies updated: `pip install --upgrade -r requirements.txt`

## Security Reminders

- [ ] Change admin password from default
- [ ] Use strong ADMIN_PASSWORD_HASH (12+ characters)
- [ ] Enable HTTPS (cPanel AutoSSL usually enables automatically)
- [ ] Backup database regularly
- [ ] Monitor error logs for suspicious activity
- [ ] Keep Python dependencies updated

---

**Last Updated**: December 2024
**cPanel Version**: Compatible with cPanel 102+
**Python**: 3.9, 3.10, 3.11, 3.12
**Status**: Ready for Production
