═══════════════════════════════════════════════════════════════════════════
                    SERVER MASTER CONTROL SCRIPT
           ALL-IN-ONE Server Setup, Diagnostics & Management
═══════════════════════════════════════════════════════════════════════════

🎯 ONE SCRIPT TO RULE THEM ALL

This is a complete, unified script that combines:
✓ Initial server setup (LAMP + SSH + SSL + Firewall)
✓ Complete diagnostics (checks EVERYTHING)
✓ SSH password authentication fix
✓ Web server access fix
✓ Database management
✓ Virtual host management
✓ SSL certificate installation
✓ System backups
✓ Service management
✓ And much more!

═══════════════════════════════════════════════════════════════════════════
QUICK START
═══════════════════════════════════════════════════════════════════════════

1. UPLOAD TO YOUR SERVER:
   scp -i your-key.pem server-master.sh ubuntu@your-server-ip:~

2. CONNECT TO SERVER:
   ssh -i your-key.pem ubuntu@your-server-ip

3. RUN THE SCRIPT:
   chmod +x server-master.sh
   sudo ./server-master.sh

4. USE THE MENU:
   Select options 1-18 or 0 to exit

═══════════════════════════════════════════════════════════════════════════
MENU OPTIONS
═══════════════════════════════════════════════════════════════════════════

🟢 INITIAL SETUP
────────────────────────────────────────────────────────────────────────
  1. 🚀 Complete Server Setup (LAMP + SSH + SSL)
     - Full automated installation
     - Configures everything from scratch
     - Creates test pages
     - Sets up firewall
     - Installs SSL if DNS is ready

🟡 DIAGNOSTICS & TROUBLESHOOTING
────────────────────────────────────────────────────────────────────────
  2. 🔍 Run Complete Diagnostics
     - Checks ALL server components
     - Shows pass/fail/warning for each
     - Provides detailed status

  3. 🔧 Fix SSH Password Authentication
     - Fixes "No supported authentication methods" error
     - Properly configures SSH
     - Sets ubuntu user password
     - Tests configuration

  4. 🌐 Fix Web Server Access
     - Installs/starts Apache
     - Configures firewall
     - Creates test pages
     - Tests local and external access

🔵 DATABASE & WEBSITE MANAGEMENT
────────────────────────────────────────────────────────────────────────
  5. 📊 Create MySQL Database and User
     - Interactive database creation
     - Saves credentials to file
     - Sets proper permissions

  6. 🌍 Add New Virtual Host (Domain)
     - Creates Apache virtual host
     - Sets up document root
     - Configures basic site

  7. 🔒 Install SSL Certificate
     - Uses Let's Encrypt
     - Configures auto-renewal
     - Supports www subdomain

  8. 📝 View Databases
     - Lists all MySQL databases
     - Shows created database credentials

🟣 SYSTEM MAINTENANCE
────────────────────────────────────────────────────────────────────────
  9. 💾 Backup Website and Database
     - Creates timestamped backups
     - Backs up /var/www
     - Exports all MySQL databases

  10. 🔄 Update System Packages
      - Updates package lists
      - Upgrades all packages
      - Cleans up old packages

  11. ⚙️ Fix File Permissions
      - Sets correct ownership (www-data)
      - Sets directory permissions (755)
      - Sets file permissions (644)

  12. 📋 View Server Summary
      - Shows setup summary
      - Displays credentials
      - Lists service status

  13. 🔐 Change SSH Password
      - Changes password for ubuntu user
      - Interactive password entry

  14. 📦 Install Additional PHP Extensions
      - Common extensions menu
      - Custom extension option
      - Restarts Apache after install

🔷 SERVICE MANAGEMENT
────────────────────────────────────────────────────────────────────────
  15. 🔄 Restart Services
      - SSH
      - Apache
      - MySQL
      - All services at once

  16. 📊 Check Service Status
      - Shows detailed status
      - Displays if running/stopped
      - Shows recent log entries

  17. 📖 View Logs
      - Apache error log
      - Apache access log
      - SSH authentication log
      - MySQL error log

  18. 🧪 Test Website Configuration
      - Tests DNS resolution
      - Tests HTTP response
      - Tests HTTPS response
      - Shows SSL certificate info

  0. ❌ Exit
     - Saves log file
     - Clean exit

═══════════════════════════════════════════════════════════════════════════
USAGE EXAMPLES
═══════════════════════════════════════════════════════════════════════════

🎯 SCENARIO 1: Fresh Server Setup
1. Run script: sudo ./server-master.sh
2. Choose option 1 (Complete Server Setup)
3. Enter your domain and email
4. Wait for completion
5. Test in browser

🎯 SCENARIO 2: Fix SSH Password Issue
1. Upload script with key-based auth
2. Run: sudo ./server-master.sh
3. Choose option 3 (Fix SSH Password Auth)
4. Follow prompts
5. Test in new terminal

🎯 SCENARIO 3: Website Not Working
1. Run: sudo ./server-master.sh
2. Choose option 4 (Fix Web Server Access)
3. Script automatically fixes issues
4. Check AWS Security Group if still not working

