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:
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.
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
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.
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.
RSS Atom.