Jordan Savant # Software Engineer

From: https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions


APACHE INSTALLATION

Install Xcode and Xcode CLI Tools (xcode-select --install)
Install homebrew

Check installed correctly
> brew doctor

Tap extra homebrew repos
> brew tap homebrew/dupes
> brew tap homebrew/versions
> brew tap homebrew/php
> brew tap homebrew/apache

Update homebrew
> brew update

Stop any existing apache
> sudo apachectl stop

Install homebrew apache
> brew install httpd24 --with-privileged-ports --with-http2

This will produce a summary like:
/usr/local/Cellar/httpd24/2.4.25: 212 files, 4.5MB, built in 2 minutes 31 seconds

Use this path for installing apache
> 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 localhost:80

Test you can restart apache
> sudo apachectl -k restart

View error log for any issues
> tail -f /usr/local/var/log/apache2/error_log

How to use:
$ sudo apachectl start
$ sudo apachectl stop
$ sudo apachectl -k restart


APACHE CONFIGURATION

Change httpd.conf settings to make it devworthy
> vim /usr/local/etc/apache2/2.4/httpd.conf
  ... Enable mode rewrite
  172 #LoadModule rewrite_module libexec/mod_rewrite.so
  173 LoadModule rewrite_module libexec/mod_rewrite.so
  ... Change User:Group apache runs as
  184 #User daemon
  185 #Group daemon
  186 User jsavant
  187 Group staff
  ... Change the document room
  238 #DocumentRoot "/usr/local/var/www/htdocs"
  239 #<Directory "/usr/local/var/www/htdocs">
  240 DocumentRoot "/Users/jsavant/Sites"
  241 <Directory "/Users/jsavant/Sites">
  ... Enable directory overrides
  261     #AllowOverride None
  262     AllowOverride All

Create Sites
> mkdir ~/Sites
> echo "<h1>My User Web Root</h1>" > ~/Sites/index.html

Restart Apache
> sudo apachectl -k restart


PHP INSTALLATION

Install multiple PHP versions with homebrew
> brew install php55 --with-httpd24
> brew unlink php55
> brew install php56 --with-httpd24
> brew unlink php56
> brew install php70 --with-httpd24
> brew unlink php70
> brew install php71 --with-httpd24
// brew install php55 --with-httpd24 && brew unlink php55; brew install php56 --with-httpd24 && brew unlink php56; brew install php70 --with-httpd24 && brew unlink php70; brew install php71 --with-httpd24

Edit INI files to set timezone, warnings, memory etc
> vim /usr/local/etc/php/5.5/php.ini
> vim /usr/local/etc/php/5.6/php.ini
> vim /usr/local/etc/php/7.0/php.ini
> vim /usr/local/etc/php/7.1/php.ini

Tell Apache to use one version of PHP
> vim /usr/local/etc/apache2/2.4/httpd.conf
  ... Change generated module paths to point to generic entries
  174 #LoadModule php5_module        /usr/local/Cellar/php55/5.5.38_11/libexec/apache2/libphp5.so
  175 #LoadModule php5_module        /usr/local/Cellar/php56/5.6.30_6/libexec/apache2/libphp5.so
  176 #LoadModule php7_module        /usr/local/Cellar/php70/7.0.15_8/libexec/apache2/libphp7.so
  177 #LoadModule php7_module        /usr/local/Cellar/php71/7.1.2_13/libexec/apache2/libphp7.so
  178 #LoadModule php5_module        /usr/local/opt/php55/libexec/apache2/libphp5.so
  179 LoadModule php5_module        /usr/local/opt/php56/libexec/apache2/libphp5.so
  180 #LoadModule php7_module        /usr/local/opt/php70/libexec/apache2/libphp7.so
  181 #LoadModule php7_module        /usr/local/opt/php71/libexec/apache2/libphp7.so
  ...
  285 #<IfModule dir_module>
  286 #    DirectoryIndex index.html
  287 #</IfModule>
  288 <IfModule dir_module>
  289     DirectoryIndex index.php index.html
  290 </IfModule>
  291 <FilesMatch \.php$>
  292     SetHandler application/x-httpd-php
  293 </FilesMatch>

Restart Apache
> sudo apachectl -k restart

Install PHP Switcher script
> curl -L https://gist.github.com/w00fz/142b6b19750ea6979137b963df959d11/raw > /usr/local/bin/sphp
> chmod +x /usr/local/bin/sphp