🎯 SCENARIO 4: Create Database for WordPress
1. Run: sudo ./server-master.sh
2. Choose option 5 (Create Database)
3. Enter: wordpress_db, wordpress_user, strong_password
4. Use these credentials in wp-config.php

🎯 SCENARIO 5: Add New Website
1. Run: sudo ./server-master.sh
2. Choose option 6 (Add Virtual Host)
3. Enter new domain name
4. Upload files to the document root shown
5. Choose option 7 to add SSL

🎯 SCENARIO 6: Something's Wrong, Not Sure What
1. Run: sudo ./server-master.sh
2. Choose option 2 (Run Complete Diagnostics)
3. Review results (Pass/Fail/Warn)
4. Use appropriate fix option from menu

═══════════════════════════════════════════════════════════════════════════
COLOR CODING
═══════════════════════════════════════════════════════════════════════════

The script uses colors to make output clear:

🟢 GREEN   = Success / Pass
🔴 RED     = Error / Fail
🟡 YELLOW  = Warning
🔵 BLUE    = Information
🟣 MAGENTA = Debug / Section headers
🔷 CYAN    = Headers / Titles

═══════════════════════════════════════════════════════════════════════════
FEATURES
═══════════════════════════════════════════════════════════════════════════

✓ Interactive menu system - easy to navigate
✓ Color-coded output - easy to read
✓ Automatic logging - every action logged
✓ Error checking - validates everything
✓ Safe operations - backs up configs
✓ Comprehensive - covers all common tasks
✓ One script - no multiple files to manage
✓ Can run repeatedly - safe to use multiple times

═══════════════════════════════════════════════════════════════════════════
LOGS & FILES CREATED
═══════════════════════════════════════════════════════════════════════════

The script creates/uses these files:

/root/server-master-log-TIMESTAMP.log     - Session log
/root/server_setup_summary.txt            - Setup summary
/root/mysql_root_password.txt             - MySQL password
/root/databases.txt                       - Database credentials
/root/backups/                            - Backup directory
/etc/ssh/sshd_config.backup.*             - SSH config backups

═══════════════════════════════════════════════════════════════════════════
ADVANTAGES OVER SEPARATE SCRIPTS
═══════════════════════════════════════════════════════════════════════════

✓ ONE FILE to upload and manage
✓ UNIFIED MENU for all operations
✓ CONSISTENT OUTPUT formatting
✓ SHARED LOGGING across all operations
✓ NO CONFUSION about which script to use
✓ EASIER TO UPDATE - just replace one file
✓ ORGANIZED by category in menu

═══════════════════════════════════════════════════════════════════════════
REQUIREMENTS
═══════════════════════════════════════════════════════════════════════════

- Ubuntu 20.04, 22.04, or 24.04
- Root/sudo access
- Internet connection
- At least 1GB RAM (2GB recommended)
- 10GB disk space minimum

═══════════════════════════════════════════════════════════════════════════
SAFETY FEATURES
═══════════════════════════════════════════════════════════════════════════

✓ Always backs up SSH config before changes
✓ Tests configuration before applying
✓ Validates syntax before restarting services
✓ Saves credentials to secure files (600 permissions)
✓ Logs every action for troubleshooting
✓ Confirms destructive operations
✓ Can be run multiple times safely

═══════════════════════════════════════════════════════════════════════════
TROUBLESHOOTING
═══════════════════════════════════════════════════════════════════════════

Q: Script says "must be run as root"
A: Use: sudo ./server-master.sh

Q: Menu doesn't show properly
A: Your terminal may not support colors - still works fine

Q: Option fails
A: Check the log file shown at exit for details

Q: Can I run this on existing server?
A: Yes! Skip option 1 if already set up. Use other options as needed.

Q: Will this break my existing setup?
A: No - it backs up configs and tests before applying changes

Q: How do I see what went wrong?
A: Check the log file path shown when you exit the script

═══════════════════════════════════════════════════════════════════════════
PRO TIPS
═══════════════════════════════════════════════════════════════════════════

💡 Run diagnostics (option 2) FIRST to see what needs fixing
💡 Always test SSH in a NEW terminal before closing current one
💡 Check AWS Security Groups if website not accessible externally
💡 Use option 12 to quickly view all credentials and summary
💡 Run option 10 (update system) weekly for security
💡 Use option 9 (backup) before making major changes
💡 Keep the log files - they're useful for troubleshooting

═══════════════════════════════════════════════════════════════════════════
QUICK COMMAND REFERENCE
═══════════════════════════════════════════════════════════════════════════

# Upload script
scp -i key.pem server-master.sh ubuntu@server-ip:~

# Run script
chmod +x server-master.sh
sudo ./server-master.sh

# After exit, view log
sudo cat /root/server-master-log-*.log

# View credentials
sudo cat /root/server_setup_summary.txt
sudo cat /root/mysql_root_password.txt
sudo cat /root/databases.txt

═══════════════════════════════════════════════════════════════════════════

🎉 YOU'RE ALL SET! Just run the script and select what you need!

═══════════════════════════════════════════════════════════════════════════
