Skip to content

Instantly share code, notes, and snippets.

@levivoelz
Forked from DragonBe/php_apache_homebrew.md
Last active January 9, 2020 08:57
Show Gist options
  • Save levivoelz/8c006c26e8f2d9be82e556ff179c9bdd to your computer and use it in GitHub Desktop.
Save levivoelz/8c006c26e8f2d9be82e556ff179c9bdd to your computer and use it in GitHub Desktop.
Installation of Apache 2.4 and PHP 7.1 with Homebrew

Apache and PHP with Homebrew

The installation procedures

These installation procedures will set up your mac with PHP 7.2 and Apache 2.4.

Install Xcode command line tools (if not done yet)

xcode-select --install

Install Homebrew (if not done yet)

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

Unload (not uninstall) the provisioned Apache from macOS

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 boot (if desired)

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

Alternatively, run brew services start httpd to start Apache (preferred).

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 (if installed)

Example:

brew unlink php70

Install latest PHP version

Time to install latest PHP with the apache web server

brew install php --with-httpd24

Set date.timezone to America in /usr/local/etc/php/7.2/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/php72/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 and run PHP

brew services restart httpd
brew services start php

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

Set up your virtual hosts.

Uncomment these lines:

Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

in /usr/local/etc/apache2/2.4/httpd.conf and edit the vhosts file.

Example vhosts:

<VirtualHost *:8080>
  ServerName localhost
  DocumentRoot /usr/local/var/www
</VirtualHost>

<VirtualHost *:8080>
    # ServerAdmin [email protected]
    DocumentRoot "/Users/USERNAME/Sites/EXAMPLE.com"
    ServerName localhost
    # ServerAlias www.dummy-host.example.com
    ErrorLog "/usr/local/var/log/httpd/dev.EXAMPLE.com-error_log"
    # CustomLog "/usr/local/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment