# 🚀 cPanel Deployment Guide for Yatra Booking System

## Prerequisites
- cPanel hosting account with Python 3.8+ support
- MySQL database access
- SSH/Terminal access to cPanel

## Step 1: Check cPanel Environment

First, connect to your cPanel terminal and check:

```bash
# Check Python version
python3 --version
python --version

# Check pip availability
pip3 --version
which pip3

# Check if you can install packages
pip3 install --user requests
```

## Step 2: Upload Project Files

1. **Upload via File Manager or FTP:**
   - Upload all files from `server/` directory to your domain folder
   - Typical path: `/public_html/` or `/public_html/yourdomain.com/`

2. **Set proper permissions:**
```bash
chmod 755 manage.py
chmod 755 setup_and_run.py
find . -name "*.py" -exec chmod 644 {} \;
```

## Step 3: Install Dependencies

```bash
# Navigate to your project directory
cd /home/yourusername/public_html/

# Install Python packages (user mode for cPanel)
pip3 install --user -r requirements.txt

# If pip3 doesn't work, try:
python3 -m pip install --user -r requirements.txt
```

## Step 4: Database Setup

1. **Create MySQL database in cPanel:**
   - Go to cPanel → MySQL Databases
   - Create database: `yatra_booking`
   - Create user and assign to database
   - Note down: database name, username, password, host

2. **Import existing database (if you have one):**
```bash
mysql -h localhost -u your_db_user -p your_db_name < yatra_booking.sql
```

## Step 5: Configure Django Settings

Update `yatra_testing/settings.py` with your cPanel details:

```python
# Database configuration for cPanel
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_cpanel_username_yatra',  # Usually prefixed with username
        'USER': 'your_cpanel_username_dbuser',
        'PASSWORD': 'your_database_password',
        'HOST': 'localhost',  # Or provided by cPanel
        'PORT': '3306',
    }
}

# Disable Redis for cPanel (no Docker support)
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    }
}

# Static files for cPanel
STATIC_URL = '/static/'
STATIC_ROOT = '/home/yourusername/public_html/static/'

# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/yourusername/public_html/media/'

# Allowed hosts - add your domain
ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com', 'localhost']

# Debug mode (set to False for production)
DEBUG = False
```

## Step 6: Run Django Setup

```bash
# Make migrations
python3 manage.py makemigrations
python3 manage.py makemigrations testing_app

# Apply migrations
python3 manage.py migrate --run-syncdb

# Collect static files
python3 manage.py collectstatic --noinput

# Create superuser
python3 manage.py createsuperuser
```

## Step 7: Configure Web Server

### Option A: Using .htaccess (Apache)
Create `.htaccess` in your domain root:

```apache
RewriteEngine On
RewriteBase /

# Handle Django static files
RewriteRule ^static/(.*)$ /static/$1 [L]
RewriteRule ^media/(.*)$ /media/$1 [L]

# Route all other requests to Django
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /passenger_wsgi.py/$1 [QSA,L]
```

### Option B: Using cPanel Python App (Recommended)
1. Go to cPanel → Setup Python App
2. Create new app:
   - Python version: 3.8+
   - Application root: `/public_html/`
   - Application URL: your domain
   - Application startup file: `passenger_wsgi.py`

## Step 8: Create WSGI Configuration

Create `passenger_wsgi.py` in your project root:

```python
#!/usr/bin/python3
import sys
import os

# Add your project directory to Python path
sys.path.insert(0, os.path.dirname(__file__))

# Set Django settings module
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yatra_testing.settings')

# Import Django WSGI application
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
```

## Step 9: Handle Redis Alternative

Since cPanel doesn't support Docker/Redis:

### Option A: Use Redis Cloud Service
1. Sign up for Redis Labs or AWS ElastiCache
2. Update settings:
```python
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("your-redis-cloud-url", 6379)],
        },
    },
}
```

### Option B: Disable Real-time Features (Temporary)
```python
# Use in-memory channels (no persistence, single process only)
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    }
}
```

## Step 10: Test Deployment

1. **Check if site loads:**
   - Visit your domain
   - Should see Django application

2. **Test admin panel:**
   - Visit: `yourdomain.com/admin/`
   - Login with superuser credentials

3. **Check static files:**
   - CSS/JS should load properly
   - Images should display

## Step 11: SSL Certificate (Recommended)

1. In cPanel → SSL/TLS
2. Enable "Force HTTPS Redirect"
3. Update Django settings:
```python
# HTTPS settings
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
```

## Common Issues & Solutions

### Issue: ModuleNotFoundError
```bash
# Install missing packages
pip3 install --user package_name
```

### Issue: Permission Denied
```bash
# Fix permissions
chmod 755 passenger_wsgi.py
chmod -R 755 static/
```

### Issue: Database Connection Error
- Check database credentials in settings.py
- Verify database exists and user has permissions
- Check if MySQL service is running

### Issue: Static Files Not Loading
```bash
# Recollect static files
python3 manage.py collectstatic --clear --noinput
```

## Performance Tips for cPanel

1. **Enable caching:**
```python
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'cache_table',
    }
}
```

2. **Optimize static files:**
```bash
python3 manage.py createcachetable
```

3. **Use compression:**
```python
# Add to MIDDLEWARE
'django.middleware.gzip.GZipMiddleware',
```

## Monitoring & Maintenance

1. **Check error logs:**
   - cPanel → Error Logs
   - Check Django logs in your app directory

2. **Database backups:**
   - Use cPanel → MySQL Databases → phpMyAdmin
   - Export database regularly

3. **Update dependencies:**
```bash
pip3 install --user --upgrade -r requirements.txt
```

---

**Your Yatra booking system should now be live on cPanel!** 🎉

Need help? Check cPanel error logs and Django debug output.