Getting ready to develop on OSX + Macports (PHP, Tomcat, MySQL, Apache, PostgreSQL)

This is a quick and nasty post with the exact steps I went through to get my Macbook Air running Lion ready to develop PHP, Tomcat, MySQL, Apache and PostgreSQL. I know I need to tidy up the formatting.
These instructions built on three sources:
Chad Kieffer’s apache2 and php5 install guide
Chad Kieffer’s MySQL5 install guide
Macports install guide

Set up macports

Install XCode 4.3.2 from the app store.
Install the XCode command line tools by launching XCode and going to Open XCode->Preferences->Downloads->Components->Install Command Line Tools

Run the following command to set up the XCode environment:
sudo xcode-select -switch /Applications/

Dowload and install Java from the Apple web site – here.

Download macports and run the installer.

Set up apache

sudo port install apache2
Edit /opt/local/apache2/conf/httpd.conf
Uncomment the line “Include conf/extra/httpd-vhosts.conf”
Change “User www” to “User _www” and “Group www” to “Group _www”

Make somewhere in your home directory for your projects to live and give apache access rights to it:
cd ~
mkdir Projects
chgrp www Projects/

Add to /etc/hosts:

Add to /opt/local/apache2/conf/extra/httpd-vhosts.conf

Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all

DocumentRoot “/Users/USERNAME/Projects”
ServerName projects.imac.local

Set up PHP

sudo port install php5 +apache2 +pear +mysql5 +postgresql

This tells you to compile the apache php extension as follows:
cd /opt/local/apache2/conf/
sudo /opt/local/apache2/bin/apxs -a -e -n “php5″

Now edit your apache config (/opt/local/apache2/conf/httpd.conf)
Add the lines

AddType application/x-httpd-php .php


DirectoryIndex index.html


DirectoryIndex index.html index.php

Choose a PHP.ini file to use:
cd /opt/local/etc/php5/
sudo mv php.ini-development php.ini

– You’ve now installed PHP5 but there’s loads of other packages you’ll want installed, the ones I require are:
sudo port install php5-mysql +mysqlnd
sudo port install php5-apc php5-curl php5-gd php5-iconv php5-imagick php5-imap php5-mbstring php5-mcrypt php5-openssl php5-xmlrpc php5-xdebug

– Finally you need to add the xdebug config to the php.ini file – note that if you put it in the xdebug.ini file it will get over written when macports upgrades xdebug:
sudo vim /opt/local/etc/php5/php.ini
Add the following lines to the end of the file:

Set up MySQL

I made things slightly harder for myself by installing MySQL 5.5 which is slightly more fiddly.
sudo port install mysql55 +server
sudo port install mysql55-server
These install mysql but for all the tools to be available on the command line you want to:
sudo port select mysql mysql55
this creates symlinks into opt/local/bin for convenience

For some reason the permissions on all the folders that contain mysql are set to be owned by root not the mysql user so run the following command to correct this:
sudo chown -R mysql /opt/local/var/db/mysql55

mysql55-server said to run sudo -u _mysql /opt/local/lib/mysql55/scripts/mysql_install_db5 – this command is wrong it should be:
sudo -u mysql /opt/local/lib/mysql55/bin/mysql_install_db

Set the following properties in php.ini
mysqli.default_socket = /opt/local/var/run/mysql55/mysqld.sock
mysql.default_socket = /opt/local/var/run/mysql55/mysqld.sock

Out the box mac ports doesn’t set the max allowed packet for mysql which means that some apps (Drupal) will fall over very quickly. It also sets no networking which prevents anything from connecting via tcp
To fix this we need to edit the my.cnf file:
sudo vim /opt/local/etc/mysql55/my.cnf

Comment out (#) the line !include /opt/local/etc/mysql55/macports-default.cnf

And add the following lines:
max_allowed_packet = 64M


sudo port install postgresql91-server
sudo port install php5-postgresql

sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb (-p creates all missing folders in the path not just the last one)
sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb
sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb’

start server:
sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/pg_ctl -D /opt/local/var/db/postgresql91/defaultdb -l /tmp/pg.log start’

sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/createuser -s andrewdchancox’
sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/createdb andrewdchancox’

sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/createuser -s moodleuser’
\password moodleuser
give it a password


sudo port install tomcat6

Aliases to start and stop

Because I use my laptop for many things other than development I like to start and stop all these services manually, to do this I’ve edited my bash profile (~/.bash_profile) and added the following aliases:
alias start_mysql=’sudo /opt/local/bin/mysqld_safe &’
alias stop_mysql=’sudo /opt/local/bin/mysqladmin shutdown’
alias start_apache=’sudo /opt/local/apache2/bin/apachectl start’
alias stop_apache=’sudo /opt/local/apache2/bin/apachectl stop’
alias start_tomcat=’sudo tomcatctl start’
alias stop_tomcat=’sudo tomcatctl stop’
alias stop_postgresql=”sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/pg_ctl -D /opt/local/var/db/postgresql91/defaultdb -l /tmp/pg.log stop'”
alias start_postgresql=”sudo su postgres -c ‘/opt/local/lib/postgresql91/bin/pg_ctl -D /opt/local/var/db/postgresql91/defaultdb -l /tmp/pg.log start'”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>