# Production Server Deployment Guide

## ⚠️ CRITICAL: Production vs Local Environment

**This guide is for PRODUCTION SERVER deployment ONLY**

- **Production Server Path**: `/var/www/html/proviska-ai`
- **Local Development Path**: `/var/www/html/proviska/proviska-ai`
- **Environment**: PHP 8.3 + Apache on Ubuntu/Debian production server

## 🚨 Apache Restart Failure Recovery

The performance optimization deployment encountered an Apache restart failure. Here's how to diagnose and fix it on the **PRODUCTION SERVER**:

### Step 1: Diagnose the Issue (Run on Production)

```bash
# Connect to your production server first
ssh your-server

# Navigate to the correct production path
cd /var/www/html/proviska-ai

# Run comprehensive Apache diagnostic
sudo bash scripts/diagnose-apache-failure.sh
```

### Step 2: Fix Common Issues (Production Server)

**If Apache configuration has syntax errors:**
```bash
# Comment out performance config temporarily
sudo bash scripts/diagnose-apache-failure.sh --fix-comment

# Test Apache config
apache2ctl configtest

# If clean, restart Apache
sudo systemctl restart apache2
```

**If performance config file is missing:**
```bash
# Verify the performance config file exists
ls -la /var/www/html/proviska-ai/config/apache-performance.conf

# If missing, check if deployment completed
ls -la /var/www/html/proviska-ai/config/
```

### Step 3: Safe Re-deployment (Production Server)

Use the improved deployment script with rollback capability:

```bash
# Pre-validate Apache configuration
sudo bash scripts/validate-apache-config.sh

# If validation passes, deploy safely
sudo bash scripts/apply-performance-optimizations-v3.sh
```

## 🔧 Production Deployment Scripts

### 1. `diagnose-apache-failure.sh`
- **Purpose**: Diagnose Apache restart failures
- **Usage**: `sudo bash scripts/diagnose-apache-failure.sh`
- **Features**: Configuration testing, error log analysis, automatic fixes

### 2. `validate-apache-config.sh`
- **Purpose**: Pre-validate Apache configuration before deployment
- **Usage**: `sudo bash scripts/validate-apache-config.sh`
- **Features**: Syntax checking, module validation, integration testing

### 3. `apply-performance-optimizations-v3.sh`
- **Purpose**: Safe deployment with rollback capability
- **Usage**: `sudo bash scripts/apply-performance-optimizations-v3.sh`
- **Features**: Backup creation, configuration testing, graceful failure handling

## 🎯 Expected Results After Successful Deployment

### Performance Targets
- **Current**: 169.4 req/sec (Grade C)
- **Target**: 300-500+ req/sec (Grade A/A+)
- **Improvement**: 77-195% increase

### Validation Commands (Production Server)
```bash
# Test benchmark endpoint
curl http://your-domain/proviska-ai/api/benchmark-endpoint.php

# Check Apache status
systemctl status apache2

# Monitor performance
# Access: http://your-domain/proviska-ai/api/admin-dashboard-v2.html
```

## 🛠️ Troubleshooting Guide

### Issue: Apache Won't Start
**Symptoms**: `systemctl restart apache2` fails
**Solution**: 
1. Run `apache2ctl configtest` to check syntax
2. Use `scripts/diagnose-apache-failure.sh` for detailed analysis
3. Check `/var/log/apache2/error.log` for specific errors

### Issue: Performance Config Not Loading
**Symptoms**: Performance improvements not visible
**Solution**:
1. Verify config inclusion: `grep proviska-performance /etc/apache2/apache2.conf`
2. Check file exists: `ls -la /var/www/html/proviska-ai/config/apache-performance.conf`
3. Test config syntax: `apache2ctl configtest`

### Issue: Permission Errors
**Symptoms**: 403 errors or file access issues
**Solution**:
```bash
sudo chown -R www-data:www-data /var/www/html/proviska-ai
sudo chmod -R 755 /var/www/html/proviska-ai
sudo chmod 644 /var/www/html/proviska-ai/api/.htaccess
```

## 📋 Production Deployment Checklist

**Pre-Deployment:**
- [ ] Connected to production server
- [ ] Working directory: `/var/www/html/proviska-ai`
- [ ] Backup created: `scripts/apply-performance-optimizations-v3.sh` creates automatic backups
- [ ] Apache config validated: `sudo bash scripts/validate-apache-config.sh`

**Deployment:**
- [ ] Run safe deployment: `sudo bash scripts/apply-performance-optimizations-v3.sh`
- [ ] Apache restarts successfully
- [ ] Performance endpoint responds: `curl http://domain/proviska-ai/api/benchmark-endpoint.php`

**Post-Deployment:**
- [ ] Load test shows >300 req/sec (Grade A)
- [ ] Admin dashboard performance monitor working
- [ ] Error logs clean: `tail -f /var/log/apache2/error.log`

## 🚀 Quick Recovery Commands

**If deployment fails:**
```bash
# Diagnose the issue
sudo bash scripts/diagnose-apache-failure.sh

# Comment out performance config
sudo bash scripts/diagnose-apache-failure.sh --fix-comment

# Restart Apache
sudo systemctl restart apache2
```

**If Apache is broken:**
```bash
# Emergency restoration from backup
sudo cp backups/perf_YYYYMMDD_HHMMSS/apache2.conf /etc/apache2/apache2.conf
sudo systemctl restart apache2
```

## 📞 Support Information

**Key Files for Support:**
- Apache config: `/etc/apache2/apache2.conf`
- Performance config: `/var/www/html/proviska-ai/config/apache-performance.conf`
- Error logs: `/var/log/apache2/error.log`
- Deployment logs: Output from deployment scripts

**Diagnostic Commands:**
```bash
# Apache status
systemctl status apache2

# Configuration test
apache2ctl configtest

# Virtual host summary
apache2ctl -S

# Error logs
tail -n 50 /var/log/apache2/error.log
```

---

**Remember**: All commands in this guide must be run on the **PRODUCTION SERVER** at path `/var/www/html/proviska-ai`, not in the local development environment.