# 🚀 Quick Deployment Guide for ctf.munaltech.com

## Step-by-Step Deployment

### 1. Upload Files
Upload all files from the `server/` directory to your cPanel file manager:
- Path: `/public_html/ctf/` (for subdomain deployment)

### 2. Access cPanel Terminal
Log into your cPanel and open the Terminal.

### 3. Navigate to Project Directory
```bash
cd public_html/ctf
```

### 4. Check Environment
```bash
python3 --version
pip3 --version
```

### 5. Run Automated Deployment
```bash
python3 cpanel_deploy.py
```

This script will:
- Install all Python dependencies
- Set up the database
- Collect static files
- Create proper file permissions
- Generate .htaccess file

### 6. Manual Configuration Steps

#### A. Database Setup in cPanel
1. Go to cPanel → MySQL Databases
2. Create database: `yourusername_yatra` 
3. Create database user and assign full permissions
4. Note down the credentials

#### B. Update Settings
Edit `yatra_testing/settings.py` and add these lines at the bottom:

```python
# Import cPanel-specific settings
from .cpanel_settings import *

# Update with your actual cPanel database details
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'yourusername_yatra',        # Replace with actual DB name
        'USER': 'yourusername_dbuser',       # Replace with actual DB user
        'PASSWORD': 'your_actual_password',   # Replace with actual password
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

# Update paths with your actual cPanel username
STATIC_ROOT = '/home/yourusername/public_html/ctf/static/'
MEDIA_ROOT = '/home/yourusername/public_html/ctf/media/'
```

#### C. Create Python App in cPanel
1. Go to cPanel → Setup Python App
2. Click "Create Application"
3. Settings:
   - **Python version**: 3.8 or higher
   - **Application root**: `/public_html/ctf`
   - **Application URL**: `ctf.munaltech.com`
   - **Application startup file**: `passenger_wsgi.py`
   - **Application Entry point**: `application`

#### D. Install Dependencies via cPanel Python App
1. In the Python App interface, click "Run pip install"
2. Enter: `requirements.txt`
3. Click "Install"

### 7. Run Django Setup Commands
```bash
# Create cache table
python3 manage.py createcachetable

# Run migrations
python3 manage.py migrate

# Create superuser
python3 manage.py createsuperuser

# Collect static files
python3 manage.py collectstatic --noinput
```

### 8. Test Your Deployment

Visit these URLs to verify everything works:
- **Main site**: https://ctf.munaltech.com
- **Admin panel**: https://ctf.munaltech.com/admin/
- **API test**: https://ctf.munaltech.com/test/

### 9. Enable SSL (Recommended)
1. Go to cPanel → SSL/TLS
2. Enable "Force HTTPS Redirect"
3. Update `yatra_testing/settings.py`:
```python
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
```

### 10. Final Security Steps
1. Generate a new SECRET_KEY at https://djecrety.ir/
2. Update `SECRET_KEY` in settings.py
3. Set `DEBUG = False`
4. Restart the Python application in cPanel

## Quick Test Commands

After deployment, test these in cPanel terminal:

```bash
# Check if Django is working
python3 manage.py check

# Test database connection
python3 manage.py dbshell

# Check static files
ls -la static/

# View recent logs
tail -f /home/yourusername/logs/django_error.log
```

## Common Issues & Solutions

### Issue: "ModuleNotFoundError"
```bash
pip3 install --user package_name
```

### Issue: "Database connection failed"
- Double-check database credentials in settings.py
- Ensure database user has all privileges

### Issue: "Static files not loading"
```bash
python3 manage.py collectstatic --clear
```

### Issue: "Permission denied"
```bash
chmod 755 passenger_wsgi.py
chmod -R 755 static/
```

## Your URLs After Deployment

- **Main Dashboard**: https://ctf.munaltech.com/
- **Passenger Booking**: https://ctf.munaltech.com/test/
- **Operator Login**: https://ctf.munaltech.com/operator/
- **Admin Panel**: https://ctf.munaltech.com/admin/
- **API Endpoints**: https://ctf.munaltech.com/api/

---

**Need help?** Check the detailed guide in `cpanel_deployment_guide.md` or contact support with error logs from `/home/yourusername/logs/django_error.log`