Setup MAMP (Apache,MySQL,PHP) and MongoDB on Mac

Published -  2012-06-26

My setup for my Macbook for developing PHP-Apps using local MongoDB

1. MAMP-Stack (Apache, PHP, MySQL)

To get PHP up and running I use the BitNami MAMP-Stack. Just download by http://bitnami.org/stack/mampstack. Setup is quite easy and intuitive.

2. Install HomeBrew

To setup Mongo I use the HomeBrew System on Mac. Homebrew Homebrew allows easy installation for many projects and works a bit like “apt-get” (for those who are familiar with Debian/Ubuntu).

If you already have homebrew installed go to step 3. Installation seems a bit complicated but is not. Please read the requirements ( make shure you have Xcode etc. installed) https://github.com/mxcl/homebrew/wiki/installation

$ /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

then call brew update to get the latest package descriptions:

$ brew update

3. Install MongoDB

$ brew install mongodb

==> Downloading http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.6.tgz
######################################################################## 100,0%
==> Caveats

If this is your first install, automatically load on login with:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mongodb/2.0.6-x86_64/homebrew.mxcl.mongodb.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

If this is an upgrade and you already have the homebrew.mxcl.mongodb.plist loaded:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
cp /usr/local/Cellar/mongodb/2.0.6-x86_64/homebrew.mxcl.mongodb.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Or start it manually:

mongod run --config /usr/local/etc/mongod.conf
The launchctl plist above expects the config file to be at /usr/local/etc/mongod.conf.
==> Summary
/usr/local/Cellar/mongodb/2.0.6-x86_64: 17 files, 122M, built in 13 seconds

4. Setup PHP Extension for MongoDB:

use the “pecl” provided by mampstack to install mongo:

$ cd /Applications/mampstack-5.3.14-0/
$ php/bin/pecl install mongo

WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading mongo-1.2.10.tgz ...
Starting to download mongo-1.2.10.tgz (86,463 bytes)
.....................done: 86,463 bytes
36 source files, building
WARNING: php_bin /Applications/mampstack-5.3.14-0/php/bin/php appears to have a suffix /bin/php, but config variable php_suffix does not match
running: phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
building in /Users/cs/pear/temp/pear-build-cssAeH9n/mongo-1.2.10
running: /Users/cs/pear/temp/mongo/configure

[lots of output deleted]

Build complete.
Don't forget to run 'make test'.

running: make INSTALL_ROOT="/Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10" install
Installing shared extensions:     /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications/mampstack-5.3.14-0/php/lib/php/extensions/
running: find "/Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10" | xargs ls -dils
41035412   0 drwxr-xr-x  3 cs  staff     102 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10
41036083   0 drwxr-xr-x  3 cs  staff     102 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications
41036084   0 drwxr-xr-x  3 cs  staff     102 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications/mampstack-5.3.14-0
41036085   0 drwxr-xr-x  3 cs  staff     102 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications/mampstack-5.3.14-0/php
41036086   0 drwxr-xr-x  3 cs  staff     102 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications/mampstack-5.3.14-0/php/lib
41036087   0 drwxr-xr-x  3 cs  staff     102 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications/mampstack-5.3.14-0/php/lib/php
41036088   0 drwxr-xr-x  3 cs  staff     102 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications/mampstack-5.3.14-0/php/lib/php/extensions
41036089 912 -rwxr-xr-x  1 cs  staff  463820 26 Jun 11:53 /Users/cs/pear/temp/pear-build-cssAeH9n/install-mongo-1.2.10/Applications/mampstack-5.3.14-0/php/lib/php/extensions/mongo.so

Build process completed successfully
Installing '/Applications/mampstack-5.3.14-0/php/lib/php/extensions/mongo.so'
install ok: channel://pecl.php.net/mongo-1.2.10
configuration option "php_ini" is not set to php.ini location

You should add "extension=mongo.so" to php.ini, Then edit php.ini :

$ cd /Applications/mampstack-5.3.14-0/
$ vim php/etc/php.ini

and add mongo extension:

; MongoDB
extension=mongo.so

then restart mamp and verify mongodb is available:

$ cd /Applications/mampstack-5.3.14-0/
$ ./ctlscript.sh restart
$ php/bin/php -m|grep mongo
mongo

5. Start a local mongo server and create admin user:

Start the MongoDB Server (I prefer to run in in Terminal and not as a Service)

$ mkdir ~/.mongodb
$ mongod --dbpath ~/.mongodb
Tue Jun 26 11:38:29 [initandlisten] MongoDB starting : pid=5309 port=27017 dbpath=/Users/cs/.mongodb 64-bit host=Carstens-MacBook-Pro-i7-2.local
Tue Jun 26 11:38:29 [initandlisten] db version v2.0.6, pdfile version 4.5
Tue Jun 26 11:38:29 [initandlisten] git version: e1c0cbc25863f6356aa4e31375add7bb49fb05bc
Tue Jun 26 11:38:29 [initandlisten] build info: Darwin erh2.10gen.cc 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40
Tue Jun 26 11:38:29 [initandlisten] options: { dbpath: "/Users/cs/.mongodb" }
Tue Jun 26 11:38:29 [initandlisten] journal dir=/Users/cs/.mongodb/journal
Tue Jun 26 11:38:29 [initandlisten] recover : no journal files present, no recovery needed
Tue Jun 26 11:38:29 [websvr] admin web console waiting for connections on port 28017
Tue Jun 26 11:38:29 [initandlisten] waiting for connections on port 27017

Then go to other terminal and connect to mongo and create root user:

$ mongo
MongoDB shell version: 2.0.6
connecting to: test
> db.addUser('root','root')
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
{
     "user" : "root",
     "readOnly" : false,
     "pwd" : "2a8025f0885adad5a8ce0044070032b3",
     "_id" : ObjectId("4fe9833cdd79a0bc22c8d5ad")
}
> db.auth('root','root')
1

Test if it works:

> db.test.save({'field1':'the first field','field2':'the second field'})
> db.test.find()
{ "_id" : ObjectId("4fe9839ddd79a0bc22c8d5ae"), "field1" : "the first field", "field2" : "the second field" }
>

6. Install RockMongo

RockMongo is a webfrontend like phpMyAdmin and easy to install. Download from http://code.google.com/p/rock-php/wiki/rock_mongo and copy to your web server’s htdocs directory

$ cp -a ~/Downloads/rockmongo /Applications/mampstack-5.3.14-0/apache2/htdocs/

Note: make shure in config.php the authentication is set to “true”:

$ vim /Applications/mampstack-5.3.14-0/apache2/htdocs/rockmongo/config.php

...
$MONGO["servers"][$i]["mongo_auth"] = true;//enable mongo authentication?
...

open RockMongo:

http://localhost:8080/rockmongo

and login with “root” / “root” (like created before)

Thats all.