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/Xcode.app

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:
127.0.0.1 projects.mba.local

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

ServerAdmin webmaster@dummy-host2.example.com
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″ libphp5.so

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

AddType application/x-httpd-php .php

Change

DirectoryIndex index.html

To

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 file that gets loaded by php:
sudo vim /opt/local/var/db/php5/xdebug.ini
Add the following lines to the end of the file:
xdebug.remote_enable=on
xdebug.remote_log=”/var/log/xdebug.log”
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

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

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/scripts/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
pdo_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:
[mysqld]
max_allowed_packet = 64M

Tomcat

TODO
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’

Installing mod_sed on OSX/Macports

I had to use mod_sed for a project that required more exotic functionality than mod_substitute could deliver.
The version of apache that includes mod_sed in core (2.4) is not available via macports, neither is a package containing this one module and I couldn’t find a binary to download so that left me having to compile it myself which wasn’t as scary as I thought it would be.

Go somewhere you can put the files while you work on them:
cd ~/Documents

Download the tarball containing the code:
wget wget http://src.opensolaris.org/source/raw/webstack/apache-modules/sed/mod_sed.tar.gz

Decompress and expand the archive:
tar xzf mod_sed.tar.gz

Go into the folder containing the sourcecode:
cd mod_sed

Now you’ve got the source files accessible we need to compile them. You do this using apxs which is a tool dedicated to compiling apache extensions – you need to make sure that you use the copy of this tool that sits in your macports installation, not the stock OSX version so prepend the path to your mac ports apache bin folder (probably /opt/local/apache2/bin) to the commands:
/opt/local/apache2/bin/apxs -c mod_sed.c sed0.c sed1.c regexp.c
/opt/local/apache2/bin/apxs -i -a mod_sed.la

Once this is done the module should be in the apache modules folder (/opt/local/apache2/modules/mod_sed.so) and enabled in httpd.conf (/opt/local/apache2/confhttpd.conf) by the line:
LoadModule sed_module modules/mod_sed.so

So restart your instance of apache and you can start using the module.