Main Content

Lion Apache MySQL MongoDB PHP (LiAMMP?)

Lion Apache MySQL MongoDB PHP

OSX Preference panes can work like MAMP for starting and stopping Apache and MySQL.

It takes about 30 minutes to run through these steps to config PHP, Apache, MySQL, MongoDB & PHP on Mac OSX 10.7. 

Also included are some quick configs to get XDebug, XHProf, and APC running on your new Lion install.

This is an alternative to installing MAMP.

We will use Xcode and Homebrew to get many things done for this config.

We're going to use the System Preferences to turn on Apache.  Then we'll jump into our terminal and set up Lion for local web development.

Configuring Lion

You can get Xcode for free from the Apple 'App Store' application.  After you have Xcode installed, you'll need to open it up and agree to any licenses/upgrades.

Now go to:  Xcode -> Preferences -> Downloads -> Install Command Line Tools

Once you have the command line tools installed, check to make sure Xcode installed git on your system:

$ git status
 

Run these commands to make sure the directories exist (they should!):

$ sudo mkdir /usr/local/include

$ sudo mkdir /usr/local/bin

$ sudo mkdir /usr/local/lib

$ sudo mkdir -p /usr/local/man/man1
 

Now follow the instructions for installing Homebrew at http://mxcl.github.com/homebrew
 

Test Homebrew by using the brew doctor and make sure you are not having any warnings.  

-- Note:  If you see  "Error: Your Xcode is configured with an invalid path." -- $  sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

$  brew doctor

If you see "your system is raring to brew" you're ready for the next step.

Apache Config
 

Go into System Preferences and turn on web sharing.

Apple - > System Preferences - > Sharing

Click the checkbox next to "Web Sharing"

Go to the apache directory on your Lion machine

$ cd /etc/apache2

Edit the httpd.conf file...

$ sudo vim httpd.conf

Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading # to enable php5
 

Find AllowOverride None within the <Directory "/Library/WebServer/Documents">section and change to AllowOverride All so that .htaccess files will work.
 

Save using :wq!
 

You'll also need to edit your primary user's httpd.conf
 

$  sudo vim users/YOURUSERNAME.conf
 

Example config:

<Directory "/Users/YOURUSERNAME/Sites/">

    Options Indexes MultiViews

    AllowOverride All

    Order allow,deny

    Allow from all

</Directory>
 

Save using :wq!


Restart Apache by unchecking and then checking on again the Web Sharing checkbox in System Preferences -> Sharing  (kinda silly but it works)
 

You can also restart apache in lion this way:
 

$  sudo apachectl graceful
 

MySQL on OSX
 

First, download the 64bit DMG version of MySQ 5.5.x)for OS X 10.6 from mysql.com and install the pkg, the startup item, and the pref pane.

Link: http://www.mysql.com/downloads/mysql/ (look for Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive)

-- Note: I know it says 10.6 but the Snow Leopard version works with Lion.
 

At this point, you should be able to turn on MySQL via the new System Preferences pane and even set it to run on boot. w00t!
 

In order to use MySQL, you must add /usr/local/mysql/bin to your path 
 

$  vim ~/.bash_profile 

or

$  vim ~/.zshrc

(if using ZSH)
 

Add this line:  export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH
 

Set up MySQL root password:

$  mysqladmin -u root password {new-password}

$  mysqladmin -u root -p{new-password} -h localhost password {new-password}

$  mysqladmin -u root -p{new-password} reload
 

Optional: Clear the history file by typing history -c so that {new-password} isn't in plain text on the disk.
 

Now ensure that the mysql.sock file can be found by PHP  (important!):

Make sure that MySQL is running via the preferences pane or the mysql.sock wont show up...

$ sudo mkdir /var/mysql

$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock
 

Test mysql is working:

$ mysql

If you get a mysql> prompt, all is working!  You can now exit mysql by typing 'quit' into the prompt.
 

MongoDB Setup
 

$ brew install mongodb
 

Now create directories required by MongoDB:
 

$ sudo mkdir -p /var/lib/mongodb/

$ sudo mkdir -p /var/log/mongodb/
 

Create a config file for MongoDB

$ sudo vim /etc/mongodb.conf
 

Here is a basic mongodb config file, this is all you'll need for our use case:

# This is an example config file for MongoDB.

# Place it at /etc/mongodb.conf

# Based on a sample provided at

# http://www.mongodb.org/display/DOCS/File+Based+Configuration

dbpath = /var/lib/mongodb

bind_ip = 127.0.0.1

noauth = true
 

Save using :wq!
 

Add a mongodb launch daemon so your mongo server is running when you boot up…

$  sudo vim  /Library/LaunchDaemons/org.mongo.mongod.plist

-- This creates a new file… insert this into the file and save using :wq!

