Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewstobbe/c4b15cf4f90cbc37db6456bdcdce8556 to your computer and use it in GitHub Desktop.
Save andrewstobbe/c4b15cf4f90cbc37db6456bdcdce8556 to your computer and use it in GitHub Desktop.

Homebrew Install Apache with PHP and MySQL on OSX

Setup a local Apache + PHP + MySQL + DnsMasq + PHPSwitcher environment using Homebrew on Mac OSX High Sierra that supports multiple versions of PHP and a self signed SSL certificate.

Install HomeBrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"	
brew doctor
brew update

Install Apache

Unload original OSX apache

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
brew install httpd24 --with-privileged-ports --with-http2

Load new Apache version at startup (2.4.34) in this case.

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

Make sure all http instances have been stopped before going on.

sudo apachectl -k stop
ps -aef | grep httpd
sudo apachectl -k start

Check for errors.

tail -f /usr/local/var/log/httpd/error_log 

Modify httpd.conf and test Apache.

sudo nano /usr/local/etc/httpd/httpd.conf 

Edit File.

DocumentRoot /Users/YourLocalUser/Sites/active-projects

AllowOverride All

Uncomment

rewrite_module

Edit User

User YourLocalUser
Group staff

Save File. Then mkdir ~/Sites/active-projects

Create an index page in new webroot.

echo "<h1>Active Projects Web Root</h1>" > ~/Sites/active-projects/index.html

Setup PHP versions

brew install php@5.6
brew install php@7.0
brew install php@7.1
brew install php@7.2

You may have the need to tweak configuration settings of PHP to your needs.The php.ini files for each version of PHP are located in the following directories:

/usr/local/etc/php/5.6/php.ini
/usr/local/etc/php/7.0/php.ini
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.2/php.ini

Switch to php5.6 to test

brew unlink php@7.2 && brew link --force --overwrite php@5.6

Quick php version test.

php -v

Edit /usr/local/etc/httpd/httpd.conf file

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

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

We can only have one module processing PHP at a time, so for now, add all versions then comment out all but the php56 entry:

LoadModule php5_module /usr/local/opt/php@5.6/lib/httpd/modules/libphp5.so
#LoadModule php7_module /usr/local/opt/php@7.0/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so
#LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so

Create an index.php page in new webroot.

echo "<?php phpinfo(); ?>" > ~/Sites/active-projects/index.php

Test it.

sudo apachectl -k restart

Open browser http://localhost/

Install php switcher

curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw > /usr/local/bin/sphp
chmod +x /usr/local/bin/sphp

It's IMPORTANT at this stage to fully stop the Apache sever, and start it again. Do not just restart it!

sudo apachectl -k stop
sudo apachectl start

Homebrew should have added its preferred /usr/local/bin and /usr/local/sbin to your path as part of its installation process. Quickly test this by typing:

echo $PATH

After you have completed these steps, you should be able to switch your PHP version by using the command sphp followed by a two digit value for the PHP version:

sphp 5.6
sphp 7.0
sphp 7.1
sphp 7.2

You will need to switch to each of your installed PHP versions and run update again to get updates for each PHP version and ensure you are running the version of PHP you intend.

brew update
brew ugrade

Information on php version.

brew info php@7.2

Virtual hosts

Apache already comes preconfigured to support this behavior but it is not enabled.

First you will need to uncomment the following lines in your httpd.conf file:

sudo nano /usr/local/etc/httpd/httpd.conf

Uncomment

LoadModule vhost_alias_module libexec/mod_vhost_alias.so

and:

Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

and add the following entries to httpd-vhosts.conf:

<VirtualHost *:80>
	DocumentRoot "/Users/YourLocalUser/Sites/active-projects"
	ServerName localhost
</VirtualHost>

<VirtualHost *:80>
	DocumentRoot "/Users/YourLocalUser/Sites/active-projects/myproject.test"
	ServerName myproject.test
</VirtualHost>

Make the directory

mkdir ~/Sites/active-projects/myproject.test

Install DnsMasq

brew install dnsmasq
echo 'address=/.test/127.0.0.1' > /usr/local/etc/dnsmasq.conf
sudo brew services start dnsmasq
sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'

Test it!

ping fakedomain.test

Install APCu

Switch to PHP 5.6 mode, then run the following brew commands to install autoconf:

sphp 5.6
brew install autoconf

Then you can install APCu via PECL.Using it requires a little more manual work than before when these packages were available via a single one-line brew install command.

pecl channel-update pecl.php.net
pecl install apcu-4.0.11

APCu Configuration

Remove the extension="apcu.so" entry that PECL adds to the top of your php.ini. Edit this file and remove the top line:

sudo nano /usr/local/etc/php/5.6/php.ini

Add a new file with an entry to the recently built apcu.so library:

sudo nano /usr/local/etc/php/5.6/conf.d/ext-apcu.ini

In this file paste the following:

[apcu]
extension="apcu.so"
apc.enabled=1
apc.shm_size=64M
apc.ttl=7200
apc.enable_cli=1

Restart Apache with sudo apachectl -k restart command.

APCu for other PHP versions

For PHP 7.0 and above use the 5.x release of APCu. Switch to PHP 7.0 and install the APCu library:

sphp 7.0
pecl uninstall -r apcu
pecl install apcu

Restart Apache with sudo apachectl -k restart command.

For PHP 7.1, repeat steps but use 7.1 instead of 7.0:

sphp 7.1
pecl uninstall -r apcu
pecl install apcu

and for PHP 7.2:

sphp 7.2
pecl uninstall -r apcu
pecl install apcu

Generate Self-Signed SSL Cert

cd /usr/local/etc/httpd
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

SSL VirtualHost

Modifications to your httpd.conf:

sudo nano /usr/local/etc/httpd/httpd.conf

In this file you should uncomment both the socache_shmcb_module, ssl_module, and also the include for the httpd-ssl.conf by removing the # on those lines:

LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
...
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
...

Next change the 8443 port to a standard 443. Open the SSL config file:

sudo nano /usr/local/etc/httpd/extra/httpd-ssl.conf

find:

Listen 8443

replace with:

Listen 443

then find:

<VirtualHost _default_:8443>
	# General setup for the virtual host
	DocumentRoot "/usr/local/var/www"
	ServerName www.example.com:8443

and replace the 8443 references with 443 and change the paths:

<VirtualHost _default_:443>
	# General setup for the virtual host
	DocumentRoot "/Users/YourLocalUser/Sites/active-projects"
	ServerName localhost:443

After saving this file, open up your /usr/local/etc/httpd/extra/httpd-vhosts.conf to add SSL based virtual hosts.

sudo nano /usr/local/etc/httpd/extra/httpd-vhosts.conf

Create a VirtualHost entry for each SSL virtual host.

<VirtualHost *:443>
	DocumentRoot "/Users/YourLocalUser/Sites/active-projects"
	ServerName localhost
	SSLEngine on
	SSLCertificateFile "/usr/local/etc/httpd/server.crt"
	SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>

Test it.

sudo apachectl configtest
sudo apachectl -k restart

Install MySQL

brew install mysql

Start the MySql server:

brew services start mysql

Configure your MySql installation with a root password:

mysql_secure_installation

Test that MySql is installed and configured:

mysql -u root -p

Exit mysql:

exit

Tweak my.cnf as needed.

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