Created
March 31, 2025 06:06
-
-
Save marchildmann/c5778b7aab36d77433df6f3c2618b8fa to your computer and use it in GitHub Desktop.
macOS Web Development Setup Script - Sets up Apache and PHP 8.4 with Sites directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # macOS Web Development Setup Script | |
| # For macOS 15 Sequoia - Sets up Apache and PHP 8.4 with Sites directory | |
| # Created on: $(date) | |
| # Make it executable: chmod +x setup-webdev.sh | |
| # Run it: ./setup-webdev.sh | |
| # After running the script, you'll be able to access: | |
| # http://localhost:8080 - Your web server | |
| # http://localhost:8080/info.php - PHP info page | |
| # http://localhost:8080/test.php - A simple PHP test script | |
| # Text formatting | |
| BOLD='\033[1m' | |
| GREEN='\033[0;32m' | |
| BLUE='\033[0;34m' | |
| RED='\033[0;31m' | |
| NC='\033[0m' # No Color | |
| # Print section header | |
| print_header() { | |
| echo -e "\n${BOLD}${BLUE}==== $1 ====${NC}\n" | |
| } | |
| # Print success message | |
| print_success() { | |
| echo -e "${GREEN}✓ $1${NC}" | |
| } | |
| # Print error message and exit | |
| print_error() { | |
| echo -e "${RED}✗ ERROR: $1${NC}" | |
| exit 1 | |
| } | |
| # Check if Homebrew is installed, install if needed | |
| check_homebrew() { | |
| print_header "Checking for Homebrew" | |
| if ! command -v brew &> /dev/null; then | |
| echo "Homebrew not found. Installing Homebrew..." | |
| /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" || print_error "Failed to install Homebrew" | |
| # Add Homebrew to path for ARM Macs if needed | |
| if [[ $(uname -m) == 'arm64' ]]; then | |
| echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile | |
| eval "$(/opt/homebrew/bin/brew shellenv)" | |
| fi | |
| else | |
| print_success "Homebrew is already installed" | |
| fi | |
| # Update Homebrew | |
| echo "Updating Homebrew..." | |
| brew update || print_error "Failed to update Homebrew" | |
| print_success "Homebrew updated successfully" | |
| } | |
| # Create Sites directory | |
| create_sites_directory() { | |
| print_header "Creating Sites Directory" | |
| # Create Sites directory if it doesn't exist | |
| if [ ! -d ~/Sites ]; then | |
| mkdir -p ~/Sites | |
| print_success "Created ~/Sites directory" | |
| else | |
| print_success "~/Sites directory already exists" | |
| fi | |
| # Set permissions | |
| chmod 755 ~/Sites | |
| # Create a test HTML file | |
| echo "<html><body><h1>macOS Web Development Setup</h1><p>If you can see this, your Apache server is working!</p></body></html>" > ~/Sites/index.html | |
| # Create a test PHP file | |
| echo "<?php phpinfo(); ?>" > ~/Sites/info.php | |
| # Create a simple test PHP script | |
| cat > ~/Sites/test.php << 'EOF' | |
| <?php | |
| echo "<h1>PHP Test</h1>"; | |
| echo "<p>Current PHP version: " . phpversion() . "</p>"; | |
| echo "<p>Current date: " . date('Y-m-d H:i:s') . "</p>"; | |
| echo "<p>Server name: " . $_SERVER['SERVER_NAME'] . "</p>"; | |
| echo "<p>Document root: " . $_SERVER['DOCUMENT_ROOT'] . "</p>"; | |
| ?> | |
| EOF | |
| print_success "Created test files in ~/Sites" | |
| } | |
| # Install and configure Apache | |
| install_apache() { | |
| print_header "Installing Apache" | |
| # Install Apache via Homebrew | |
| brew install httpd || print_error "Failed to install Apache" | |
| print_success "Apache installed successfully" | |
| # Configure Apache | |
| print_header "Configuring Apache" | |
| # Backup original config | |
| HTTPD_CONF="/opt/homebrew/etc/httpd/httpd.conf" | |
| cp $HTTPD_CONF "${HTTPD_CONF}.bak" | |
| print_success "Created backup of httpd.conf" | |
| # Modify the Apache configuration | |
| echo "Configuring Apache to use ~/Sites directory..." | |
| # Update the DocumentRoot and Directory permissions | |
| sed -i '' "s|DocumentRoot \"/opt/homebrew/var/www\"|DocumentRoot \"${HOME}/Sites\"|g" $HTTPD_CONF | |
| sed -i '' "s|<Directory \"/opt/homebrew/var/www\">|<Directory \"${HOME}/Sites\">|g" $HTTPD_CONF | |
| # Ensure proper directory permissions | |
| cat > /tmp/directory_config.txt << EOF | |
| Options Indexes FollowSymLinks MultiViews | |
| AllowOverride All | |
| Require all granted | |
| EOF | |
| # Replace the Directory block content | |
| perl -i -0pe "s|<Directory \"${HOME}/Sites\">(.*?)</Directory>|<Directory \"${HOME}/Sites\">\n$(cat /tmp/directory_config.txt)\n</Directory>|s" $HTTPD_CONF | |
| # Make sure necessary modules are enabled | |
| sed -i '' 's|#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so|LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so|g' $HTTPD_CONF | |
| # Comment out the virtual hosts include | |
| sed -i '' 's|Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf|# Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf|g' $HTTPD_CONF | |
| print_success "Apache configured to use ~/Sites directory" | |
| } | |
| # Install and configure PHP | |
| install_php() { | |
| print_header "Installing PHP 8.4" | |
| # Install PHP 8.4 | |
| brew install [email protected] || print_error "Failed to install PHP 8.4" | |
| print_success "PHP 8.4 installed successfully" | |
| # Link PHP 8.4 | |
| brew link [email protected] --force || print_error "Failed to link PHP 8.4" | |
| print_success "PHP 8.4 linked successfully" | |
| # Configure Apache to use PHP | |
| print_header "Integrating PHP with Apache" | |
| # Add PHP configuration to httpd.conf | |
| HTTPD_CONF="/opt/homebrew/etc/httpd/httpd.conf" | |
| # Check if PHP module is already configured | |
| if ! grep -q "LoadModule php_module" $HTTPD_CONF; then | |
| echo "Adding PHP module to Apache configuration..." | |
| echo "" >> $HTTPD_CONF | |
| echo "# PHP 8.4 configuration" >> $HTTPD_CONF | |
| echo "LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so" >> $HTTPD_CONF | |
| echo "" >> $HTTPD_CONF | |
| echo "<FilesMatch \\.php\$>" >> $HTTPD_CONF | |
| echo " SetHandler application/x-httpd-php" >> $HTTPD_CONF | |
| echo "</FilesMatch>" >> $HTTPD_CONF | |
| print_success "Added PHP module to Apache configuration" | |
| else | |
| print_success "PHP module already configured in Apache" | |
| fi | |
| # Update DirectoryIndex to include index.php | |
| if ! grep -q "DirectoryIndex index.php" $HTTPD_CONF; then | |
| echo "Updating DirectoryIndex to include index.php..." | |
| sed -i '' 's|DirectoryIndex index.html|DirectoryIndex index.php index.html|g' $HTTPD_CONF | |
| print_success "Updated DirectoryIndex to include index.php" | |
| else | |
| print_success "DirectoryIndex already includes index.php" | |
| fi | |
| # Start PHP service | |
| brew services start php || print_error "Failed to start PHP service" | |
| print_success "PHP service started" | |
| } | |
| # Start services and finalize | |
| start_services() { | |
| print_header "Starting Services" | |
| # Restart Apache | |
| brew services restart httpd || print_error "Failed to restart Apache" | |
| print_success "Apache service restarted" | |
| # Get the local IP address for display | |
| LOCAL_IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "localhost") | |
| print_header "Setup Complete!" | |
| echo -e "${BOLD}Your web server is now configured and running!${NC}" | |
| echo -e "\nAccess your sites at:" | |
| echo -e " ${BOLD}http://localhost:8080${NC} or ${BOLD}http://$LOCAL_IP:8080${NC}" | |
| echo -e "\nTest files created:" | |
| echo -e " ${BOLD}http://localhost:8080/index.html${NC} - Basic HTML test" | |
| echo -e " ${BOLD}http://localhost:8080/info.php${NC} - PHP info page" | |
| echo -e " ${BOLD}http://localhost:8080/test.php${NC} - Simple PHP test" | |
| echo -e "\nYour web files should be placed in: ${BOLD}~/Sites/${NC}" | |
| echo -e "\nApache config file: ${BOLD}$HTTPD_CONF${NC}" | |
| echo -e "PHP version installed: ${BOLD}$(php -v | head -n 1)${NC}" | |
| echo -e "\nTo restart Apache: ${BOLD}brew services restart httpd${NC}" | |
| echo -e "To restart PHP: ${BOLD}brew services restart php${NC}" | |
| } | |
| # Main script execution | |
| main() { | |
| print_header "macOS Web Development Setup" | |
| echo "This script will set up a local web development environment on macOS 15 Sequoia" | |
| echo "It will install and configure:" | |
| echo " - Homebrew (if not already installed)" | |
| echo " - Apache web server" | |
| echo " - PHP 8.4" | |
| echo " - ~/Sites directory for web files" | |
| # Confirm before proceeding | |
| echo -e "\nPress Enter to continue or Ctrl+C to cancel..." | |
| read | |
| # Run steps | |
| check_homebrew | |
| create_sites_directory | |
| install_apache | |
| install_php | |
| start_services | |
| } | |
| # Run the script | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment