@cvee

Configuring MongoDB on Ubuntu

This tutorial describes how to download and install the lastest stable version of MongoDB on an Ubuntu system.

The following instructions cover installing and upgrading MongoDB using two different methods. The first and recommended method uses apt-get to install the MongoDB package provided by the 10gen repository. If you require more control over the location of the binaries, use the instructions in “Generic Linux Binaries” to install the generic Linux binaries provided at http://www.mongodb.org/downloads.

10gen Repository

Add 10gen Repository

10gen, the creator of MongoDB, publishes a MongoDB package for Ubuntu. To access the package via apt-get add the 10gen repository to /etc/apt/sources.list.

sudo sh -c "echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' >> /etc/apt/sources.list.d/downloads-distro.mongodb.org.list"

Next, import the public GPG key used to sign packages contained in the 10gen repository.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

Run apt-get to retrieve the list of package files provided by the the new source respository.

sudo apt-get update

Install mongodb-10gen

Installation via the 10gen repository requires one command to download, install and automatically start MongoDB.

sudo apt-get -y install mongodb-10gen

Installation Script

So you’ve made this far through the tutorial and are thinking, “That’s a lot of steps!”. To simplify things I’ve created a shell script to automatically install MongoDB. Download and run the shell script using the following command:

wget -q -O - https://raw.github.com/cvee/ubuntu-env/master/install-mongodb.sh | sudo sh

Generic Linux Binaries

Installation

cd ~/
git clone git://github.com/cvee/ubuntu-env.git
sudo adduser --system --group --home /opt/mongodb --shell /bin/false --no-create-home mongodb
sudo mkdir /opt/mongodb
sudo mkdir /etc/mongodb
sudo mkdir /var/opt/mongodb
sudo mkdir /var/opt/mongodb/db
sudo mkdir /var/opt/mongodb/log
sudo chown -R mongodb:mongodb /var/opt/mongodb
sudo cp ~/ubuntu-env/etc/mongodb/mongodb.conf /etc/mongodb/mongodb.conf
sudo cp ~/ubuntu-env/etc/init/mongodb.conf /etc/init/mongodb.conf
cd ~/
curl -O http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.1.tgz
sudo tar -C /opt/mongodb -xzf mongodb-linux-x86_64-2.0.1.tgz
cd /opt/mongodb
sudo mv mongodb-linux-x86_64-2.0.1 2.0.1
sudo ln -s 2.0.1 latest
sudo service mongodb start

Upgrade

sudo service mongodb stop
cd ~/
curl -O http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.1.tgz
sudo tar -C /opt/mongodb -xzf mongodb-linux-x86_64-2.0.1.tgz
cd /opt/mongodb
sudo mv mongodb-linux-x86_64-2.0.1 2.0.1
sudo unlink latest && sudo ln -s 2.0.1 latest
sudo service mongodb start

More Information

Configuring CouchDB on Ubuntu

To ensure an Ubuntu system is using the lastest stable version of CouchDB currently requires building from source. This tutorial describes how to download, build, install and configure the lastest stable version of CouchDB on an Ubuntu system.

Install Dependencies

Install the packages needed to download, build and install CouchDB.

sudo apt-get -y install build-essential autoconf automake libtool erlang libicu-dev libmozjs-dev libcurl4-openssl-dev

Install CouchDB

Get the Source

Download and extract the CouchDB 1.1.1 source code.

curl -O http://www.apache.org/dyn/closer.cgi?path=/couchdb/1.1.1/apache-couchdb-1.1.1.tar.gz
tar -C /tmp -xzf apache-couchdb-1.1.1.tar.gz
cd /tmp/apache-couchdb-1.1.1

OR

Clone the latest CouchDB source code:

git clone http://git-wip-us.apache.org/repos/asf/couchdb.git /tmp/couchdb

Checkout the 1.2.x branch:

cd /tmp/couchdb
git checkout --track -b 1.2.x origin/1.2.x

Run bootstrap to create the configure script:

./bootstrap

Build and Install

Build the source and install the compiled binaries.

./configure
make && sudo make install

Create a user account dedicated for CouchDB:

sudo adduser --system --home /usr/local/var/lib/couchdb --no-create-home --shell /bin/bash --group --gecos "CouchDB Administrator" couchdb

Change the ownership of the CouchDB directories:

sudo chown -R couchdb:couchdb /usr/local/etc/couchdb
sudo chown -R couchdb:couchdb /usr/local/var/lib/couchdb
sudo chown -R couchdb:couchdb /usr/local/var/log/couchdb
sudo chown -R couchdb:couchdb /usr/local/var/run/couchdb

Change the permission of the CouchDB directories:

sudo chmod 0770 /usr/local/etc/couchdb
sudo chmod 0770 /usr/local/var/lib/couchdb
sudo chmod 0770 /usr/local/var/log/couchdb
sudo chmod 0770 /usr/local/var/run/couchdb

Create a administrator, edit /usr/local/etc/couchdb/local.ini and add the section:

[admins]
root = <password>

To start the daemon on boot, copy the init script to /etc/init.d

sudo cp /usr/local/etc/init.d/couchdb /etc/init.d
sudo update-rc.d couchdb defaults

Manually start CouchDB:

sudo service couchdb start

More Information

Running node.js Apps With Upstart

After developing my first node.js app, I realized the need to run it as a system service. Specifically I wanted to have the app start during boot, run in the background and, if the process died unexpectedly, have it respawn automatically. While researching how to do this on an Ubuntu system I came across Upstart.

