setup_testenv.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/bash
  2. #
  3. # setup_testenv.sh — Local test environment setup for Feuerwehr Getränkeautomat Status
  4. # Usage: sudo bash setup_testenv.sh
  5. # Prerequisite: Project must already be located under /var/www
  6. #
  7. set -euo pipefail
  8. # ── Determine project root (directory containing this script) ──────────────
  9. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  10. PROJECT_ROOT="$SCRIPT_DIR"
  11. # ── 1. Install Apache, PHP, and required modules ────────────────────────────
  12. echo "→ Installing Apache (httpd), PHP, and required modules..."
  13. sudo dnf install -y httpd php php-json php-mbstring php-xml jq
  14. echo "✓ Packages installed"
  15. # ── 2. Verify project is under /var/www ───────────────────────────────────
  16. if [[ "$PROJECT_ROOT" != /var/www* ]]; then
  17. echo "ERROR: Project must be located under /var/www"
  18. echo " Current location: $PROJECT_ROOT"
  19. echo " Please move the project to /var/www (e.g. /var/www/feuerwehr-getraenkeautomat-status)"
  20. echo " and run this script again from that location."
  21. exit 1
  22. fi
  23. echo "✓ Project location verified: $PROJECT_ROOT"
  24. # ── 3. Update data/config.json to set base_path to /automat/ ───────────────
  25. CONFIG_FILE="$PROJECT_ROOT/data/config.json"
  26. if [[ ! -f "$CONFIG_FILE" ]]; then
  27. echo "ERROR: Config file not found: $CONFIG_FILE"
  28. exit 1
  29. fi
  30. echo "→ Updating config: app.base_path → /automat/"
  31. jq '.app.base_path = "/automat/"' "$CONFIG_FILE" > "${CONFIG_FILE}.tmp"
  32. mv "${CONFIG_FILE}.tmp" "$CONFIG_FILE"
  33. echo "✓ Config updated"
  34. # ── 4. Set ownership and permissions for Apache + current user ─────────────
  35. CURRENT_USER=$(whoami)
  36. APACHE_USER="apache"
  37. APACHE_GROUP="apache"
  38. echo "→ Setting permissions for user '$CURRENT_USER' and Apache user '$APACHE_USER'..."
  39. # Ensure ACL support is available
  40. if ! command -v setfacl &>/dev/null; then
  41. echo " Installing ACL tools..."
  42. dnf install -y acl
  43. fi
  44. # Set ACLs so both the current user and Apache can read/write
  45. setfacl -R -m u:"$CURRENT_USER":rwx,u:"$APACHE_USER":rwx "$PROJECT_ROOT"
  46. setfacl -R -d -m u:"$CURRENT_USER":rwx,u:"$APACHE_USER":rwx "$PROJECT_ROOT"
  47. # Also ensure the data directory itself is writable
  48. chmod 0775 "$PROJECT_ROOT/data"
  49. chmod 0664 "$PROJECT_ROOT/data/"*.json 2>/dev/null || true
  50. echo "✓ Permissions configured (ACLs set for $CURRENT_USER and $APACHE_USER)"
  51. # ── 5. Create Apache alias config for /automat ─────────────────────────────
  52. APACHE_CONF="/etc/httpd/conf.d/automat.conf"
  53. echo "→ Creating Apache configuration: $APACHE_CONF"
  54. sudo cat > "$APACHE_CONF" << EOF
  55. Alias /automat "$PROJECT_ROOT"
  56. <Directory "$PROJECT_ROOT">
  57. Options Indexes FollowSymLinks
  58. AllowOverride All
  59. Require all granted
  60. </Directory>
  61. EOF
  62. echo "✓ Apache configuration written"
  63. # ── 6. Enable and start httpd ─────────────────────────────────────────────
  64. echo "→ Enabling and starting httpd..."
  65. systemctl enable httpd
  66. systemctl restart httpd
  67. echo "✓ Apache (httpd) is running"
  68. # ── 7. Configure SELinux permissions ─────────────────────────────────────
  69. echo "→ Configuring SELinux permissions..."
  70. # Check if SELinux is enabled
  71. if command -v getenforce &>/dev/null; then
  72. SELINUX_STATUS=$(getenforce 2>/dev/null || echo "Disabled")
  73. if [[ "$SELINUX_STATUS" != "Disabled" && "$SELINUX_STATUS" != "Permissive" ]]; then
  74. echo " SELinux is enabled ($SELINUX_STATUS), setting contexts..."
  75. # Install SELinux tools if not present
  76. if ! command -v semanage &>/dev/null; then
  77. echo " Installing SELinux management tools..."
  78. sudo dnf install -y policycoreutils-python-utils
  79. fi
  80. # Set SELinux context for web content (readable by httpd)
  81. echo " → Setting httpd_sys_content_t context for project files..."
  82. sudo semanage fcontext -a -t httpd_sys_content_t "$PROJECT_ROOT(/.*)?" 2>/dev/null || \
  83. sudo semanage fcontext -m -t httpd_sys_content_t "$PROJECT_ROOT(/.*)?" 2>/dev/null || true
  84. # Set SELinux context for data directory (writable by httpd/PHP)
  85. echo " → Setting httpd_sys_rw_content_t context for data directory..."
  86. sudo semanage fcontext -a -t httpd_sys_rw_content_t "$PROJECT_ROOT/data(/.*)?" 2>/dev/null || \
  87. sudo semanage fcontext -m -t httpd_sys_rw_content_t "$PROJECT_ROOT/data(/.*)?" 2>/dev/null || true
  88. # Apply the contexts
  89. sudo restorecon -Rv "$PROJECT_ROOT" 2>/dev/null || true
  90. # Allow httpd to read/write to the data directory via PHP
  91. echo " → Setting SELinux boolean: httpd_can_network_connect..."
  92. sudo setsebool -P httpd_can_network_connect on 2>/dev/null || true
  93. # If using PHP-FPM, allow httpd to connect to FPM socket
  94. if systemctl is-active php-fpm &>/dev/null; then
  95. echo " → Allowing httpd to connect to PHP-FPM..."
  96. sudo setsebool -P httpd_can_network_relay on 2>/dev/null || true
  97. fi
  98. echo "✓ SELinux contexts configured"
  99. else
  100. echo " SELinux is $SELINUX_STATUS, skipping SELinux configuration"
  101. fi
  102. else
  103. echo " (SELinux tools not found, skipping SELinux configuration)"
  104. fi
  105. # ── 8. Open firewall for HTTP ─────────────────────────────────────────────
  106. echo "→ Configuring firewall for HTTP..."
  107. if command -v firewall-cmd &>/dev/null; then
  108. firewall-cmd --permanent --add-service=http 2>/dev/null || true
  109. firewall-cmd --reload 2>/dev/null || true
  110. echo "✓ Firewall updated (HTTP service added)"
  111. else
  112. echo " (firewall-cmd not found, skipping firewall configuration)"
  113. fi
  114. # ── Done ───────────────────────────────────────────────────────────────────
  115. echo ""
  116. echo "========================================"
  117. echo " Setup complete!"
  118. echo "========================================"
  119. echo ""
  120. echo " Project root : $PROJECT_ROOT"
  121. echo " Served at : http://localhost/automat/"
  122. echo ""
  123. echo " Next steps:"
  124. echo " 1. Open http://localhost/automat/ in your browser"
  125. echo " 2. Check httpd status: sudo systemctl status httpd"
  126. echo " 3. Check PHP errors: $PROJECT_ROOT/data/php_errors.log"
  127. echo " 4. Check SELinux denials: sudo ausearch -m AVC -ts recent"
  128. echo ""