# cPanel Backend Deployment Guide for dr.e-soft.az

## Prerequisites
- cPanel hosting with SSH access
- MySQL/MariaDB database
- Python 3.8+ support

## Step 1: Database Setup

1. **Create MySQL Database:**
   - Login to cPanel
   - Go to "MySQL Databases"
   - Create new database: `dr_e_soft_az`
   - Create new database user with strong password
   - Add user to database with all privileges

2. **Import Database Schema:**
   ```bash
   # Via SSH
   mysql -u username -p dr_e_soft_az < backend/database_schema.sql
   ```

## Step 2: Upload Backend Files

1. **Upload via File Manager or FTP:**
   - Create folder: `~/backend` outside public_html
   - Upload all files from `backend/` folder
   - Keep `uploads/` folder for file uploads

## Step 3: Python Environment Setup

```bash
# SSH into your server
ssh username@your-server.com

# Navigate to backend directory
cd ~/backend

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt
```

## Step 4: Environment Configuration

Create `.env` file in `~/backend`:

```bash
# Database
DATABASE_URL=mysql+pymysql://db_user:db_password@localhost/dr_e_soft_az

# JWT Secret
JWT_SECRET_KEY=your-super-secret-random-key-change-this

# Admin Credentials
ADMIN_USERNAME=admin
ADMIN_PASSWORD=your-secure-password

# Server
HOST=0.0.0.0
PORT=8000
```

## Step 5: Initialize Database

```bash
cd ~/backend
source venv/bin/activate

# Run database initialization
python -c "from database import engine, Base; from models import *; Base.metadata.create_all(bind=engine)"

# Seed initial data
python seed_data.py
```

## Step 6: Run Backend with Gunicorn

```bash
# Install gunicorn
pip install gunicorn

# Test run
gunicorn backend.server:app --host 0.0.0.0 --port 8000

# Production run (background)
nohup gunicorn backend.server:app --host 0.0.0.0 --port 8000 --workers 4 > backend.log 2>&1 &
```

## Step 7: Setup Auto-restart with Supervisor

```bash
# Install supervisor (if not installed)
pip install supervisor

# Create supervisor config
echo "[program:dr-backend]
command=/home/username/backend/venv/bin/gunicorn backend.server:app --host 0.0.0.0 --port 8000
directory=/home/username/backend
user=username
autostart=true
autorestart=true
stderr_logfile=/home/username/backend/error.log
stdout_logfile=/home/username/backend/backend.log" > /etc/supervisor/conf.d/dr-backend.conf

# Start supervisor
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl reload
supervisorctl start dr-backend
```

## Step 8: Setup Reverse Proxy (Optional)

If you want backend on specific subdomain:

1. **Create subdomain:** `api.dr.e-soft.az`
2. **Configure Apache/Nginx proxy** to forward requests to port 8000

## Step 9: API URL Configuration

Update frontend API URL to: `https://api.dr.e-soft.az`

Or if backend on same domain: `https://dr.e-soft.az/api`

## Troubleshooting

**Check backend status:**
```bash
ps aux | grep gunicorn
tail -f ~/backend/backend.log
```

**Restart backend:**
```bash
pkill -f gunicorn
cd ~/backend
source venv/bin/activate
nohup gunicorn backend.server:app --host 0.0.0.0 --port 8000 --workers 4 > backend.log 2>&1 &
```

**Database connection issues:**
- Verify DATABASE_URL format
- Check MySQL user permissions
- Ensure MySQL service is running

## Security Notes

- Change default admin password immediately
- Use strong JWT_SECRET_KEY
- Keep backend folder outside public_html
- Regularly update dependencies
- Enable SSL/HTTPS
