macOS · Apple Silicon M4 · Apache 2.4

Apache Web Server
Setup Guide

MacBook Air M4 — Complete Installation & Configuration

// Table of Contents
  1. Prerequisites & Homebrew
  2. Install Apache
  3. Start & Control Apache
  4. Configuration Files
  5. Virtual Hosts
  6. Enable PHP
  7. Firewall & Ports
  8. Troubleshooting
STEP 01

Prerequisites & Homebrew

Apple Silicon M4 Macs run on ARM64 architecture. Homebrew installs to /opt/homebrew on Apple Silicon (vs /usr/local on Intel). Make sure you are using a native ARM terminal — do not use Rosetta.

ℹ️

macOS ships with a legacy Apache, but it's outdated and unsupported. We'll use Homebrew to install a modern, maintained version.

// Install Xcode Command Line Tools

Terminal
xcode-select --install

// Install Homebrew

Terminal
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

// Add Homebrew to your PATH (Apple Silicon)

Terminal
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
# Verify Homebrew works
brew --version
STEP 02

Install Apache (httpd)

Homebrew's Apache formula is called httpd. Before installing, disable the built-in macOS Apache if it's running.

// Stop & Disable macOS Built-in Apache

Terminal
# Stop the built-in apache if running
sudo apachectl stop

# Disable it from auto-starting
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

// Install Apache via Homebrew

Terminal
brew install httpd

// Verify Installation

Terminal
httpd -v
# Expected output: Server version: Apache/2.4.x (Homebrew)

Apache installs to /opt/homebrew/opt/httpd/. Config files live at /opt/homebrew/etc/httpd/. Document root defaults to /opt/homebrew/var/www.

STEP 03

Start, Stop & Control Apache

Use brew services to manage Apache as a background service that auto-starts on login.

// Service Management

Terminal — Service Commands
# Start Apache (and auto-start on login)
brew services start httpd

# Stop Apache
brew services stop httpd

# Restart Apache
brew services restart httpd

# Check status
brew services info httpd

// Manual apachectl Commands

Terminal — Manual Control
# Start
/opt/homebrew/bin/apachectl start

# Stop
/opt/homebrew/bin/apachectl stop

# Graceful restart (applies config changes)
/opt/homebrew/bin/apachectl graceful

# Test config for syntax errors before restarting
/opt/homebrew/bin/apachectl configtest

// Test in Browser

After starting, open your browser and navigate to:

Browser URL
http://localhost:8080
# You should see: "It works!"
⚠️

Homebrew's Apache defaults to port 8080 (not 80) to avoid requiring root privileges. You can change this in httpd.conf.

STEP 04

Configuration Files

The main config file is /opt/homebrew/etc/httpd/httpd.conf. Open it in your editor:

// Open httpd.conf

Terminal
# Open with nano
nano /opt/homebrew/etc/httpd/httpd.conf

# Or open with VS Code
code /opt/homebrew/etc/httpd/httpd.conf

// Change Port to 80 (Optional — requires sudo)

Find and replace the Listen directive in httpd.conf:

httpd.conf — change
# Find this line:
Listen 8080

# Change to:
Listen 80
⚠️

Ports below 1024 require root. If using port 80, start Apache with sudo /opt/homebrew/bin/apachectl start or configure a port forwarding rule instead.

// Change Document Root

Your web files go in the Document Root. To change it to your home folder's Sites directory:

httpd.conf — Document Root
# Find and replace these two lines (use your username):
DocumentRoot "/Users/YOUR_USERNAME/Sites"
<Directory "/Users/YOUR_USERNAME/Sites">

// Create the Sites Directory

Terminal
mkdir -p ~/Sites
echo "<h1>Hello from Apache on M4!</h1>" > ~/Sites/index.html

// Directory Permissions in httpd.conf

Find the <Directory> block for your root and ensure it allows access:

httpd.conf — Directory Block
<Directory "/Users/YOUR_USERNAME/Sites">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

// Set User & Group

Find the User and Group lines and set them to your username:

httpd.conf — User/Group
User YOUR_USERNAME
Group staff

// Enable mod_rewrite (for .htaccess URL rewriting)