What is Upstart?

The official Upstart website defines Upstart as “an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.” Through the creation of custom configuration files, called job definitions, we are able to “daemonize” a node.js app by assigning it behaviors common to system services.

Upstart Job Definitions

Upstart Job Definitions are are defined in files located in /etc/init. The name of a job is the definition filename without the .conf extension. The following is an example job definition for a node.js app:

# node-upstart - Example Upstart job definition for a node.js based app
#

description     "Example Upstart job definition for a node.js based app"
author          "Chris Verwymeren"

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [06]

# Prepare the environment
#   Create directories for logging and process management
#   Change ownership to the user running the process
pre-start script
    mkdir -p /var/opt/node
    mkdir -p /var/opt/node/log
    mkdir -p /var/opt/node/run
    chown -R node:node /var/opt/node
end script

# If the process quits unexpectadly trigger a respawn
respawn

# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile \
  --pidfile /var/opt/node/run/node-upstart.pid \
  --exec /home/node/local/node/bin/node -- \
  /home/node/apps/node-upstart.js >> \
  /var/opt/node/log/node-upstart.log 2>&1

The first section provides information about the job definition. In this instance a description of the app is provided along with the name and email address of the author who created the job definition. Similar to most other configuration files, comments are initiated by placing a # symbol at the beginning of a line of text.

# node-upstart - Example Upstart job definition for a node.js based app
#

description     "Example Upstart job definition for a node.js based app"
author          "Chris Verwymeren <cvee@me.com>"

The next section provides information on when to start and stop the job. In this example, the process will be automatically started on the various Multi-User Modes (2-5) and stopped on Halt (0) or Reboot (6).

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [06]

pre-start allows the environment to be prepared before a job is started. In this example, the directories for logging and process management are created and assigned ownership.

# Prepare the environment
#   Create directories for logging and process management
#   Change ownership to the user running the process
pre-start script
    mkdir -p /var/opt/node
    mkdir -p /var/opt/node/log
    mkdir -p /var/opt/node/run
    chown -R node:node /var/opt/node
end script

Using the respawn command will restart the job if it quits unexpectadly.

# If the process quits unexpectadly trigger a respawn
respawn

The last section handles the details of the job command which is run during the “start” action.

# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile \
  --pidfile /var/opt/node/run/node-upstart.pid \
  --exec /home/node/local/node/bin/node -- \
  /home/node/apps/node-upstart.js >> \
  /var/opt/node/log/node-upstart.log 2>&1

In the example above, the job command is run under the node user (--chuid node), a process id file is created at a specific location (–make-pidfile –pidfile /var/opt/node/run/node-upstart.pid) and the node.js app is executed with stdout and stderr logged to a file (–exec /home/node/local/node/bin/node – /home/node/apps/node-upstart.js >> /var/opt/node/log/node-upstart.log 2>&1).

In the above example, the name of the job definition file is node-upstart.conf therefore the name of the job is node-upstart. When using command line utilites to interact with the job, use the job name.

After creating a job definition save the file to /etc/init. Then, use initctl to verify the job has been recognized by Upstart.

$ initctl list | grep node-upstart
node-upstart stop/waiting

Manual Job Control

As an added bonus, Upstart allows for services to be controlled manually using the commands start, stop and status. For example, an administrator can start, stop and determine the status of the above job definition using the following commands:

$ sudo start node-upstart
node-upstart start/running, process 3410
$ sudo status node-upstart
node-upstart start/running, process 3410
$ sudo stop node-upstart
node-upstart stop/waiting
$ sudo status node-upstart
node-upstart stop/waiting

More Information

Configuring node.js and Npm on Ubuntu

The rapid pace of development on node.js and npm has resulted in frequent releases and huge improvements. As an unfortunate side-effect, the official Ubuntu provided packages quickly become outdated. To ensure an Ubuntu system is using the lastest stable (or unstable) versions of node.js and npm currently requires an extra bit of work. This tutorial describes how to install the lastest stable versions of node.js and npm on an Ubuntu system.

Install Developer Packages

Install the packages needed to download, build and install node.js and npm.

sudo apt-get -y install build-essential libssl-dev openssh-server pkg-config curl git-core

node.js Installation

curl -L http://nodejs.org/dist/v0.6.11/node-v0.6.11.tar.gz | tar -C /tmp -xzf -
cd /tmp/node-v0.6.11
./configure
make -j2 && sudo make install
cd ~/
rm -rf /tmp/node-v0.6.11

npm Installation

As of node.js 0.6.3, npm is included and installed on make install. The following installation instructions are included for historical purposes only.

curl http://npmjs.org/install.sh | sudo sh

Create a Dedicated User Account

To avoid the security concerns caused by running applications as root, you may want to create a user responsible for running node apps.

sudo adduser --home /home/node --shell /bin/bash --disabled-password node

Become the user and generate an SSH keypair:

sudo su - node
ssh-keygen -b 4096 -t rsa

For security, the user account is created with password logins disabled. In order to login via SSH, create or edit the file ~/.ssh/authorized_keys, copy in your public key, save ~/.ssh/authorized_keys and assign correct access permissions.

chmod 600 ~/.ssh/authorized_keys

Installation Script

To automate installation I’ve collected the above steps into a shell script. To install node.js and npm using this method download and run the shell script using the following command:

wget -q -O - https://raw.github.com/cvee/ubuntu-env/master/install-node.sh | sudo sh

More Information