Install Apache with Homebrew

Installing Apache

# Unload Apache
sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

# "homebrew-apache/httpd22" relies on "homebrew-dupes/zlib"
# and install Apache 2.2 with the event MPM, and we'll use Homebrew's OpenSSL library
# since it's more up-to-date than OS X's:
brew tap homebrew/dupes
brew install -v homebrew/apache/httpd22 --with-brewed-openssl --with-mpm-event

# In order to get Apache and PHP to communicate via PHP-FPM, we'll install the mod_fastcgi module:
brew install -v homebrew/apache/mod_fastcgi --with-brewed-httpd22

Setting up Apache

# Load PHP-FPM via mod_fastcgi
LoadModule fastcgi_module    ${MODFASTCGIPREFIX}/libexec/

<IfModule fastcgi_module>
  FastCgiConfig -maxClassProcesses 1 -idle-timeout 1500

  # Prevent accessing FastCGI alias paths directly
  <LocationMatch "^/fastcgi">
    Order Deny,Allow
    Deny from All
    Allow from env=REDIRECT_STATUS

  FastCgiExternalServer /php-fpm -host -pass-header Authorization -idle-timeout 1500
  ScriptAlias /fastcgiphp /php-fpm
  Action php-fastcgi /fastcgiphp

  # Send PHP extensions to PHP-FPM
  AddHandler php-fastcgi .php

  # PHP options
  AddType text/html .php
  DirectoryIndex index.php index.html

# Include our VirtualHosts
Include ${USERHOME}/Sites/httpd-vhosts.conf

# We'll also create folders for logs and SSL files:
mkdir -pv ~/Sites/{logs,ssl}

# Let's populate the ~/Sites/httpd-vhosts.conf file:
touch ~/Sites/httpd-vhosts.conf

(export USERHOME=$(dscl . -read /Users/`whoami` NFSHomeDirectory | awk -F"\: " '{print $2}') ; cat > ~/Sites/httpd-vhosts.conf <<EOF
# Listening ports.
#Listen 8080  # defined in main httpd.conf
Listen 8443

# Use name-based virtual hosting.
NameVirtualHost *:8080
NameVirtualHost *:8443

# Set up permissions for VirtualHosts in ~/Sites
<Directory "${USERHOME}/Sites">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all

# For http://localhost in the users' Sites folder
<VirtualHost _default_:8080>
    ServerName localhost
    DocumentRoot "${USERHOME}/Sites"
<VirtualHost _default_:8443>
    ServerName localhost
    Include "${USERHOME}/Sites/ssl/"
    DocumentRoot "${USERHOME}/Sites"

# VirtualHosts

## Manual VirtualHost template for HTTP and HTTPS
#<VirtualHost *:8080>
#  ServerName
#  CustomLog "${USERHOME}/Sites/logs/" combined
#  ErrorLog "${USERHOME}/Sites/logs/"
#  DocumentRoot "${USERHOME}/Sites/"
#<VirtualHost *:8443>
#  ServerName
#  Include "${USERHOME}/Sites/ssl/"
#  CustomLog "${USERHOME}/Sites/logs/" combined
#  ErrorLog "${USERHOME}/Sites/logs/"
#  DocumentRoot "${USERHOME}/Sites/"

# Automatic VirtualHosts
# A directory at ${USERHOME}/Sites/webroot can be accessed at
# In Drupal, uncomment the line with: RewriteBase /

# This log format will display the per-virtual-host as the first field followed by a typical log line
LogFormat "%V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedmassvhost

# Auto-VirtualHosts with .dev
<VirtualHost *:8080>
  ServerName dev
  ServerAlias *.dev

  CustomLog "${USERHOME}/Sites/logs/dev-access_log" combinedmassvhost
  ErrorLog "${USERHOME}/Sites/logs/dev-error_log"

  VirtualDocumentRoot ${USERHOME}/Sites/%-2+
<VirtualHost *:8443>
  ServerName dev
  ServerAlias *.dev
  Include "${USERHOME}/Sites/ssl/"

  CustomLog "${USERHOME}/Sites/logs/dev-access_log" combinedmassvhost
  ErrorLog "${USERHOME}/Sites/logs/dev-error_log"

  VirtualDocumentRoot ${USERHOME}/Sites/%-2+

# Create the SSL files:
(export USERHOME=$(dscl . -read /Users/`whoami` NFSHomeDirectory | awk -F"\: " '{print $2}') ; cat > ~/Sites/ssl/ <<EOF
SSLEngine On
SSLProtocol all -SSLv2 -SSLv3
SSLCertificateFile "${USERHOME}/Sites/ssl/selfsigned.crt"
SSLCertificateKeyFile "${USERHOME}/Sites/ssl/private.key"

openssl req \
  -new \
  -newkey rsa:2048 \
  -days 3650 \
  -nodes \
  -x509 \
  -subj "/C=US/ST=State/L=City/O=Organization/OU=$(whoami)/CN=*.dev" \
  -keyout ~/Sites/ssl/private.key \
  -out ~/Sites/ssl/selfsigned.crt

# Start Homebrew's Apache and set to start on login:
ln -sfv $(brew --prefix httpd22)/homebrew.mxcl.httpd22.plist ~/Library/LaunchAgents
launchctl load -Fw ~/Library/LaunchAgents/homebrew.mxcl.httpd22.plist

# The following command will create the file /Library/LaunchDaemons/co.echo.httpdfwd.plist
# as root, and owned by root, since it needs elevated privileges:
sudo bash -c 'export TAB=$'"'"'\t'"'"'
cat > /Library/LaunchDaemons/co.echo.httpdfwd.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
${TAB}${TAB}<string>echo "rdr pass proto tcp from any to any port {80,8080} -> port 8080" | pfctl -a "" -Ef - &amp;&amp; echo "rdr pass proto tcp from any to any port {443,8443} -> port 8443" | pfctl -a "" -Ef - &amp;&amp; sysctl -w net.inet.ip.forwarding=1</string>

# Load it!
sudo launchctl load -Fw /Library/LaunchDaemons/co.echo.httpdfwd.plist

Leave a Reply

Your email address will not be published. Required fields are marked *