Install and configure Apache 2 on Mojave with Homebrew

In this blog series, I take you through the steps I took to create my new setup: a blazingly fast and flexible setup with Apache, automatic DNS setup for .test domains, multiple PHP versions that I can easily switch between, Redis, Opcache, Image Magick, Blackfire and more. Links to the other blogs in this series are at the bottom.

My development setup part 2: Install and configure Apache 2 on Mojave with Homebrew

Install Xcode

First of all, you need to have Xcode installed. Open your terminal of choice and let's start running some commands.

# Check if Xcode is installed
xcode-select -v

# If it's not, then install it
xcode-select --install

Install Homebrew

# Check if Homebrew is already installed
brew --version

# If it's not, install it
# Follow the prompts in your terminal
ruby -e "$(curl -fsSL"

Check the health of your Homebrew installation.

brew update
brew doctor

Install missing libraries when on Mojave

If you are on OSX Mojave, you will need some additional libraries. You can install them with Homebrew.

brew install openldap libiconv

Stop pre-installed Apache

Install Apache via Homebrew OSX Mojave comes with Apache installed, but it's not easy to get this working with Homebrew. So first off, we are going to install Apache via Homebrew.

# Stop your running native Apache server
sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

# Install a fresh Apache server via Homebrew
brew install httpd

# Start Apache as a service
# This way it will be automatically loaded after every reboot of your Mac
sudo brew services start httpd

Once Apache is running you can check this by opening your browser and surfing to http://localhost:8080. (you new cache install will listen to port 8080). You should see "It works!".

Your sites folder

You probably already have a folder where you keep all you local sites. Most of the time, this will be something like Users/your_username/Sites. In my case it's Users/joostvanveen/htdocs.

Your sites folder should be somewhere in your Users/your_username folder, to avoid permissions problems. Also, it is good practice to create a subfolder or every project in there. Anyway, make sure you have a sites folder before configuring Apache.

Configure Apache

Now, let's setup Apache to our liking.

  1. Open /usr/local/etc/httpd/httpd.conf in a text editor like Sublime.
  2. Find Listen 8080 and replace it with Listen 80 (so you can leave :8080 off your local domains)
  3. Find DocumentRoot "/usr/local/var/www" and replace it with DocumentRoot "Users/your_username/your_sites_folder" (so your localhost will point to your sites folder)
  4. Find <Directory "/usr/local/var/www"> and replace it with <Directory "Users/your_username/your_sites_folder"> (to finish pointing your localhost to your sites folder)
  5. Find User www-data and replace it with User your_username (so Apache will run as your user and you won't have any permission problems)
  6. Find Group www-data and replace it with Group staff (so Apache's group permissions will match your personal group)
  7. Find AllowOverride None and replace it with AllowOverride All (to enable setting Apache directives via .htaccess files)
  8. Find ServerName and replace it with ServerName localhost. Find CustomLog "/usr/local/var/log/httpd/access_log" common and uncomment it. Check the log path, as it is handy to have all Apache logs in one place.
  9. Find LoadModule rewrite_module lib/httpd/modules/ and uncomment it (so you can set Apache RewriteRules in your .htaccess files)
  10. Find LoadModule deflate_module lib/httpd/modules/ and uncomment it (so you can set gzip compression in your .htaccess files)
  11. Find LoadModule deflate_module lib/httpd/modules/ and uncomment it (so you can set expiration headers in your .htaccess files)
  12. Find LoadModule deflate_module lib/httpd/modules/ and uncomment it (so you can set custom headers in your .htaccess files)

After you have changed configuration, you will need to restart Apache: sudo apachectl -k restart. Here's some other useful Apache commands:

sudo apachectl start
sudo apachectl stop
sudo apachectl -k restart
tail -f /usr/local/var/log/httpd/error_log
tail -f /usr/local/var/log/httpd/access_log
tail -f /usr/local/var/log/httpd/access_log
open /usr/local/etc/httpd/httpd.conf
tail-f /usr/local/var/log/httpd/ssl_request_log
##  Install PHP with Homebrew

Blogs in this series:

  1. Choosing a local PHP development setup on Mojave
  2. Install and configure Apache 2 on Mojave with Homebrew
  3. Install and configure multiple PHP versions on Mojave with Homebrew
  4. Installing PHP extensions and services with PECL and Homebrew