MAMP with MacPorts

From ThinkyWiki

Revision as of 08:29, 7 December 2010 by (Talk)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Time For a Package System?

As the dependencies of custom-installed components start to pile up -as more PHP and Apache extensions are needed- it becomes harder to maintain /usr/local and keep everything up to date. Fortunately, there are a couple of great package management systems (PMSs) for Mac OS X that take the guesswork out of the process. There may be some flexibility lost (e.g., you probably can't build PHP 5 with static extensions) but the gains in consistency and community support are worth it.

Install All Packages

Installing Apache 2, PHP 5.2, MySQL 5, and all their dependencies takes only one command with MacPorts. Although this does the full installation, it doesn't necessarily prepare the system to use all these facilities as the standard.

sudo port install php52 +apache2 +mysql5-server +sqlite

Initialize MySQL

Next we have to initialize MySQL 5. There are some permission issues on Mac OS X so these instructions are a little different than those given elsewhere. In particular, we run the installation as the root user, but then change ownership on the databases to the 'mysql' user.

sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=root
sudo chown -R mysql /opt/local/var/db/mysql5

To make sure the new MySQL starts at boot time, if this isn't already working:

sudo port install mysql5-server

Set Up PHP 5 Module

To enable the PHP 5 module in Apache 2:

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

To configure the MacPorts version of PHP 5:

cd /opt/local/etc/php5/
cp php.ini-dist php.ini         ;# dev server:
cp php.ini-recommended php.ini  ;# production server
mate php.ini                    ;# or bbedit, pico, joe, emacs...

Migrate Settings & Data

If you already have the built-in Apache configured, you'll need to copy over Virtual Hosts and other settings:

cd /etc/apache2
sudo cp -R other vhosts /opt/local/apache2/conf/

Copy over your existing databases by any means necessary. This example moves and replaces the data completely.

sudo mv /opt/local/var/db/mysql5 /opt/local/var/db/mysql5-hide
sudo mv /usr/local/mysql/data /opt/local/var/db/mysql5

Prepare the PATH Variable

Paths should be in precedence order. Usually you want things in ~/bin to have the highest priority, followed by /usr/local, and finally any package paths.


What's Changed?

The new MySQL and Apache 2 will now automatically start at boot time. If built-in Apache (Web Sharing) is also enabled, there will be conflicts as they both try to handle requests to port 80, so it's important to leave Web Sharing off from now on. It's probably possible to make Mac OS X use the new Apache binary for the Web Sharing option, but it's not needed.

The Apache and MySQL servers are now started and stopped using Launch Services like so:

sudo launchctl load -F /Library/LaunchDaemons/org.macports.apache2.plist 
sudo launchctl unload  /Library/LaunchDaemons/org.macports.apache2.plist
sudo launchctl load -F /Library/LaunchDaemons/org.macports.mysql5.plist
sudo launchctl unload  /Library/LaunchDaemons/org.macports.mysql5.plist
Personal tools