Skip to content

Instantly share code, notes, and snippets.

@DragonBe
Last active November 20, 2022 18:15
Show Gist options
  • Star 75 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save DragonBe/0faebe58deced34744953e3bf6afbec7 to your computer and use it in GitHub Desktop.
Save DragonBe/0faebe58deced34744953e3bf6afbec7 to your computer and use it in GitHub Desktop.
Installation of Apache 2.4 and PHP 7.1 with Homebrew

I posted several talks about compiling PHP from source, but everyone was trying to convince me that a package manager like Homebrew was a more convenient way to install.

The purpose of Homebrew is simple: a package manager for macOS that will allow you to set up and install common packages easily and allows you to update frequently using simple commands.

I used a clean installation of macOS Sierra to ensure all steps could be recorded and tested. In most cases you already have done work on your Mac, so chances are you can skip a few steps in this tutorial.

Apache and PHP with homebrew

I’ve made this according to the installation instructions given on GetGrav.

The installation procedures

These installation procedures will set up your macOS Sierra with PHP 7.1 and Apache 2.4.

Install Xcode command line tools (if not done yet)

xcode-select --install

Install Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Set up for installation of PHP and Apache

brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew tap homebrew/apache

Unload the provisioned Apache from macOS Sierra

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Install Homebrew Apache

brew install httpd24 --with-privileged-ports --with-http2

Set up Apache for autostart at macOS Sierra (re)boot

sudo cp -v /usr/local/Cellar/httpd24/2.4.25/homebrew.mxcl.httpd24.plist /Library/LaunchDaemons
sudo chown -v root:wheel /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
sudo chmod -v 644 /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

Check to see Apache is running

ps -aef | grep httpd

You should see something similar as output

    0 23417     1   0  2:48PM ??         0:00.06 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND
    1 23420 23417   0  2:48PM ??         0:00.00 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND
    1 23421 23417   0  2:48PM ??         0:00.00 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND
    1 23422 23417   0  2:48PM ??         0:00.00 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND
    1 23423 23417   0  2:48PM ??         0:00.00 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND
    1 23424 23417   0  2:48PM ??         0:00.00 /usr/local/opt/httpd24/bin/httpd -D FOREGROUND
  501 23428   403   0  2:48PM ttys000    0:00.00 grep httpd

Uninstall previous versions of PHP

We assume you had PHP 7.0 installed

brew unlink php70

Install latest PHP version (PHP 7.1.2 at the time of writing)

Time to install latest PHP with the apache web server

brew install php71 --with-httpd24

Don’t forget to set date.timezone in /usr/local/etc/php/7.1/php.ini!!!

Configure Apache for PHP usage

Also make sure to set the following line in /usr/local/etc/apache2/2.4/httpd.conf (You might need to disable others):

LoadModule php7_module    /usr/local/opt/php71/libexec/apache2/libphp7.so

Also make sure you have set the following lines correctly:

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Time to restart apache

sudo /usr/local/bin/apachectl -k restart

Test your setup

Create a phpinfo.php in /usr/local/var/www/htdocs with the following contents:

<?php phpinfo();

You should see the famous PHP information page!

Next steps

Now you can set up your virtual hosts like you normally would do.

@danillonunes
Copy link

Hey, just to point that you can use Homebrew services manager to setup the installation and load of the launchdaemon. Instead of the whole process of the Set up Apache for autostart at macOS Sierra (re)boot you can just run sudo brew services start httpd24 (if you do not have the brew services installed yet it will be installed automatically).

@edarioq
Copy link

edarioq commented Dec 29, 2017

Thanks for the guide, just an update:

Warning: homebrew/php/php72: --with-httpd24 was deprecated; using --with-httpd instead!

@Reiszecke
Copy link

"Set up Apache for autostart at macOS Sierra (re)boot" didn't work like this for me anymore.

if you get No such file or directory then start with sudo cp -v /usr/local/Cellar/httpd/2 and tab your way from there, you can take half of the next couple of commands and hit tab again, it will fill in the correct (updated) paths

Haven't even finished setup yet but before I forget; THANK YOU for this gist, really helpful!

@MikeyBeLike
Copy link

brew services start httpd this works for autostart

@gem2016
Copy link

gem2016 commented Feb 19, 2018

Update:
homebrew/dupes
homebrew/versions
homebrew/apache

The above were deprecated
All formulae were migrated to Homebrew/homebrew-core or deleted. (https://github.com/Homebrew/homebrew-core)

@rbur0425
Copy link

rbur0425 commented Apr 9, 2018

this is the best guide i have found...it finally straightened out my set up. i had 2 apaches when brew switched to httpd and figuring out which was running was a real pain. uninstalled everything and followed this guide and it finally works! thank you!

@nemesyssoft
Copy link

All options using "--with" don't work.

@crmpicco
Copy link

This was really useful, thanks 👍

@DimitriDR
Copy link

Thank you for this very useful tutorial. Now I'm up-to-date and I'll stay easily!

@jmona789
Copy link

Getting these errors:

brew tap homebrew/dupes
Error: homebrew/dupes was deprecated. This tap is now empty and all its contents were either deleted or migrated.

brew tap homebrew/versions
Error: homebrew/versions was deprecated. This tap is now empty and all its contents were either deleted or migrated.

brew tap homebrew/homebrew-php
Error: homebrew/php was deprecated. This tap is now empty and all its contents were either deleted or migrated.

brew tap homebrew/apache
Error: homebrew/apache was deprecated. This tap is now empty and all its contents were either deleted or migrated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment