# Deploying to cPanel (Python/Passenger) - Migration from Fly.io & Supabase

This project is a Flask app designed to run on cPanel using Passenger WSGI.

## Prerequisites
- A cPanel account with "Setup Python App" feature enabled
- Python 3.9+ available on your host
- PostgreSQL database (created via cPanel → PostgreSQL® Databases)

## Key Changes from Previous Hosting (Fly.io + Cloudinary)
- **Image Storage**: Now uses local file storage (`static/uploads/`) instead of Cloudinary
- **Database**: PostgreSQL on cPanel localhost instead of Supabase cloud
- **Environment Variables**: Set via cPanel UI instead of `.env` files
- **WSGI Server**: Passenger instead of Gunicorn
- **Static Files**: Served via `.htaccess` rewrite rules

## Files Used by cPanel
- `passenger_wsgi.py` - WSGI entry point for Passenger
- `requirements.txt` - Python dependencies (updated for cPanel)
- `app.py` - Main Flask application
- `.htaccess` - URL rewriting and static file handling
- `templates/` and `static/` - Flask templates and static assets
- `config.py` - Configuration with enhanced DATABASE_URL parsing

## Quick Setup Steps (cPanel UI)
1. Upload this folder to your preferred application path (e.g., `~/apps/inventory`).
2. In cPanel, open "Setup Python App":
   - Select a Python version (3.9+ recommended).
   - Set "Application root" to the directory you uploaded (e.g., `apps/inventory`).
   - Set "Application startup file" to `passenger_wsgi.py`.
   - Set "Application entry point" to `application`.
   - Click "Create".
3. After the app is created, click "Run Pip Install" and install dependencies from `requirements.txt`.
4. Configure Environment Variables:
   - `DATABASE_URL` (recommended): `postgresql://cpanel_user:password@localhost:5432/database_name`
   - OR use individual variables: `DB_HOST`, `DB_USER`, `DB_PASSWORD`, `DB_NAME`, `DB_PORT`
   - `SECRET_KEY`: Random string (32+ characters)
   - `ADMIN_USERNAME`: admin
   - `ADMIN_PASSWORD_HASH`: bcrypt hash (see guide below)
   - Optional Cloudinary: `CLOUDINARY_CLOUD_NAME`, `CLOUDINARY_API_KEY`, `CLOUDINARY_API_SECRET` (leave empty for local storage)
5. Click "Restart" application.

## Important Setup Notes

### PostgreSQL on cPanel
1. Go to cPanel → PostgreSQL® Databases
2. Create database (e.g., `username_inventory`)
3. Create user (e.g., `username_dbuser`)
4. Assign user to database with all privileges
5. Use these in DATABASE_URL: `postgresql://username_dbuser:password@localhost:5432/username_inventory`

### Generate Admin Password
```bash
python -c "import bcrypt; password = input('Enter password: '); print(bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode())"
```

### Create Upload Directory
```bash
mkdir -p static/uploads
chmod 755 static/uploads
```

## Code Updates for cPanel Compatibility

The following changes have been made:
1. **config.py**: Enhanced DATABASE_URL parsing using `urllib.parse` (handles special chars in passwords)
2. **app.py**: Improved database connection with proper port handling and error messages
3. **requirements.txt**: Removed gunicorn (not needed with Passenger), added python-dotenv
4. **.htaccess**: URL rewriting, static file caching, security headers

## Notes
- Static files & uploads: Flask serves from `static/`. Image uploads are saved to `static/uploads/` on disk. Ensure directory exists and is writable.
- Database: Use PostgreSQL on cPanel localhost. Create database and user via cPanel UI.
- Cloudinary: Leave empty to use local storage. Add credentials to use Cloudinary instead.
- Logs: Check Passenger logs via Setup Python App screen or cPanel error logs.
- Health Check: Visit `/health` to verify database connectivity.

## Troubleshooting

### Database Connection Issues
- Verify DATABASE_URL format: `postgresql://user:password@localhost:5432/dbname`
- Check credentials in cPanel PostgreSQL® Databases
- Ensure @ and : in password are not special URL characters (new parser handles them)

### Static Files Not Loading
- Verify `.htaccess` is in application root
- Check `mod_rewrite` is enabled in cPanel
- Clear browser cache

### Images Not Uploading  
- Ensure `static/uploads/` exists: `mkdir -p static/uploads`
- Set permissions: `chmod 755 static/uploads`

## Common Commands (cPanel Terminal)

```bash
# Install dependencies
pip install -r requirements.txt

# Create upload directory
mkdir -p static/uploads
chmod 755 static/uploads

# Run migrations (if needed)
python migrate_add_color.py

# Restart app
touch tmp/restart.txt
```

## Migration from Fly.io + Supabase

To migrate your database:
```bash
# Export from Supabase
pg_dump $OLD_DATABASE_URL > dump.sql

# Import to cPanel PostgreSQL (via cPanel Terminal)
psql $NEW_DATABASE_URL < dump.sql
```

Or use cPanel backup/restore tools if available.
