setup_testenv_fedora.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/bin/bash
  2. #
  3. # setup_testenv.sh — General PHP development environment setup for Fedora
  4. # Usage: bash setup_testenv.sh
  5. # Sets up Apache (httpd) with PHP to serve /var/www/html on localhost
  6. # Requires sudo privileges for package installation and service management
  7. #
  8. # PRIVILEGE SEPARATION: This script must NOT be run as root.
  9. # The running user should not have direct rights to install packages
  10. # (i.e., must use sudo which requires authentication).
  11. #
  12. set -euo pipefail
  13. # ── Colors for output ─────────────────────────────────────────────────────
  14. RED='\033[0;31m'
  15. GREEN='\033[0;32m'
  16. YELLOW='\033[1;33m'
  17. NC='\033[0m' # No Color
  18. # ── Check for privilege separation ─────────────────────────────────────────
  19. echo "→ Checking user privileges..."
  20. CURRENT_USER=$(whoami)
  21. # Check if running as root (violates privilege separation)
  22. if [[ $EUID -eq 0 ]]; then
  23. echo -e "${RED}ERROR: This script should not be run as root directly.${NC}"
  24. echo " Please run as a normal user with sudo privileges."
  25. echo " This ensures proper separation of privileges."
  26. exit 1
  27. fi
  28. echo -e "${GREEN}✓${NC} Not running as root (good for privilege separation)"
  29. # Check if user has sudo privileges (Fedora uses wheel group for sudo access)
  30. if groups | grep -qw wheel; then
  31. echo -e "${GREEN}✓${NC} User '$CURRENT_USER' is in wheel group (has sudo privileges)"
  32. else
  33. echo -e "${YELLOW}WARNING:${NC} User '$CURRENT_USER' is not in the wheel group."
  34. echo " This user may not have sudo privileges required for this script."
  35. echo " The script will attempt to use sudo, which may prompt for a password."
  36. echo ""
  37. read -p " Continue anyway? (y/N) " -n 1 -r
  38. echo
  39. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  40. echo "Aborting."
  41. exit 1
  42. fi
  43. fi
  44. echo ""
  45. echo "Note: sudo commands will prompt for your password if required."
  46. # ── Configuration ─────────────────────────────────────────────────────────
  47. DOCROOT="/var/www/html"
  48. APACHE_USER="apache"
  49. APACHE_GROUP="apache"
  50. APACHE_CONF="/etc/httpd/conf.d/dev.conf"
  51. # ── 1. Install Apache, PHP, and required modules ──────────────────────────
  52. echo ""
  53. echo "→ Installing Apache (httpd), PHP, and required modules..."
  54. sudo dnf install -y httpd php php-json php-mbstring php-xml php-fpm jq acl
  55. echo -e "${GREEN}✓${NC} Packages installed"
  56. # ── 2. Ensure document root exists ────────────────────────────────────────
  57. echo ""
  58. echo "→ Ensuring document root exists: $DOCROOT"
  59. sudo mkdir -p "$DOCROOT"
  60. echo -e "${GREEN}✓${NC} Document root ready"
  61. # ── 3. Set ownership and permissions for Apache + current user ────────────
  62. echo ""
  63. echo "→ Setting permissions for user '$CURRENT_USER' and Apache user '$APACHE_USER'..."
  64. # Ensure ACL support is available (should be installed now)
  65. if ! command -v setfacl &>/dev/null; then
  66. echo " Installing ACL tools..."
  67. sudo dnf install -y acl
  68. fi
  69. # Set ownership to current user (so user can easily create files)
  70. sudo chown -R "$CURRENT_USER:$CURRENT_USER" "$DOCROOT"
  71. # Set base permissions
  72. sudo chmod 0755 "$DOCROOT"
  73. # Set ACLs so both the current user and Apache can read/write
  74. sudo setfacl -R -m u:"$CURRENT_USER":rwx,u:"$APACHE_USER":rwx "$DOCROOT"
  75. sudo setfacl -R -d -m u:"$CURRENT_USER":rwx,u:"$APACHE_USER":rwx "$DOCROOT"
  76. # Also allow the apache group to access
  77. sudo setfacl -R -m g:"$APACHE_GROUP":r-x "$DOCROOT"
  78. sudo setfacl -R -d -m g:"$APACHE_GROUP":r-x "$DOCROOT"
  79. echo -e "${GREEN}✓${NC} Permissions configured (ACLs set for $CURRENT_USER and $APACHE_USER)"
  80. # ── 4. Create/update Apache configuration ─────────────────────────────────
  81. echo ""
  82. echo "→ Creating Apache configuration: $APACHE_CONF"
  83. sudo tee "$APACHE_CONF" > /dev/null << EOF
  84. # Development environment - serve /var/www/html on localhost
  85. <Directory "$DOCROOT">
  86. Options Indexes FollowSymLinks
  87. AllowOverride All
  88. Require all granted
  89. # Enable directory listing if no index file found
  90. DirectoryIndex index.php index.html index.htm
  91. </Directory>
  92. # Ensure PHP files are processed
  93. <FilesMatch \.php$>
  94. SetHandler application/x-httpd-php
  95. </FilesMatch>
  96. EOF
  97. echo -e "${GREEN}✓${NC} Apache configuration written"
  98. # ── 5. Configure PHP for development ──────────────────────────────────────
  99. echo ""
  100. echo "→ Configuring PHP for development..."
  101. PHP_INI="/etc/php.ini"
  102. if [[ -f "$PHP_INI" ]]; then
  103. # Backup original
  104. sudo cp "$PHP_INI" "$PHP_INI.backup.$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true
  105. # Enable error display for development
  106. sudo sed -i 's/^display_errors = .*/display_errors = On/' "$PHP_INI" 2>/dev/null || true
  107. sudo sed -i 's/^display_startup_errors = .*/display_startup_errors = On/' "$PHP_INI" 2>/dev/null || true
  108. sudo sed -i 's/^error_reporting = .*/error_reporting = E_ALL/' "$PHP_INI" 2>/dev/null || true
  109. echo -e "${GREEN}✓${NC} PHP configured for development (errors displayed)"
  110. fi
  111. # ── 6. Enable and start httpd ─────────────────────────────────────────────
  112. echo ""
  113. echo "→ Enabling and starting httpd..."
  114. sudo systemctl enable httpd
  115. sudo systemctl restart httpd
  116. echo -e "${GREEN}✓${NC} Apache (httpd) is running"
  117. # ── 7. Configure SELinux permissions ─────────────────────────────────────
  118. echo ""
  119. echo "→ Configuring SELinux permissions..."
  120. # Check if SELinux is enabled
  121. if command -v getenforce &>/dev/null; then
  122. SELINUX_STATUS=$(getenforce 2>/dev/null || echo "Disabled")
  123. if [[ "$SELINUX_STATUS" != "Disabled" && "$SELINUX_STATUS" != "Permissive" ]]; then
  124. echo " SELinux is enabled ($SELINUX_STATUS), setting contexts..."
  125. # Install SELinux tools if not present
  126. if ! command -v semanage &>/dev/null; then
  127. echo " Installing SELinux management tools..."
  128. sudo dnf install -y policycoreutils-python-utils
  129. fi
  130. # Set SELinux context for web content (readable/writable by httpd)
  131. echo " → Setting httpd_sys_rw_content_t context for $DOCROOT..."
  132. sudo semanage fcontext -a -t httpd_sys_rw_content_t "$DOCROOT(/.*)?" 2>/dev/null || \
  133. sudo semanage fcontext -m -t httpd_sys_rw_content_t "$DOCROOT(/.*)?" 2>/dev/null || true
  134. # Apply the contexts
  135. sudo restorecon -Rv "$DOCROOT" 2>/dev/null || true
  136. # Allow httpd to read/write to the document root via PHP
  137. echo " → Setting SELinux boolean: httpd_can_network_connect..."
  138. sudo setsebool -P httpd_can_network_connect on 2>/dev/null || true
  139. # Allow httpd to send mail (sometimes needed for PHP mail())
  140. sudo setsebool -P httpd_can_sendmail on 2>/dev/null || true
  141. echo -e "${GREEN}✓${NC} SELinux contexts configured"
  142. else
  143. echo " SELinux is $SELINUX_STATUS, skipping SELinux configuration"
  144. fi
  145. else
  146. echo " (SELinux tools not found, skipping SELinux configuration)"
  147. fi
  148. # ── 8. Open firewall for HTTP ─────────────────────────────────────────────
  149. echo ""
  150. echo "→ Configuring firewall for HTTP..."
  151. if command -v firewall-cmd &>/dev/null; then
  152. sudo firewall-cmd --permanent --add-service=http 2>/dev/null || true
  153. sudo firewall-cmd --reload 2>/dev/null || true
  154. echo -e "${GREEN}✓${NC} Firewall updated (HTTP service added)"
  155. else
  156. echo " (firewall-cmd not found, skipping firewall configuration)"
  157. fi
  158. # ── 9. Create a test PHP file ─────────────────────────────────────────────
  159. echo ""
  160. echo "→ Creating test PHP file..."
  161. TEST_FILE="$DOCROOT/info.php"
  162. sudo tee "$TEST_FILE" > /dev/null << 'EOF'
  163. <?php
  164. phpinfo();
  165. ?>
  166. EOF
  167. # Set proper ownership on the test file
  168. sudo chown "$CURRENT_USER:$CURRENT_USER" "$TEST_FILE"
  169. sudo setfacl -m u:"$APACHE_USER":r "$TEST_FILE"
  170. echo -e "${GREEN}✓${NC} Test file created: $TEST_FILE"
  171. # ── Done ───────────────────────────────────────────────────────────────────
  172. echo ""
  173. echo -e "${GREEN}========================================${NC}"
  174. echo -e "${GREEN} Setup complete!${NC}"
  175. echo -e "${GREEN}========================================${NC}"
  176. echo ""
  177. echo " Document root : $DOCROOT"
  178. echo " Served at : http://localhost/"
  179. echo " Test page : http://localhost/info.php"
  180. echo ""
  181. echo " Next steps:"
  182. echo " 1. Open http://localhost/ in your browser"
  183. echo " 2. Place your PHP files in $DOCROOT"
  184. echo " 3. Check httpd status: sudo systemctl status httpd"
  185. echo " 4. View PHP errors: sudo tail -f /var/log/httpd/error_log"
  186. echo " 5. Check SELinux denials: sudo ausearch -m AVC -ts recent"
  187. echo ""
  188. echo -e "${YELLOW}Note:${NC} Files created in $DOCROOT will be owned by $CURRENT_USER"
  189. echo " and accessible by Apache. Use standard file permissions"
  190. echo " or ACLs if you need to adjust access for specific files."
  191. echo ""