In httpd.conf, uncomment this line (remove the #):

httpd.conf — uncomment
# Before:
#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

# After (remove the #):
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

// Apply Changes

Terminal
# Always test config before restarting
/opt/homebrew/bin/apachectl configtest

# If "Syntax OK", restart
brew services restart httpd
STEP 05

Virtual Hosts

Virtual Hosts let you run multiple sites on one machine using local domain names like myproject.test.

// Enable Virtual Hosts in httpd.conf

Uncomment this line in httpd.conf:

httpd.conf
# Uncomment this line (remove the #):
Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

// Edit httpd-vhosts.conf

Terminal
nano /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
httpd-vhosts.conf — example
# Default catch-all (keep this first)
<VirtualHost *:8080>
    DocumentRoot "/opt/homebrew/var/www"
    ServerName localhost
</VirtualHost>

# Your custom local site
<VirtualHost *:8080>
    DocumentRoot "/Users/YOUR_USERNAME/Sites/myproject"
    ServerName myproject.test
    ErrorLog "/opt/homebrew/var/log/httpd/myproject-error.log"
    CustomLog "/opt/homebrew/var/log/httpd/myproject-access.log" combined
    <Directory "/Users/YOUR_USERNAME/Sites/myproject">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

// Add to /etc/hosts

Map your local domain name to 127.0.0.1:

Terminal
sudo nano /etc/hosts

# Add this line at the end:
127.0.0.1    myproject.test

Now visit http://myproject.test:8080 in your browser.

STEP 06

Enable PHP

Install PHP via Homebrew and wire it into Apache using the mod_php module (or FastCGI).

// Install PHP

Terminal
brew install php

# Verify
php -v

// Load PHP Module in httpd.conf

Add this line to the LoadModule section of httpd.conf:

httpd.conf — add PHP module
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

// Add PHP MIME Types

Also in httpd.conf, find the <IfModule mime_module> block and add:

httpd.conf — MIME type
<IfModule mime_module>
    # ...existing lines...
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
</IfModule>

// Set index.php as Default

Find the DirectoryIndex line and add index.php:

httpd.conf
DirectoryIndex index.php index.html

// Test PHP

Terminal
echo "<?php phpinfo(); ?>" > /opt/homebrew/var/www/info.php
# Visit http://localhost:8080/info.php

// Restart Apache

Terminal
brew services restart httpd
STEP 07

Firewall & Port Access

If you need Apache accessible on your local network (e.g., testing from another device), ensure macOS firewall allows it.

// Allow httpd Through macOS Firewall

Terminal
# Add httpd as an allowed application
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /opt/homebrew/opt/httpd/bin/httpd
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblockapp /opt/homebrew/opt/httpd/bin/httpd

// Check What's Listening on a Port

Terminal
# Check if port 8080 is in use
lsof -i :8080

# Find your local IP address
ipconfig getifaddr en0
ℹ️

Other devices on your network can reach your server at http://YOUR_LOCAL_IP:8080 — handy for mobile testing.

STEP 08

Troubleshooting

// View Apache Error Logs

Terminal
# Watch error log in real time
tail -f /opt/homebrew/var/log/httpd/error_log

# View access log
tail -f /opt/homebrew/var/log/httpd/access_log

// Common Fixes

Terminal — Diagnosis Commands
# Check syntax errors in config
/opt/homebrew/bin/apachectl configtest

# Check if httpd process is running
ps aux | grep httpd

# Force-stop all httpd processes
sudo pkill -9 httpd

# Reinstall httpd cleanly
brew reinstall httpd

# Check Homebrew for issues
brew doctor

// Useful File Paths Reference

Reference
# Main config file
/opt/homebrew/etc/httpd/httpd.conf

# Virtual hosts config
/opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

# Default document root
/opt/homebrew/var/www/

# Error log
/opt/homebrew/var/log/httpd/error_log

# Access log
/opt/homebrew/var/log/httpd/access_log

# Apache binary
/opt/homebrew/bin/apachectl

# Apache modules directory
/opt/homebrew/lib/httpd/modules/
🎉

You're all set! Visit http://localhost:8080 to confirm Apache is running. Use brew services list to check service status at any time.