<?xml version="1.0" encoding="UTF-8"?>

      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"

        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

      <plist version="1.0">

      <dict>

        <key>Label</key>

        <string>org.mongodb.mongod</string>

        <key>ProgramArguments</key>

        <array>

          <string>/usr/local/bin/mongod</string>

          <string>run</string>

          <string>--config</string>

          <string>/etc/mongodb.conf</string>

        </array>

        <key>RunAtLoad</key>

        <true/>

        <key>KeepAlive</key>

        <true/>

        <key>WorkingDirectory</key>

        <string>/var/log/mongodb/</string>

        <key>StandardErrorPath</key>

        <string>/var/log/mongodb/output.log</string>

        <key>StandardOutPath</key>

        <string>/var/log/mongodb/output.log</string>

      </dict>

      </plist>
 

You can test the launch daemon by calling it directly:
 

sudo launchctl load /Library/LaunchDaemons/org.mongo.mongod.plist


Or restart your computer and test to see if MongoDB is running automatically.
 

$ mongo
 

If you get a > prompt, all is working.  Type 'exit' into the prompt to quit mongo.
 

PHP Configurations
 

You will need PEAR to finish configuring - and it's always good to have for PHP!

 

For some reason, it is not already set up on Lion, but the install phar file is available...

$ cd /usr/lib/php

$ sudo php install-pear-nozlib.phar
 

$ sudo cp /etc/php.ini.default /etc/php.ini

$ sudo vim /etc/php.ini
 

Change the following:

display_errors = On

html_errors = On

extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20090626"

date.timezone = America/Chicago (or whatever your local timezone is)
 

find the line: ;include_path = ".:/php/includes" 

...and change it to: include_path = ".:/usr/lib/php/pear"
 

Find the line:
 

;zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
 

…and remove the semicolon to enable xdebug
 

Also, use find & replace to change all instances of /var/mysql/mysql.sock to /tmp/mysql.sock
 

Save the php.ini using :wq!
 

Now would be a good time to update PEAR and PECL
 

$ sudo pear channel-update pear.php.net

$ sudo pecl channel-update pecl.php.net

$ sudo pear upgrade-all
 

Autoconf is needed to compile and install things using PECL - so let's add it... 

$  brew install autoconf

Now we're ready to install the MongoDB extension for PHP

$ sudo pecl install mongo
 

This will run for a bit and then, if successful, tell you something like, "You should add "extension=mongo.so" to php.ini" so lets do just that...
 

Edit /etc/php.ini one more tim:
 

$sudo /etc/php.ini
 

 add the mongodb extension by adding a line in the extensions list (near line 976):

extension=mongo.so
 

Save using :wq! as we've been the entire time (Note: I find this beats changing permissions back and forth just to edit php.ini)

Restart apache…

$  sudo apachectl graceful
 

Now you should be able to visit http://localhost/~YOURUSERNAME in any browser.  This is far as you need to go to basically have MAMP running on Lion without all that MAMP clutter...
 

~/sites becomes essentially the same thing as the old /Applications/MAMP/htdocs folder that you're used to.


Now delete index.html and the images folder from your ~/sites directory.
 

Add a file called 'index.php' to ~/sites with the following code:
 

<?php 

phpinfo(); 

?>
 

Now, http://localhost/~YOURUSERNAME should output your PHP config.  If you check for it, you should find MySQL and Mongo are working with PHP.  Nice!

--Note:  setting up vhosts on a Mac is a tutorial itself, but it's easy to do as well. Maybe next time... for now we can deal with the long localhost/~USERNAME urls.
 

We've got PECL and Autoconf... why not add Facebook's profiler...
 

XHProf
 

First, use pecl to install xhprof

$  sudo pecl install xhprof-beta
 

Add "extension=xhprof.so" to /etc/php.ini
 

Also, add the following to the bottom of the php.ini
 

[xhprof]

xhprof.output_dir=/tmp
 

As always, these changes to php.ini require a graceful restart of apache
 

$ sudo apachectl graceful
 

You'll also need to install xhprof PHP library from Facebook:

$  cd ~/sites

$  git clone https://github.com/facebook/xhprof.git
 

Now you should have a working profiler at:

http://localhost/~YOURUSERNAME/xhprof/xhprof_html
 

APC

To add the Alternative PHP Cache, run these commands:

$  curl -O http://freefr.dl.sourceforge.net/project/pcre/pcre/8.02/pcre-8.02.tar.gz

$  tar xvf pcre-8.02.tar.gz

$  sudo cp pcre-8.02/pcre.h /usr/include

$  sudo cp pcre-8.02/pcre.h.generic /usr/include/pcre.h

$  rm -r pcre-8.02

$  export MACOSX_DEPLOYMENT_TARGET=10.7

$  export CFLAGS="-arch x86_64"

$  export CXXFLAGS="-arch x86_64"

$  sudo pecl install apc
 

During the [yes] / [no] prompts, make sure you type the recommended setting in full - hitting Enter defaults to 'yes'


Then add the apc.so reference to your php.ini

$  sudo vim /etc/php.ini
 

Add 'extension=apc.so' near line 977 where we added the mongo.so earlier.
 

Restart apache…

$  sudo apachectl graceful
 

At this point, you should have Apache, MySQL, MongoDB, PHP with Xdebug, XHProf, and APC running on your OSX Lion 10.7 machine.