setup_testenv_ubuntu.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #!/bin/bash
  2. #
  3. # setup_testenv_ubuntu.sh — General PHP development environment setup for Ubuntu
  4. # Usage: bash setup_testenv_ubuntu.sh
  5. # Sets up Apache (apache2) with PHP to serve /home/josef/Code 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 (Ubuntu uses sudo group for sudo access)
  30. if groups | grep -qw sudo; then
  31. echo -e "${GREEN}✓${NC} User '$CURRENT_USER' is in sudo group (has sudo privileges)"
  32. else
  33. echo -e "${YELLOW}WARNING:${NC} User '$CURRENT_USER' is not in the sudo 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="/home/josef/Code"
  48. APACHE_USER="www-data"
  49. APACHE_GROUP="www-data"
  50. APACHE_CONF="/etc/apache2/conf-available/dev.conf"
  51. # ── 1. Install Apache, PHP, and required modules ──────────────────────────
  52. echo ""
  53. echo "→ Installing Apache (apache2), PHP, and required modules..."
  54. sudo apt-get update
  55. sudo apt-get install -y apache2 php libapache2-mod-php php-mbstring php-xml php-cli jq acl
  56. echo -e "${GREEN}✓${NC} Packages installed"
  57. # ── 2. Ensure document root exists ────────────────────────────────────────
  58. echo ""
  59. echo "→ Ensuring document root exists: $DOCROOT"
  60. sudo mkdir -p "$DOCROOT"
  61. echo -e "${GREEN}✓${NC} Document root ready"
  62. # ── 3. Set ownership and permissions for Apache + current user ────────────
  63. echo ""
  64. echo "→ Setting permissions for user '$CURRENT_USER' and Apache user '$APACHE_USER'..."
  65. # Ensure ACL support is available (should be installed now)
  66. if ! command -v setfacl &>/dev/null; then
  67. echo " Installing ACL tools..."
  68. sudo apt-get install -y acl
  69. fi
  70. # Set ownership to current user (so user can easily create files)
  71. sudo chown -R "$CURRENT_USER:$CURRENT_USER" "$DOCROOT"
  72. # Set base permissions
  73. sudo chmod 0755 "$DOCROOT"
  74. # Set ACLs so both the current user and Apache can read/write
  75. sudo setfacl -R -m u:"$CURRENT_USER":rwx,u:"$APACHE_USER":rwx "$DOCROOT"
  76. sudo setfacl -R -d -m u:"$CURRENT_USER":rwx,u:"$APACHE_USER":rwx "$DOCROOT"
  77. # Also allow the apache group to access
  78. sudo setfacl -R -m g:"$APACHE_GROUP":r-x "$DOCROOT"
  79. sudo setfacl -R -d -m g:"$APACHE_GROUP":r-x "$DOCROOT"
  80. echo -e "${GREEN}✓${NC} Permissions configured (ACLs set for $CURRENT_USER and $APACHE_USER)"
  81. # ── 4. Create/update Apache configuration ─────────────────────────────────
  82. echo ""
  83. echo "→ Creating Apache configuration: $APACHE_CONF"
  84. sudo tee "$APACHE_CONF" > /dev/null << EOF
  85. # Development environment - serve /home/josef/Code on localhost
  86. <Directory "$DOCROOT">
  87. Options Indexes FollowSymLinks
  88. AllowOverride All
  89. Require all granted
  90. # Enable directory listing if no index file found
  91. DirectoryIndex index.php index.html index.htm
  92. </Directory>
  93. # Ensure PHP files are processed
  94. <FilesMatch \.php$>
  95. SetHandler application/x-httpd-php
  96. </FilesMatch>
  97. EOF
  98. sudo a2enconf dev > /dev/null
  99. echo -e "${GREEN}✓${NC} Apache configuration written and enabled"
  100. # ── 5. Configure PHP for development ──────────────────────────────────────
  101. echo ""
  102. echo "→ Configuring PHP for development..."
  103. PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')
  104. PHP_INI="/etc/php/$PHP_VERSION/apache2/php.ini"
  105. if [[ -f "$PHP_INI" ]]; then
  106. # Backup original
  107. sudo cp "$PHP_INI" "$PHP_INI.backup.$(date +%Y%m%d_%H%M%S)" 2>/dev/null || true
  108. # Enable error display for development
  109. sudo sed -i 's/^display_errors = .*/display_errors = On/' "$PHP_INI" 2>/dev/null || true
  110. sudo sed -i 's/^display_startup_errors = .*/display_startup_errors = On/' "$PHP_INI" 2>/dev/null || true
  111. sudo sed -i 's/^error_reporting = .*/error_reporting = E_ALL/' "$PHP_INI" 2>/dev/null || true
  112. echo -e "${GREEN}✓${NC} PHP configured for development (errors displayed)"
  113. else
  114. echo -e "${YELLOW}WARNING:${NC} PHP ini not found at $PHP_INI, skipping PHP configuration"
  115. fi
  116. # ── 6. Configure mail sending (msmtp) ──────────────────────────────────────
  117. echo ""
  118. echo "→ Installing and configuring msmtp for outgoing mail..."
  119. sudo apt-get install -y msmtp msmtp-mta
  120. MSMTP_CONF="/etc/msmtprc"
  121. sudo tee "$MSMTP_CONF" > /dev/null << 'EOF'
  122. defaults
  123. auth off
  124. tls on
  125. tls_certcheck off
  126. port 25
  127. host mailpit.medowar.de
  128. account default
  129. from josef@dev-framework13.med0.de
  130. EOF
  131. # World-readable: no credentials stored, and PHP's mail() (running as
  132. # www-data) needs to read this file when invoking msmtp as sendmail.
  133. sudo chown root:root "$MSMTP_CONF"
  134. sudo chmod 644 "$MSMTP_CONF"
  135. echo -e "${GREEN}✓${NC} msmtp installed and configured ($MSMTP_CONF)"
  136. # ── 7. Enable and start apache2 ────────────────────────────────────────────
  137. echo ""
  138. echo "→ Enabling and starting apache2..."
  139. sudo systemctl enable apache2
  140. sudo systemctl restart apache2
  141. echo -e "${GREEN}✓${NC} Apache (apache2) is running"
  142. # ── 8. Configure AppArmor permissions ──────────────────────────────────────
  143. echo ""
  144. echo "→ Checking AppArmor permissions..."
  145. if command -v aa-status &>/dev/null; then
  146. if sudo aa-status --enabled 2>/dev/null; then
  147. echo " AppArmor is enabled. The default apache2 profile permits"
  148. echo " read/write access under $DOCROOT, so no changes are needed."
  149. echo " If you see denials, check: sudo aa-status | grep apache2"
  150. echo -e "${GREEN}✓${NC} AppArmor check complete"
  151. else
  152. echo " AppArmor is installed but not enabled, skipping"
  153. fi
  154. else
  155. echo " (AppArmor tools not found, skipping AppArmor configuration)"
  156. fi
  157. # ── 9. Open firewall for HTTP ─────────────────────────────────────────────
  158. echo ""
  159. echo "→ Configuring firewall for HTTP..."
  160. if command -v ufw &>/dev/null; then
  161. if sudo ufw status | grep -qw active; then
  162. sudo ufw allow "Apache" 2>/dev/null || sudo ufw allow 80/tcp 2>/dev/null || true
  163. echo -e "${GREEN}✓${NC} Firewall updated (HTTP allowed)"
  164. else
  165. echo " ufw is installed but not active, skipping firewall rule"
  166. fi
  167. else
  168. echo " (ufw not found, skipping firewall configuration)"
  169. fi
  170. # ── 10. Create a test PHP file ─────────────────────────────────────────────
  171. echo ""
  172. echo "→ Creating test PHP file..."
  173. TEST_FILE="$DOCROOT/info.php"
  174. sudo tee "$TEST_FILE" > /dev/null << 'EOF'
  175. <?php
  176. phpinfo();
  177. ?>
  178. EOF
  179. # Set proper ownership on the test file
  180. sudo chown "$CURRENT_USER:$CURRENT_USER" "$TEST_FILE"
  181. sudo setfacl -m u:"$APACHE_USER":r "$TEST_FILE"
  182. echo -e "${GREEN}✓${NC} Test file created: $TEST_FILE"
  183. # ── Done ───────────────────────────────────────────────────────────────────
  184. echo ""
  185. echo -e "${GREEN}========================================${NC}"
  186. echo -e "${GREEN} Setup complete!${NC}"
  187. echo -e "${GREEN}========================================${NC}"
  188. echo ""
  189. echo " Document root : $DOCROOT"
  190. echo " Served at : http://localhost/"
  191. echo " Test page : http://localhost/info.php"
  192. echo ""
  193. echo " Next steps:"
  194. echo " 1. Open http://localhost/ in your browser"
  195. echo " 2. Place your PHP files in $DOCROOT"
  196. echo " 3. Check apache2 status: sudo systemctl status apache2"
  197. echo " 4. View PHP errors: sudo tail -f /var/log/apache2/error.log"
  198. echo " 5. Check AppArmor denials: sudo journalctl -k | grep -i apparmor"
  199. echo " 6. Test mail sending: echo 'test' | msmtp your@address"
  200. echo ""
  201. echo -e "${YELLOW}Note:${NC} Files created in $DOCROOT will be owned by $CURRENT_USER"
  202. echo " and accessible by Apache. Use standard file permissions"
  203. echo " or ACLs if you need to adjust access for specific files."
  204. echo ""