Pre-packaged MAMP, LAMP, and WAMP stacks have been common on developer’s computers for years. Such packages are convenient because they provide a single-step install process, with all components in the server stack preconfigured to work together, and off you go.
I’ve learned from experience that these packages have ways of making you pay for the convenience you enjoyed up front. If you have ever needed to:
- Install a PHP extension that wasn’t already provided in your stack
- Run a specific version of PHP or MySQL
- Install PEAR packages
- Install SSL certificates
- Run command-line PHP scripts
…you may have encountered some ugly, time-wasting surprises along the way.
This tutorial will show you how to roll your own MAMP stack with Mac OS X 10.7+ (Lion), Apache 2, MySQL 5.5, and PHP 5.4. If you wish to use different versions, a similar procedure should work but the specifics will probably vary.
For reference, I have provided a reference script which was the basis for this blog post: build-mamp-osx.sh.
We will perform the following steps:
- Install dependencies needed for PHP
- Compile PHP from scratch
- Install MySQL
- Configure PHP, MySQL, and Apache to run virtual hosts
1. Install PHP dependencies
- Verify that you have the latest Apple XCode from the App Store. If you do not already have XCode, you may need to upgrade to the latest OS X.
- Download and install XQuartz from http://xquartz.macosforge.org/. After installing XQuartz, run the following:
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer ln -s /opt/X11 /usr/X11
- Install Homebrew and run
brew doctorto verify that it is working correctly.
- Using Homebrew, install the following libraries:
brew install libpng brew install libjpeg brew install gd brew install pcre brew install libxml2 brew install mcrypt brew install icu4c brew install wget
- Install the IMAP-2007f library:
wget ftp://ftp.cac.washington.edu/imap/imap-2007f.tar.gz tar zxvf imap-2007f.tar.gz cd imap-2007f make osx EXTRACFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" sudo cp c-client/*.h /usr/local/include/ sudo cp c-client/*.c /usr/local/lib sudo cp c-client/c-client.a /usr/local/lib/libc-client.a cd ..
2. Compile PHP
- Download and extract the latest PHP 5.4 source tarball from http://php.net/downloads.php:
wget http://us3.php.net/get/php-5.4.9.tar.gz/from/this/mirror tar xzvf php-5.4.9.tar.gz cd php-5.4.9
- Configure the makefile:
./configure \ --prefix=/usr \ --mandir=/usr/share/man \ --infodir=/usr/share/info \ --sysconfdir=/private/etc \ --with-apxs2=/usr/sbin/apxs \ --enable-cli \ --with-config-file-path=/etc \ --with-libxml-dir=/usr \ --with-openssl=/usr \ --with-kerberos=/usr \ --with-zlib=/usr \ --enable-bcmath \ --with-bz2=/usr \ --enable-calendar \ --with-curl=/usr \ --enable-dba \ --enable-exif \ --enable-ftp \ --with-gd \ --enable-gd-native-ttf \ --with-icu-dir=/usr/local \ --with-iodbc=/usr \ --with-ldap=/usr \ --with-ldap-sasl=/usr \ --with-libedit=/usr \ --enable-mbstring \ --enable-mbregex \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --without-pear \ --with-pdo-mysql=mysqlnd \ --with-mysql-sock=/var/mysql/mysql.sock \ --with-readline=/usr \ --enable-shmop \ --with-snmp=/usr \ --enable-soap \ --enable-pcntl \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --with-tidy \ --enable-wddx \ --with-xmlrpc \ --with-iconv-dir=/usr \ --with-xsl=/usr \ --enable-zip \ --with-imap=/usr/local/imap-2007 \ --with-kerberos \ --with-imap-ssl \ --enable-intl \ --with-pcre-regex \ --with-pgsql=/usr \ --with-pdo-pgsql=/usr \ --with-freetype-dir=/usr/X11 \ --with-jpeg-dir=/usr \ --with-png-dir=/usr/X11
Verify that this completed without errors before proceeding. If you have installed the dependencies listed in the previous step, there should be none.
- Compile and install the PHP binary:
make sudo make install
mod_phpand virtual hosts in your Apache configuration by adding the following line to your
LoadModule php5_module /usr/local/Cellar/php54/5.4.9/libexec/apache2/libphp5.so
- Restart apache:
sudo apachectl restart
3. Install MySQL
- Download and install the latest MySQL 5.5 Community Server binary (DMG package). Within the DMG archive, be sure to install all three components: the MySQL package, the MySQL.prefPane, and the MySQLStartupItem package.
- Create your MySQL configuration file:
sudo cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
- Edit your
./profilefile and add the
/usr/local/mysql/bindirectory to the path. You will need to restart Terminal for this to take effect.
- Restart MySQL using the System Preferences MySQL pane.
- Verify that MySQL is running on localhost:
mysql -u root
It is recommended that you set a root password at this point.
Adding Virtual Hosts
Once your MAMP stack in set up, here is how to create virtual hosts:
- Add your virtual host domain name to the
/etc/hostsfile (requires Administrator or
- Add a virtual host block to your
/private/etc/apache2/extra/httpd-vhosts.conffile (requires Administrator or
<VirtualHost *:80> ServerName helloworld.dev DocumentRoot "/Users/jhill/Sites/helloworld.dev" <Directory "/Users/jhill/Sites/helloworld.dev"> Options Includes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
- Restart Apache:
sudo apachectl restart
- Add an
index.phpfile in your virtual host document root,
<?php echo 'Hello, world!';
- Browse to http://helloworld.dev/ in your web browser.
Building your own MAMP stack with Mac OS X 10.7+, MySQL 5.5, and PHP 5.4 is easier than you think, and offers the advantages of total flexibility, less “magic”, better reliability, and a greater understanding of where things are and how to fix them if they break.
Did you try it? How did it go?