Update Apache to use switcher module
> vim /usr/local/etc/apache2/2.4/httpd.conf
  ... comment out modules and add switcher versions (7 turned off)
  174 #LoadModule php5_module        /usr/local/Cellar/php55/5.5.38_11/libexec/apache2/libphp5.so
  175 #LoadModule php5_module        /usr/local/Cellar/php56/5.6.30_6/libexec/apache2/libphp5.so
  176 #LoadModule php7_module        /usr/local/Cellar/php70/7.0.15_8/libexec/apache2/libphp7.so
  177 #LoadModule php7_module        /usr/local/Cellar/php71/7.1.2_13/libexec/apache2/libphp7.so
  178 #LoadModule php5_module        /usr/local/opt/php55/libexec/apache2/libphp5.so
  179 #LoadModule php5_module        /usr/local/opt/php56/libexec/apache2/libphp5.so
  180 #LoadModule php7_module        /usr/local/opt/php70/libexec/apache2/libphp7.so
  181 #LoadModule php7_module        /usr/local/opt/php71/libexec/apache2/libphp7.so
  182 LoadModule php5_module        /usr/local/lib/libphp5.so
  183 #LoadModule php7_module        /usr/local/lib/libphp7.so

Switch by:
> sphp 55
> sphp 56


INSTALL MYSQL/MARIADB

Install with brew
> brew install mariadb
> mysql_install_db

Start server
> mysql.server start

Secure the server and set root password
> /usr/local/bin/mysql_secure_installation


VIRTUAL HOSTS

Ensure we have it enabled
> vim /usr/local/etc/apache2/2.4/httpd.conf
  ... Uncomment module
  165 #LoadModule vhost_alias_module libexec/mod_vhost_alias.so
  166 LoadModule vhost_alias_module libexec/mod_vhost_alias.so
  ... Include the config
  520 #Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf
  521 Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

Add/Edit virtual host definitions
> vim /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf
   ...  Set a the default rule first to go to Sites
   23 <VirtualHost *:80>
   24     DocumentRoot "/Users/jsavant/Sites"
   25     ServerName localhost
   26 </VirtualHost

Restart apache
> sudo apachectl restart


DNSMASQ SETUP

Install dnsmasq
> brew install dnsmasq

Create a .dev mask to local machine
> echo 'address=/.dev/127.0.0.1' > /usr/local/etc/dnsmasq.conf

Make it a service that starts automatically
> sudo brew services start dnsmasq

Add it to resolvers
> sudo mkdir -v /etc/resolver
> sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

Test it
> ping bogus.dev


XDEBUG INSTALL

Install for each version
> sphp 55
> brew install php55-xdebug
> sphp 56
> brew install php56-xdebug
> sphp 70
> brew install php70-xdebug
> sphp 71
> brew install php71-xdebug

Install xdebug toggle
> brew install xdebug-osx

Use with
> xdebug-toggle
> xdebug-toggle on
> xdebug-toggle off


SSL INSTALL

Enable SSL Support in Apache
> vim /usr/local/etc/apache2/2.4/httpd.conf
  ... Uncomment module
   88 #LoadModule socache_shmcb_module libexec/mod_socache_shmcb.so
   89 LoadModule socache_shmcb_module libexec/mod_socache_shmcb.so
  ...
  144 #LoadModule ssl_module libexec/mod_ssl.so
  145 LoadModule ssl_module libexec/mod_ssl.so
  ...
  540 #Include /usr/local/etc/apache2/2.4/extra/httpd-ssl.conf
  541 Include /usr/local/etc/apache2/2.4/extra/httpd-ssl.conf

Setup SSL on Hosted Sites
> vim /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf
   43 <VirtualHost *:443>
   44     DocumentRoot "/Users/jsavant/Sites/guidelive/dmn_guidelive_presentation_2014/docroot"
   45     ServerName glpres.dev
   46     SSLEngine on
   47     SSLCertificateFile "/usr/local/etc/apache2/2.4/server.crt"
   48     SSLCertificateKeyFile "/usr/local/etc/apache2/2.4/server.key"
   49 </VirtualHost>

Generate SSL Key and Certs
> cd /usr/local/etc/apache2/2.4
> openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

For the prompts enter sensible information, but Common name should be your ServerName in the vhosts file
  Country Name (2 letter code) [AU]:US
  State or Province Name (full name) [Some-State]:Texas
  Locality Name (eg, city) []:Dallas
  Organization Name (eg, company) [Internet Widgits Pty Ltd]:Lifeblue
  Organizational Unit Name (eg, section) []:Development
  Common Name (e.g. server FQDN or YOUR name) []:glpres.dev
  Email Address []:

Rerun for more domains you need
> openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
  Common Name (e.g. server FQDN or YOUR name) []:dnpres.dev

Double check and restart Apache
> sudo apachectl configtest
> sudo apachectl -k restart