The default Mac OS X stack

If you are running Mac OS X Snow Leopard, you should already have PHP 5.3.3 installed on your system.

To be sure, open Applications » Utilities » Terminal and check it yourself:

/usr/bin/php -v

You just have to turn on the Apple’s Personal Web Sharing in the System Preferences and place your projects in $HOME/Sites folder.

But you will need to install MySQL or any SGBDR/NoSQL Manager by hand (getting the tarballs and fighting with dependencies, configuration and compilation stuff). Installing Apache/PHP modules and libraries can also be a pain. Personally, I rather prefer the MAMP solution or using package managers such as MacPorts or Homebrew. To my point of view, the default Mac OS X stack is only suitable for simple needs.

MAMP: Drag, drop, done.

This is the quick way to get a ready-to-go PHP 5.3 environment on a Mac.

Download MAMP, drag and drop the MAMP folder into the Applications folder and that’s all.

You can easily switch between two major PHP versions (PHP 5.2 or PHP 5.3) in a single click thanks to the Applications » MAMP » MAMP application. As the default MAMP PHP version is 5.2, launch the MAMP application, go in the preferences pane and change the current PHP version.

You now have to override your system PATH.

Edit your $HOME/.profile or $HOME/.bash_profile file, then add these lines at the end:

export PATH=/Applications/MAMP/bin/php5.3/bin:$PATH

Source it:

source $HOME/.profile

Check your PHP version:

php -v

This command should return PHP 5.3.2 (cli) (or above).

Cook your own stack with MacPorts

MacPorts is the most popular package manager for Mac OS X. Homebrew is also very powerful. With MacPorts, you can install your own stack with a high level of configuration and optimization. Everything is compiled from source and MacPorts manages itself all dependencies. Obviously, this takes time but it’s worth it. Let’s go?

Turn off Apple’s Personal Web Sharing in the System Preferences.

Install MacPorts.

Open a terminal (Applications » Utilities » Terminal).

Update the ports tree:

sudo port -d selfupdate

Install Apache2:

sudo port install apache2

Activate Apache2 to start it automatically at the boot:

sudo port load apache2

Go in your browser and enter the URL: http://localhost. You should see “It works!”.

Install MySQL:

sudo port install mysql5-server

Set up the “mysql” database:

sudo -u _mysql mysql_install_db5
sudo chown -R mysql:mysql /opt/local/var/db/mysql5/
sudo chown -R mysql:mysql /opt/local/var/run/mysql5/
sudo chown -R mysql:mysql /opt/local/var/log/mysql5/

Activate MySQL to start it automatically at the boot:

sudo port load mysql5-server

Set the MySQL’s root password (the current one is empty):

mysqladmin5 -u root -p password <your-password>

Try to log you as root:

mysql5 -u root -p

Then exit the session:

mysql> exit;

Secure the configuration:


Install PHP:

sudo port install php5 +apache2 +pear
sudo port install php5-mysql php5-sqlite php5-xdebug php5-mbstring php5-iconv \
php5-posix php5-apc

Register PHP with Apache2:

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5"

Set up the PHP configuration file:

cd /opt/local/etc/php5
sudo cp php.ini-development php.ini

Set up the timezone in /opt/local/etc/php5/php.ini:

; Defines the default timezone used by the date functions
date.timezone = "Europe/Paris"

Set up the MySQL default socket:

sudo -i
cd /opt/local/etc/php5
cp php.ini php.ini.bak
defSock=`/opt/local/bin/mysql_config5 --socket`
cat php.ini | sed \
  -e "s#pdo_mysql\.default_socket.*#pdo_mysql\.default_socket=${defSock}#" \
  -e "s#mysql\.default_socket.*#mysql\.default_socket=${defSock}#" \
  -e "s#mysqli\.default_socket.*#mysqli\.default_socket=${defSock}#" > tmp.ini
grep default_socket tmp.ini
mv tmp.ini php.ini
rm php.ini.bak

Edit /opt/local/apache2/conf/httpd.conf file.

Find these lines:

<IfModule dir_module>
    DirectoryIndex index.html

Then, add index.php:

<IfModule dir_module>
    DirectoryIndex index.php index.html

At the end of the file, add this line:

Include conf/extra/mod_php.conf

Restart Apache2:

sudo /opt/local/apache2/bin/apachectl -k restart


Adding a VirtualHost

For a MacPorts configuration, edit /opt/local/apache2/conf/httpd.conf and uncomment this line:

Include conf/extra/httpd-vhosts.conf

Then edit /opt/local/apache2/conf/extra/httpd-vhosts.conf.

Remove dummy examples and replace them with your own configuration:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot "/absolute/path/to/your/project"
    ServerName project.localhost
    <Directory "/absolute/path/to/your/project">
        Options Indexes FollowSymlinks
        AllowOverride All
        Order allow,deny
        Allow from all
    ErrorLog "logs/project-error_log"
    CustomLog "logs/project-access_log" common

Restart Apache2:

sudo /opt/local/apache2/bin/apachectl -k restart

For a MAMP configuration, add your VirtualHost at the end of Applications/MAMP/conf/httpd.conf file, then restart the server.

Don’t forget to add your host in /private/etc/hosts:	localhost project.localhost

In the folder of your project, create a phpinfo.php file:

<?php phpinfo(); ?>

Now, open your browser and go to http://project.localhost/phpinfo.php.

You should see the PHP Info page.