#!/bin/bash # ============================================================ # PRACTICE EXAM SETUP SCRIPT # Betriebssysteme und Netzwerke - 45 Minute Practice Test # ============================================================ # This script sets up a server environment with multiple services # for students to practice system administration troubleshooting # ============================================================ set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color print_status() { echo -e "${BLUE}[*]${NC} $1" } print_success() { echo -e "${GREEN}[✓]${NC} $1" } print_warning() { echo -e "${YELLOW}[!]${NC} $1" } print_error() { echo -e "${RED}[✗]${NC} $1" } # Check if running as root if [[ $EUID -ne 0 ]]; then print_error "This script must be run as root (use sudo)" exit 1 fi echo "" echo "============================================================" echo " PRACTICE EXAM ENVIRONMENT SETUP" echo " Betriebssysteme und Netzwerke" echo "============================================================" echo "" # ============================================================ # STEP 1: Install required packages # ============================================================ print_status "Updating package lists..." apt-get update -qq print_status "Installing required packages..." apt-get install -y -qq nginx mysql-server php-fpm php-mysql docker.io docker-compose ufw curl > /dev/null 2>&1 print_success "Packages installed" # ============================================================ # STEP 2: Configure Nginx - Website 1 (company.local) # ============================================================ print_status "Setting up company.local website..." # Create website directory mkdir -p /var/www/company cat > /var/www/company/index.html << 'EOF' Company Portal

Company Portal

Status: Online

Welcome to the internal company portal.

EOF # Set correct permissions chown -R www-data:www-data /var/www/company chmod -R 755 /var/www/company # Create Nginx config cat > /etc/nginx/sites-available/company.local << 'EOF' server { listen 80; server_name company.local; root /var/www/company; index index.html; location / { try_files $uri $uri/ =404; } error_log /var/log/nginx/company_error.log; access_log /var/log/nginx/company_access.log; } EOF # Enable site ln -sf /etc/nginx/sites-available/company.local /etc/nginx/sites-enabled/ print_success "company.local configured" # ============================================================ # STEP 3: Configure Nginx - Website 2 (shop.local) # ============================================================ print_status "Setting up shop.local website..." # Create website directory mkdir -p /var/www/shop cat > /var/www/shop/index.html << 'EOF' Online Shop

Online Shop

Status: Online

Browse our products and enjoy shopping!

EOF # Set correct permissions chown -R www-data:www-data /var/www/shop chmod -R 755 /var/www/shop # Create Nginx config cat > /etc/nginx/sites-available/shop.local << 'EOF' server { listen 80; server_name shop.local www.shop.local; root /var/www/shop; index index.html; location / { try_files $uri $uri/ =404; } error_log /var/log/nginx/shop_error.log; access_log /var/log/nginx/shop_access.log; } EOF # Enable site ln -sf /etc/nginx/sites-available/shop.local /etc/nginx/sites-enabled/ print_success "shop.local configured" # ============================================================ # STEP 4: Configure PHP-FPM API (api.local:8080) # ============================================================ print_status "Setting up PHP API on api.local..." # Create API directory mkdir -p /var/www/api cat > /var/www/api/index.php << 'EOF' 'success', 'message' => 'API is running', 'timestamp' => date('Y-m-d H:i:s'), 'server' => 'Practice Exam Server', 'endpoints' => [ '/health' => 'Health check', '/info' => 'Server information' ] ]; echo json_encode($response, JSON_PRETTY_PRINT); ?> EOF cat > /var/www/api/health.php << 'EOF' 'healthy', 'uptime' => shell_exec('uptime -p')]); ?> EOF # Set permissions chown -R www-data:www-data /var/www/api chmod -R 755 /var/www/api # Detect PHP-FPM version PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;") # Create API Nginx config cat > /etc/nginx/sites-available/api.local << EOF server { listen 8080; server_name api.local; root /var/www/api; index index.php; location / { try_files \$uri \$uri/ =404; } location ~ \.php\$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock; } error_log /var/log/nginx/api_error.log; access_log /var/log/nginx/api_access.log; } EOF # Enable site ln -sf /etc/nginx/sites-available/api.local /etc/nginx/sites-enabled/ print_success "api.local configured on port 8080" # ============================================================ # STEP 5: Configure MySQL Database # ============================================================ print_status "Setting up MySQL database..." # Start MySQL if not running systemctl start mysql systemctl enable mysql # Create database and user mysql -e "CREATE DATABASE IF NOT EXISTS practicedb;" mysql -e "CREATE USER IF NOT EXISTS 'webuser'@'localhost' IDENTIFIED BY 'WebPass123!';" mysql -e "GRANT ALL PRIVILEGES ON practicedb.* TO 'webuser'@'localhost';" mysql -e "FLUSH PRIVILEGES;" # Create test table with data mysql practicedb << 'EOF' CREATE TABLE IF NOT EXISTS products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, price DECIMAL(10,2) NOT NULL, stock INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); INSERT IGNORE INTO products (id, name, price, stock) VALUES (1, 'Laptop', 999.99, 50), (2, 'Mouse', 29.99, 200), (3, 'Keyboard', 79.99, 150), (4, 'Monitor', 299.99, 75); EOF print_success "MySQL database 'practicedb' configured" # ============================================================ # STEP 6: Configure Docker Application # ============================================================ print_status "Setting up Docker application..." # Start and enable Docker systemctl start docker systemctl enable docker # Create Docker app directory mkdir -p /opt/docker-app cat > /opt/docker-app/docker-compose.yml << 'EOF' version: '3' services: webapp: image: nginx:alpine container_name: practice-webapp ports: - "8888:80" volumes: - ./html:/usr/share/nginx/html:ro restart: unless-stopped EOF # Create simple HTML for Docker app mkdir -p /opt/docker-app/html cat > /opt/docker-app/html/index.html << 'EOF' Docker App

