Roll your own MAMP development environment

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.

Except when they don’t.

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.

It pays to know your environment inside and out. Today, it is quite easy to roll your own Apache-MySQL-PHP stack on Windows, Linux, or even Mac OS X.

Getting started

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:

  1. Install dependencies needed for PHP
  2. Compile PHP from scratch
  3. Install MySQL
  4. Configure PHP, MySQL, and Apache to run virtual hosts
Note: OS X Lion and higher already have Apache installed.

1. Install PHP dependencies

  1. 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.
  2. 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
  3. Install Homebrew and run brew doctor to verify that it is working correctly.
  4. 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
  5. 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

  1. 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
  2. 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.

  3. Compile and install the PHP binary:
    make
    sudo make install
  4. Enable mod_php and virtual hosts in your Apache configuration by adding the following line to your /etc/apache2/httpd.conf file:
    LoadModule php5_module /usr/local/Cellar/php54/5.4.9/libexec/apache2/libphp5.so
  5. Restart apache:
  6. sudo apachectl restart

3. Install MySQL

  1. 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.
  2. Create your MySQL configuration file:
    sudo cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
  3. Edit your ./profile file and add the /usr/local/mysql/bin directory to the path. You will need to restart Terminal for this to take effect.
    export PATH=/usr/local/mysql/bin:/usr/local/opt/php54/bin:$PATH
  4. Restart MySQL using the System Preferences MySQL pane.
  5. 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:

  1. Add your virtual host domain name to the /etc/hosts file (requires Administrator or sudo access):
    127.0.0.1 helloworld.dev
  2. Add a virtual host block to your /private/etc/apache2/extra/httpd-vhosts.conf file (requires Administrator or sudo access):
    <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>
  3. Restart Apache:
    sudo apachectl restart
  4. Add an index.php file in your virtual host document root, ~/Sites/helloworld.dev/:
    <?php echo 'Hello, world!';
  5. Browse to http://helloworld.dev/ in your web browser.

Conclusion

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?

4 thoughts on “Roll your own MAMP development environment”

  1. I think you were planning to add examples here:
    “Enable mod_php and virtual hosts in your Apache configuration by adding the following lines to your /etc/apache2/httpd.conf file:
    Configure the MySQL socket settings in your /etc/php.ini file:
    Restart apache:”

    There seem to be some lines missing or something has gone wrong with a syntax highlighter or so. Today I had the nasty surprises with MAMP Pro. I like the convenience of adding vhosts by just pushing buttons, but I also wanted imagick, which comes as a PECL extension. Now at the end of the day, I have compiled php 5.4 but MAMP won’t start Apache with it, although it displays in the menu. Maybe there is still some kind of permission problem there. And, although I now have a PECL command, pecl still won’t install imagick… Anyway, thanks for sharing your thoughts.

    By the way, usually windows is worse, but today, turning on imagick in WAMP (with ZendSever) was just literaly pushing two buttons (turn on imagick and restart php). So I might consider ZendServer for the mac.

  2. Jonathan, fantastic post. Thank you so much. By the way, to compile the latest PHP 5.5.7 source I also had to install:

    1. icu-config from http://site.icu-project.org/download)
    2. postgresql (http://www.postgresql.org/download) but I used brew postgresql
    3. sql.h and related headers are missing in Mac OS X Mavericks, so this post helped: http://stackoverflow.com/questions/20074620/installing-pyodbc-fails-on-osx-10-9-mavericks. I followed MFB’s answer, and copied over includes from IODBC, but also had to copy over iodbcext.h.

    Also, make test is optional but a good idea. It does take a very, very, very long time to run.

  3. This is my go-to for setting up my local environment and I keep forgetting to uncomment this line in httpd.conf to use virtualhost:

    Include /private/etc/apache2/extra/httpd-vhosts.conf

    I also keep reminding myself to set the documentroot in the same file from:

    DocumentRoot “/Library/WebServer/Documents”

    to

    DocumentRoot “/Users/yourusername/Sites”

Leave a Reply

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