OSX 10.11 Setup Multiple Versions of Apache, PHP and MySQL

In the first part, we will walk you through setting up and configuring the OS X built-in Apache and multiple PHP versions. In the second part, we will cover MySQL, Apache virtual hosts, APC caching, and Xdebug installation.

Defaut Apache and Php

OS X 10.11 El Capitan comes with the Apache 2.4 pre-installed. By default, the document root is configured as /Library/WebServer/Documents. As this is a development machine, let’s assume we want to change the document root to point to a folder in our own home directory. To do this, we will need to edit Apache’s configuration file.

/etc/apache2/httpd.conf
  • change DocumentRoot “/Library/WebServer/Documents” to DocumentRoot “/Users/your_user/Sites”
  • change AllowOverride None to AllowOverride All in section <Directory “/Users/your_user/Sites”>
  • change User from _www to your_user, Group from _www to staff
  • add the following:

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
    
  • change the following from

    <IfModule dir_module>
        DirectoryIndex index.html
    </IfModule>
    

    to

    <IfModule dir_module>
        DirectoryIndex index.html index.php
    </IfModule>
    

image

Brew install different php versions

$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
$ brew install php54
$ brew unlink php54
$ brew install php55
$ brew unlink php55
$ brew install php56
$ brew unlink php56
$ brew install php70

php configuration location:

/usr/local/etc/php/5.4/php.ini
/usr/local/etc/php/5.5/php.ini
/usr/local/etc/php/5.6/php.ini
/usr/local/etc/php/7.0/php.ini

apache setup - take 1

# Brew PHP LoadModule
LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so

uncomment line:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

PHP Switcher Script

install

$ mkdir -p ~/bin/
$ curl -L https://raw.githubusercontent.com/conradkleinespel/sphp-osx/master/sphp > ~/bin/sphp
$ chmod +x ~/bin/sphp

apache setup - take 2

# Brew PHP LoadModule for `sphp` switcher
LoadModule php5_module /usr/local/lib/libphp5.so
#LoadModule php7_module /usr/local/lib/libphp7.so

switch php

$ sphp 54

image

$ sphp 55

image

$ sphp 56

image

$ sphp 70

image

Other Commands

list php minor versions

brew info php55

result:

homebrew/php/php55: stable 5.5.30 (bottled), HEAD
https://php.net
Conflicts with: php53, php54, php56, php70
/usr/local/Cellar/php55/5.5.28 (498 files, 51M)
  Poured from bottle
/usr/local/Cellar/php55/5.5.29_3 (327 files, 49M)
  Poured from bottle
/usr/local/Cellar/php55/5.5.30 (327 files, 49M)
  Poured from bottle

php version:

php -v

switch php minor version

brew switch php55 5.5.28

Apache mod_proxy to Tomcat

edit /private/etc/apache2/extra/httpd-vhosts.conf (same as /etc/apache2/extra/httpd-vhosts.conf)

<VirtualHost *:80>
    ServerAdmin webmaster@api.sitexa.com
    ServerName api.sitexa.com

    ProxyRequests On
    ProxyPreserveHost On

    <Proxy * >
        Order deny,allow
        Allow from all
    </Proxy>

    #Tomcat server
    ProxyPass / ajp://localhost:8109/
    ProxyPassReverse / ajp://localhost:8109/

    ErrorLog "/private/var/log/apache2/api-error_log"
    CustomLog "/private/var/log/apache2/api-access_log" common
</VirtualHost>

and copy Tomcat t0 Tomcat8180, edit server.xml to make something different:

<Connector port="8180" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

...

<Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />

start tomcat8180, restart apache http server, browse it:

image

Written on May 12, 2016