Docker Application

Running in a container on port 8888

EOF # Start Docker container cd /opt/docker-app docker-compose up -d print_success "Docker application started on port 8888" # ============================================================ # STEP 7: Configure Firewall (UFW) # ============================================================ print_status "Configuring firewall..." # Reset UFW to defaults ufw --force reset > /dev/null 2>&1 # Set default policies ufw default deny incoming > /dev/null 2>&1 ufw default allow outgoing > /dev/null 2>&1 # Allow required ports ufw allow 22/tcp > /dev/null 2>&1 # SSH ufw allow 80/tcp > /dev/null 2>&1 # HTTP ufw allow 8080/tcp > /dev/null 2>&1 # API ufw allow 8888/tcp > /dev/null 2>&1 # Docker app ufw allow 3306/tcp > /dev/null 2>&1 # MySQL (local) # Enable firewall ufw --force enable > /dev/null 2>&1 print_success "Firewall configured" # ============================================================ # STEP 8: Configure /etc/hosts # ============================================================ print_status "Configuring DNS entries in /etc/hosts..." # Remove old entries if they exist sed -i '/company\.local/d' /etc/hosts sed -i '/shop\.local/d' /etc/hosts sed -i '/api\.local/d' /etc/hosts # Add new entries echo "127.0.0.1 company.local" >> /etc/hosts echo "127.0.0.1 shop.local" >> /etc/hosts echo "127.0.0.1 www.shop.local" >> /etc/hosts echo "127.0.0.1 api.local" >> /etc/hosts print_success "/etc/hosts configured" # ============================================================ # STEP 9: Start and enable services # ============================================================ print_status "Starting all services..." # Enable and start PHP-FPM systemctl enable php${PHP_VERSION}-fpm systemctl start php${PHP_VERSION}-fpm # Test and reload Nginx nginx -t > /dev/null 2>&1 systemctl enable nginx systemctl restart nginx print_success "All services started" # ============================================================ # FINAL: Verification # ============================================================ echo "" echo "============================================================" echo " SETUP COMPLETE" echo "============================================================" echo "" print_status "Verifying services..." echo "" # Test each service echo "Testing services:" echo "" # Test company.local if curl -s -o /dev/null -w "%{http_code}" http://company.local 2>/dev/null | grep -q "200"; then print_success "company.local → OK (HTTP 200)" else print_error "company.local → FAILED" fi # Test shop.local if curl -s -o /dev/null -w "%{http_code}" http://shop.local 2>/dev/null | grep -q "200"; then print_success "shop.local → OK (HTTP 200)" else print_error "shop.local → FAILED" fi # Test API if curl -s http://api.local:8080 2>/dev/null | grep -q "success"; then print_success "api.local:8080 → OK (JSON response)" else print_error "api.local:8080 → FAILED" fi # Test Docker app if curl -s -o /dev/null -w "%{http_code}" http://localhost:8888 2>/dev/null | grep -q "200"; then print_success "Docker app :8888 → OK (HTTP 200)" else print_error "Docker app :8888 → FAILED" fi # Test MySQL if mysql -u webuser -p'WebPass123!' practicedb -e "SELECT 1;" > /dev/null 2>&1; then print_success "MySQL database → OK (Connection successful)" else print_error "MySQL database → FAILED" fi echo "" echo "============================================================" echo "Server is ready for the practice exam!" echo "" echo "Available services:" echo " - http://company.local (Company Portal)" echo " - http://shop.local (Online Shop)" echo " - http://api.local:8080 (REST API)" echo " - http://localhost:8888 (Docker App)" echo " - MySQL: webuser@localhost (Database)" echo "" echo "Run 'sudo ./break_server.sh' to start the exam" echo "============================================================"