#!/bin/bash # ============================================================================== # Automated Server Setup Script for TPU Survey (survey.tapee.ac.th) # Compatible with Ubuntu 20.04 / 22.04 / 24.04 and Debian 11 / 12 # ============================================================================== set -e # Configuration Variables DOMAIN="survey.tapee.ac.th" WEB_ROOT="/var/www/survey" DB_NAME="tpu_survey_v2" DB_USER="survey_user" DB_PASS="SurveyTPU2026!Secure" # Change this if needed GIT_REPO="https://git.tapee.ac.th/pongpon/survey.git" echo "==========================================================" echo " Starting Setup for ${DOMAIN} on VM 110" echo "==========================================================" # 1. Update system packages echo "[1/6] Updating system packages..." export DEBIAN_FRONTEND=noninteractive apt-get update -y && apt-get upgrade -y # 2. Install Web Server, PHP, and MariaDB echo "[2/6] Installing Apache, PHP, MariaDB, and utilities..." apt-get install -y \ apache2 \ mariadb-server \ mariadb-client \ git \ curl \ unzip \ certbot \ python3-certbot-apache \ php \ php-cli \ php-fpm \ php-mysql \ php-mbstring \ php-xml \ php-curl \ php-gd \ php-zip \ php-intl \ libapache2-mod-php # Enable Apache Modules a2enmod rewrite ssl headers # 3. Setup MariaDB Database echo "[3/6] Configuring Database (${DB_NAME})..." systemctl start mariadb systemctl enable mariadb mysql -u root < "${WEB_ROOT}/config/database.php" PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false ]); } catch (PDOException \$e) { http_response_code(500); echo json_encode(['error' => 'Database connection failed: ' . \$e->getMessage()]); exit; } } return \$pdo; } function jsonResponse(\$data, \$code = 200) { http_response_code(\$code); header('Content-Type: application/json; charset=utf-8'); echo json_encode(\$data, JSON_UNESCAPED_UNICODE); exit; } function getInput() { \$json = file_get_contents('php://input'); return json_decode(\$json, true) ?: []; } EOF # Set proper permissions mkdir -p "${WEB_ROOT}/uploads" "${WEB_ROOT}/temp_certificates" chown -R www-data:www-data "${WEB_ROOT}" chmod -R 755 "${WEB_ROOT}" chmod -R 775 "${WEB_ROOT}/uploads" "${WEB_ROOT}/temp_certificates" # 5. Configure Apache VirtualHost echo "[5/6] Creating Apache VirtualHost for ${DOMAIN}..." cat < /etc/apache2/sites-available/${DOMAIN}.conf ServerName ${DOMAIN} ServerAlias www.${DOMAIN} DocumentRoot ${WEB_ROOT} Options -Indexes +FollowSymLinks AllowOverride All Require all granted ErrorLog \${APACHE_LOG_DIR}/${DOMAIN}_error.log CustomLog \${APACHE_LOG_DIR}/${DOMAIN}_access.log combined EOF a2dissite 000-default.conf || true a2ensite ${DOMAIN}.conf systemctl restart apache2 # 6. Summary & SSL Instructions echo "==========================================================" echo " SETUP COMPLETED SUCCESSFULLY!" echo "==========================================================" echo "1. Web Root: ${WEB_ROOT}" echo "2. Database Name: ${DB_NAME}" echo "3. Database User: ${DB_USER}" echo "4. VirtualHost: http://${DOMAIN}" echo "" echo "To enable HTTPS (SSL Certificate via Let's Encrypt), run:" echo " certbot --apache -d ${DOMAIN}" echo "=========================================================="