June 16, 2010 at 7:20 pm by TwiRp MongoDB and PHP on Webfaction

So, this summer I decided I was going to try to learn some new things and update myself to some of the newer stuff out there.

So PHP 5 added a whole slew of new features to PHP, but I’ve been stuck in PHP 4, so I figured I’d learn some of that.  I’ve also been stuck to relational databases (and that’s usually what they teach at school), but I’ve started to see some of the sites I know and love switching to non-relational databases.  So that was also something I wanted to learn how to work with.

I went online to search, and the three major contenders seem to be Cassandra, CouchDB, and MongoDB.  I decided to go with MongoDB since it seemed like the easiest to use and setup, and I liked the idea of document-oriented storage.  CouchDB can be ran on webFaction as well, but I’m not too sure about Cassandra (I’m guessing you can with effort).

So here’s my guide on getting it setup and ready to run.

So installing MongoDB on Webfaction:

  1. Create a new custom application listening on port called mongodb
  2. Create it and take note of the port
  3. Login to your account via SSH
  4. Switch to the directory of the mongodb app you just created
    cd ~/webapps/mongodb
  5. Download the latest version of the 32-bit version of MongoDB (1.4.3 as of writing)
    wget http://downloads.mongodb.org/linux/mongodb-linux-i686-1.4.3.tgz
  6. Extract the files from the archive
    tar -xf mongodb-linux-i686-1.4.3.tgz
  7. Create a data directory for your MongoDB installation
    mkdir data
  8. Start up MongoDB
    /home/[user]/webapps/mongodb/mongodb-linux-i686-1.4.3/bin/mongod ‒‒dbpath /home/[user]/webapps/mongodb/data/ ‒‒port [port]

Doing that will launch MongoDB in the terminal.  To have MongoDB run as a background process and restart should it quit can be done with a few scripts.  My way isn’t probably the most elegant way to do it.  If mongoDB doesn’t exit cleanly, then you have to delete the lock, run repair, and restart the daemon, so there are 3 parts to my way and a dummy file, but you can probably easily merge it all into one file (I’m just too interested in playing with PHP and MongoDB to do so yet).

This is mongo.sh, it checks if the mongo daemon (mongod) is running, and it calls the appropriate action.

#!/bin/bash
# check if mongod is running
ps -u [user] | grep -v grep | grep mongod
# if not found – equals to 1, start it
if [ $? -eq 1 ]
then
/home/[user]/webapps/mongodb/startmongo.sh
else
/home/[user]/webapps/mongodb/runningmongo.sh
fi

This is startmongo.sh, there’s a dummy file called “isnt_running.txt”  If it exists when startmongo.sh is ran, that means that mongod didn’t exit cleanly.  If it didn’t exit cleanly, we have to delete mongod.lock and run repair on the database.  Otherwise, we just try to start up mongod.

#!/usr/bin/env php
<?php
// Check if MongoDB did not exit cleanly
if(file_exists(“/home/[user]/webapps/mongodb/isnt_running.txt”)){
unlink(“/home/[user]/webapps/mongodb/data/mongod.lock”);
exec(“/home/[user]/webapps/mongodb/mongodb-1.4.3/bin/mongod ‒‒dbpath /home/[user]/webapps/mongodb/data ‒‒port [port] ‒‒auth ‒‒repair”);
exec(“nohup /home/[user]/webapps/mongodb/mongodb-1.4.3/bin/mongod ‒‒dbpath /home/[user]/webapps/mongodb/data ‒‒port [port] ‒‒auth”);
}else{
exec(“nohup /home/[user]/webapps/mongodb/mongodb-1.4.3/bin/mongod ‒‒dbpath /home/emokid/webapps/mongodb/data ‒‒port [port] ‒‒auth”);
$fp = fopen(“/home/[user]/webapps/mongodb/isnt_running.txt”, “w+”);
fwrite($fp, “Starting up”);
fclose($fp);
}
?>

This is runningmongo.sh, it just deletes our dummy file letting us know that mongod is running.

#!/usr/bin/env php
<?php
// Check if MongoDB did not exit cleanly
if(file_exists(“/home/emokid/webapps/mongodb/isnt_running.txt”)){
unlink(“/home/emokid/webapps/mongodb/isnt_running.txt”);
}
?>

I just add mongo.sh to the crontab.  Mine runs every 20 minutes, but you can make it run sooner because if it does run every 20 minutes, that mean that the database could be down for 40 minutes.  Make sure to change [user] and [port] to your username and the port number you were given.

crontab

*/20 * * * * /home/[user]/webapps/mongodb/mongo.sh > /dev/null 2>&1

The PHP Stack

In order to run PHP with the mongo drivers, you need your own installation of Apache and PHP.  WebFaction makes this somewhat easy to do.

  1. Create a new custom application listening on port and call it phpstack
  2. Take note of the port.
  3. SSH onto the server and go to the phpstack webapp directory
    cd ~/webapps/phpstack
  4. Download the PHPStack setup script (this is the one for PHP 5.3.1).
    wget http://wiki.webfaction.com/attachment/wiki/MiscellaneousFiles/build_php_worker_531.sh?format=raw
  5. Rename the script to build_php_worker_531.sh
    mv build_php_worker_531.sh\?format\=raw build_php_worker_531.sh
  6. Make sure the script is executable.
    chmod +x build_php_worker_531.sh
  7. Run the install script and specify the path to the phpstack, and the port number
    ./build_php_worker_531.sh /home/[user]/webapps/phpstack [port]
  8. We have to install Pear twice in order to get PECL.
    1. Download the go-pear.php installer script
      wget http://pear.php.net/go-pear
    2. Rename it to go-pear.php
      mv go-pear go-pear.php
    3. Run it with the php_fcgi version of php
      /home/[user]/webapps/phpstack/php_fcgi/bin/php go-pear.php
    4. When it asks you about the paths, set $prefix to:
      /home/[user]/webapps/phpstack/php_fcgi
    5. Then run the installer.  Repeat the process for php_rawcgi
    6. For each installation (php_fcgi and php_rawcgi) run
      /home/[user]/webapps/phpstack/php_fcgi/bin/pecl install mongo
    7. Then in each php.ini (php_fcgi/lib/php.ini and php_rawcgi/lib/php.ini) add
      extension=”mongo.so”

To keep things easy, you can place all of your website stuff inside of the phpstack/htdocs, or you can edit the httpd.conf to point towards one of your other webapps.

Now you might want to secure your MongoDB a bit, and read up on how to use it with PHP.



Post a Comment

*
* (hidden)

  Layout composed by a TwiRp.  Brushes from blindingLight.  Menu from andrewSellick.  Powered by wordPress.
  (close) (open)   RSS  Atom.