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 https://raw.githubusercontent.com/Homebrew/install/master/install)"
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.
- Open
/usr/local/etc/httpd/httpd.conf
in a text editor like Sublime. - Find
Listen 8080
and replace it withListen 80
(so you can leave :8080 off your local domains) - Find
DocumentRoot "/usr/local/var/www"
and replace it withDocumentRoot "Users/your_username/your_sites_folder"
(so your localhost will point to your sites folder) - Find
<Directory "/usr/local/var/www">
and replace it with<Directory "Users/your_username/your_sites_folder"> (t
o finish pointing your localhost to your sites folder) - Find
User www-data
and replace it withUser your_username
(so Apache will run as your user and you won't have any permission problems) - Find
Group www-data
and replace it withGroup staff
(so Apache's group permissions will match your personal group) - Find
AllowOverride None
and replace it withAllowOverride All
(to enable setting Apache directives via .htaccess files) - Find
ServerName www.example.com:8080
and replace it withServerName localhost
. FindCustomLog "/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. - Find
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
and uncomment it (so you can set Apache RewriteRules in your .htaccess files) - Find
LoadModule deflate_module lib/httpd/modules/mod_deflate.so
and uncomment it (so you can set gzip compression in your .htaccess files) - Find
LoadModule deflate_module lib/httpd/modules/mod_expires.so
and uncomment it (so you can set expiration headers in your .htaccess files) - Find
LoadModule deflate_module lib/httpd/modules/mod_headers.so
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:
- Choosing a local PHP development setup on Mojave
- Install and configure Apache 2 on Mojave with Homebrew
- Install and configure multiple PHP versions on Mojave with Homebrew
- Installing PHP extensions and services with PECL and Homebrew
Many thanks to the guys at getgrav.org, who wrote a great blog series on this topic: https://getgrav.org/blog/macos-mojave-apache-multiple-php-versions.