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
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
# 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.
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
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.
Now, let's setup Apache to our liking.
/usr/local/etc/httpd/httpd.confin a text editor like Sublime.
Listen 8080and replace it with
Listen 80(so you can leave :8080 off your local domains)
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)
<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)
User www-dataand replace it with
User your_username(so Apache will run as your user and you won't have any permission problems)
Group www-dataand replace it with
Group staff(so Apache's group permissions will match your personal group)
AllowOverride Noneand replace it with
AllowOverride All(to enable setting Apache directives via .htaccess files)
ServerName www.example.com:8080and replace it with
ServerName localhost. Find
CustomLog "/usr/local/var/log/httpd/access_log" commonand uncomment it. Check the log path, as it is handy to have all Apache logs in one place.
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.soand uncomment it (so you can set Apache RewriteRules in your .htaccess files)
LoadModule deflate_module lib/httpd/modules/mod_deflate.soand uncomment it (so you can set gzip compression in your .htaccess files)
LoadModule deflate_module lib/httpd/modules/mod_expires.soand uncomment it (so you can set expiration headers in your .htaccess files)
LoadModule deflate_module lib/httpd/modules/mod_headers.soand 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.