Need An Ultralight WebServer? You Need Mongoose!

This is the third article in a multi-part series that describes how to use Oracle’s VirtualBox to automatically set up Debian Squeeze VMs on a host machine. All the scripts used in these articles are available for download.

The articles are available here:

  1. Headless Debian VirtualBox Setup
  2. Debian Squeeze PXE Netboot For VirtualBox
  3. Need An Ultralight WebServer? You Need Mongoose!
  4. Preseeding a Debian Squeeze Netboot Install

Contents

Introduction

This article is actually a mini-insert in a series that describes my system for easily setting up Debian Squeeze VMs using VirtualBox. In previous articles, I’ve shown how to set up the VM using a simple script instead of using the VirtualBox GUI, and how to set a NAT interface for PXE Booting the VM. Just before we go though the process of booting into a custom Debian installer, we’ll need to have a bare-bones web server that can dish up the contents of a directory.

Wait a minute – web server? That means Apache and hours of futzing around on the command line, right?

Not with Mongoose. It’s about 200K of source code, takes about 5 seconds to download and a minute to compile. You’ll be serving up content in 10 minutes or less, or your money back.

Resources

Getting and Building Mongoose

OK, this really is very simple. Originally this was two sections, but I wanted to get this done in under 5 minutes.

We’ll be setting up under $HOME/tmp/mongoose, downloading the source, extracting, and building it in this little script here:

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#! /bin/dash
#
# -----------------------------------------------------------------------------
# getMongoose.sh
#
# Gets the Mongoose source, build it, and copy the result to $HOME/bin
# -----------------------------------------------------------------------------
# Check if we already have a mongoose executable, abort if we have it
[ -x $HOME/bin/mongoose ] && exit
# -----------------------------------------------------------------------------
# Check if we already have built a mongoose executable, copy if we have it
if [ -x $HOME/tmp/mongoose/mongoose ]; then
cp --parents $HOME/tmp/mongoose/mongoose $HOME/bin
exit
fi
# -----------------------------------------------------------------------------
# Now set up a mongoose build
#
# Create a directory (no error if it already exists)
mkdir -p $HOME/tmp
cd $HOME/tmp
# Check to see if we already have the file, if not, download it
[ -f mongoose-3.0.tgz ] || wget http://mongoose.googlecode.com/files/mongoose-3.0.tgz
# Extract it...
pax -rz -f mongoose-3.0.tgz
# Jump down into the source directory and build it...
cd $HOME/tmp/mongoose
make linux
# Copy the result to $HOME/bin
cp --parents mongoose $HOME/bin

OK, we’re at less than 5 minutes, even if you’re a slow typist. You can just copy the script and paste it into your shell…

Running Mongoose

Running mongoose is even easier than building it. Let’s leave the executable image right where we built it.

In this example shell script, we’ll be running mongoose to serve content from port 8080 and out of the $HOME/.VirtualBox/ISO directory. I’m not going to enable any fancy security or anything. After we’re done with mongoose we’ll close it down to avoid leaving infiltration ports around.

Here’s the script I use to start up mongoose when I need to serve up some temporary content on my internal network (which might just be on the host-only network that VirtualBox uses). Of course, you’ll can change the variables as needed…

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#! /bin/dash
#
# -----------------------------------------------------------------------------
# startMongoose.sh
#
# Starts the Mongoose web server
#
# -----------------------------------------------------------------------------
# Set up a few shell variables to make things cleaner
MONGOOSE=$HOME/bin/mongoose
MONGOOSE_ROOT=$HOME/.VirtualBox/www/
MONGOOSE_LOG=$HOME/tmp/mongoose/accesslog.txt
MONGOOSE_PORT=8080
# -----------------------------------------------------------------------------
# Create a directory (no error if it already exists)
mkdir -p $MONGOOSE_ROOT
mkdir -p $HOME/tmp/mongoose
# -----------------------------------------------------------------------------
# Start Mongoose
$MONGOOSE -r $MONGOOSE_ROOT -p $MONGOOSE_PORT -a $MONGOOSE_LOG

That’s all there is to it – now we can serve up stuff from the $MONGOOSE_HOME directory.