How to Setup PHP & MySQL on Mac OS X

· 1 min read

If you’re a PHP developer on Mac OS X, you’ve probably heard of the applications MAMP or XAMPP. They are the easiest way to setup a local development environment with Apache, PHP, and MySQL (AMP stack). Having said that, Mac OS X comes with Apache and PHP out of the box. So, once you feel comfortable around your Mac, I recommend to experiment with setting up your own environment to gain more control and independence. It’s only four steps to configure:

1. Activate PHP

PHP isn’t launched on boot by default. To enable it, uncomment the following line in /etc/apache2/httpd.conf:

LoadModule php5_module libexec/apache2/libphp5.so

2. Install Homebrew

Homebrew can be described as the missing package manager for OS X, and it makes installing MySQL a breeze.

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

3. Install MySQL via Homebrew

brew install mysql
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

4. Secure MySQL

/usr/local/Cellar/mysql/ 5.5.27/bin/mysql \_secure\_installation

That’s it! In case you’d like to manage your databases with a GUI, you can install phpMyAdmin or use the free Mac app Sequel Pro.

To comfortably start, stop, and restart Apache and MySQL, I wrote the following shell script:

#! /bin/sh

# ----------------------------- #
# Apache + MySQL Control Script
# ----------------------------- #

command=$1

if [ $# -eq 0 ]
then
    echo "Please provide a command: start, restart, stop"
    read command
fi

if [ $command = "start" -o $command = "restart" -o $command = "stop" ]
then
    mysql.server $command
    sudo apachectl $command
else
    echo "Unknown command provided."
fi