Apache, PHP and MongoDB on Mac OS X 10.6 Snow Leopard

MongoDB (from “humongous”) is a scalable, high-performance, open source, schema-free, document-oriented database.

There’s a lot of buzz brewing about it, so I wanted to give it a try with PHP on my development Mac. The following is how I went about installing Apache, PHP and MongoDB on Snow Leopard. You must have installed the Xcode developer tools (found on the Snow Leopard install DVD) and MacPorts for this to all work.

First, let’s install Apache:

sudo port install apache2

Nice and simple. Now we can move onto PHP:

sudo port install php5 +apache2 +pear

To enable PHP for Apache:

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so

I like to make some alias commands for starting, restarting and stopping Apache:

mate ~/.bashrc

alias apache-start="sudo /opt/local/apache2/bin/apachectl start"
alias apache-restart="sudo /opt/local/apache2/bin/apachectl restart"
alias apache-stop="sudo /opt/local/apache2/bin/apachectl stop"

source ~/.bashrc

Now we can make a few changes to the Apache config so that we can use our Sites directory:

sudo mate /opt/local/apache2/conf/httpd.conf

I’m using Textmate here (mate), but you can use any editor you wish, like nano.

Change the DocumentRoot:

DocumentRoot "/Users/davidwinter/Sites"

Then update the <Directory> path to the updated DocumentRoot path.

<Directory "/Users/davidwinter/Sites">

We also want to enable index.php as a directory index file:

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

And then add the PHP mime type:

<IfModule mime_module>
    ...
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
</IfModule>

Now we need to setup the php.ini file:

sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

sudo mate /opt/local/etc/php5/php.ini

My timezone is for London:

date.timezone = Europe/London

Now we can get onto installing MongoDB (this will install the latest stable version as of time of writing – 1.2.1):

sudo port install mongodb

And then the PHP MongoDB extension:

sudo pecl install mongo

You’ll need to add the extension to the bottom of your php.ini file:

sudo mate /opt/local/etc/php5/php.ini

extension=mongo.so

Now startup Apache using the alias created earlier:

apache-start

If you create a file in your Sites directory, called test.php, and add the following to it:

<?php phpinfo(); ?>

Then visit the page in your browser; http://localhost/test.php

You can then scroll down the page and you should see a section titled ‘mongo’. This means the extension is working!

Now we need to startup MongoDB:

mkdir -p ~/data/db
mongod --dbpath ~/data/db/

Almost there, now a simple MongoDB test in PHP:

<?php
// Connect:
$connection = new Mongo();
// Select database:
$db = $connection->my_db;
// Select collection:
$films = $db->films;

$frwl = array(
    'title' => 'From Russia With Love',
    'year' => 1963,
    'actor' => 'Sean Connery'
);
// Save array to collection:
$films->insert($frwl);

$gf = array(
    'title' => 'Goldfinger',
    'year' => 1964,
    'actor' => 'Sean Connery',
    'girl' => 'Pussy Galore'
);

$films->insert($gf);

// Count documents in collection:
if ($films->count()): ?>
<table>
    <tr>
        <th>Title</th>
        <th>Year</th>
        <th>Actor</th>
        <th>Girl</th>
    </tr>
    <?php foreach ($films->find() as $film): ?>
    <tr>
        <td><?php echo $film['title']; ?></td>
        <td><?php echo $film['year']; ?></td>
        <td><?php echo $film['actor']; ?></td>
        <td><?php echo $film['girl']; ?></td>
    </tr>
    <?php endforeach; ?>
</table>
<?php endif;
?>

Very exciting stuff. More to follow.

2010-01-02 [, , , , , , ]
  • Andy L

    Excellent guide — worked flawlessly.

    Thank you very much!

    I’m looking forward to your next articles on MongoDB with PHP.

  • http://prendreuncafe.com/blog NiKo

    Yes, excellent stuff. Can’t wait for part 2 :)

  • http://bearassbear.blogspot.com/ Brandon

    fantastic guide. i also had no problems at all.

    $frwl has no index ‘girl’

  • http://davidwinter.me.uk David

    That was intended just to show that each object doesn’t have to conform to any schema.

    Though officially, Tatiana Romanova :)

  • Caleb

    Hi there,

    I successfully followed your instructions here, and have apache, php and mongo running, however php seems to have compiled without cURL? Do you have any tips on why that might be?

    Thank you, Caleb

  • tony

    Very nice guide. Kudos.

    However, I ran into one, albeit common in the macports world, problem. In order to avoid any architecture problems when building the mongo extension, you should specifically call the macports version of the pecl binary: sudo /opt/local/bin/pecl install mongo. I was having a problem loading the extension because pecl referenced the binary that comes with Snow Leopard (i.e. /usr/local/bin/pecl). It may only be a problem with the folks running a 32 bit processor under Snow Leopard.

  • http://davidwinter.me.uk David

    @Caleb – you can do sudo port install php5-curl to enable support for curl.

    @tony – be sure to check that the MacPorts /opt paths are set before the rest of your path in order to give it priority. For example:

    export PATH=/opt/local/bin:/opt/local/sbin:$PATH
    
blog comments powered by Disqus