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.
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: <pre>sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer ln -s /opt/X11 /usr/X11</pre>
- Install Homebrew and run
brew doctorto verify that it is working correctly.
Using Homebrew, install the following libraries: <pre>brew install libpng brew install libjpeg brew install gd brew install pcre brew install libxml2 brew install mcrypt brew install icu4c brew install wget</pre>
- Install the IMAP-2007f library: <pre>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 ..</pre>
2. Compile PHP
Download and extract the latest PHP 5.4 source tarball from http://php.net/downloads.php: <pre>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</pre>
Configure the makefile: <pre>./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</pre>
Verify that this completed without errors before proceeding. If you have installed the dependencies listed in the previous step, there should be none.</li> * Compile and install the PHP binary: <pre>make sudo make install</pre> * Enable `mod_php` and virtual hosts in your Apache configuration by adding the following line to your `/etc/apache2/httpd.conf` file: <pre>LoadModule php5_module /usr/local/Cellar/php54/5.4.9/libexec/apache2/libphp5.so</pre> * Restart apache: * <pre>sudo apachectl restart</pre></ol> ## 3. Install MySQL 1. Download and install the latest <a href="http://www.mysql.com/downloads/mysql/" target="_blank">MySQL 5.5 Community Server</a> 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: <pre>sudo cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf</pre> 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. <pre>export PATH=/usr/local/mysql/bin:/usr/local/opt/php54/bin:$PATH</pre> 4. Restart MySQL using the System Preferences MySQL pane. 5. Verify that MySQL is running on localhost: <pre>mysql -u root</pre> It is recommended that you <a href="http://www.cyberciti.biz/faq/mysql-change-root-password/" target="_blank">set a root password</a> at this point.</li> </ol> ## 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): <pre>127.0.0.1 helloworld.dev</pre> 2. Add a virtual host block to your `/private/etc/apache2/extra/httpd-vhosts.conf` file (requires Administrator or `sudo` access): <pre><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></pre> 3. Restart Apache: <pre>sudo apachectl restart</pre> 4. Add an `index.php` file in your virtual host document root, `~/Sites/helloworld.dev/`: <pre><?php echo 'Hello, world!';</pre> 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?