BSD DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Lightweight Web Serving with thttpd

by Julio M. Merino Vidal
10/13/2005

The Apache HTTP Server is the most popular web server due to its functionality, stability, and maturity. However, this does not make it suitable for all uses: slow machines and embedded systems may have serious problems running it because of its size. Here is where lightweight HTTP servers come into play, as their low-memory footprints deliver decent results without having to swap data back to disk.

Similarly, these small HTTP servers are suitable to serve static content efficiently so as to allow Apache, mod_perl, mod_python, or even servlet containers to handle dynamic requests without tying up memory-hungry children to serve small images. In other words, these applications can serve as a complement to your existing full-featured web server, not as a replacement.

One of these servers is thttpd, a simple, small, portable, fast, and secure HTTP server. Among its features are support for the HTTP/1.1 standard, CGIs, virtual hosts, and IPv6. This article shows how to install and configure this software under NetBSD. I chose NetBSD not only because it is my preferred OS, but also because it has the ability to run on the most disparate old hardware, where thttpd shows its strengths. I had a Macintosh Performa 630 (a 68LC040 chip at 33MHz) running NetBSD/mac68k 2.0 with thttpd on top of it, serving pages to my home network nicely.

Use Cases

Related Reading

Web Site Measurement Hacks
Tips & Tools to Help Optimize Your Online Business
By Eric T. Peterson

I previously mentioned a few use cases for thttpd, but there are several other scenarios in which it may be useful:

  • Slow machines--Machines with old hardware that are able to run a Unix-like operating system (such as NetBSD) are often powerful enough to serve static content with thttpd.
  • Heavily loaded machines--You may have a very powerful machine that runs other heavy processes, such a DBMS, yet you need it to serve some web content (statistics, for example). In this case, thttpd can do well.
  • Simple requirements--Sometimes you may want to provide web content to the public, but you do not need much of the fancy stuff provided by powerful servers such as Apache. In this case, a lightweight server may be enough for your needs. Furthermore, given their smaller code sizes, they have fewer chances to fail and you can audit their code more easily.
  • Serving static content alongside a powerful server--You may have a server running Apache with a load of modules, parsing very complex dynamic pages. These pages often need to include other files, most commonly static images. In this case, thttpd can serve the static data alongside Apache, which will exclusively handle the complex content. There is a section dedicated to this specific use case later.

Installing thttpd

Given my choice of NetBSD, I used pkgsrc, the NetBSD Packages System, to install thttpd. However, please note that you don't have to use NetBSD to use pkgsrc, so all the examples given here can apply to other systems with few hassles; otherwise, use your packaging system of choice to get thttpd.

Focusing on pkgsrc, there are two ways to install thttpd: from sources or from binaries. Sources are appropriate if you do not want to use the default settings, you want to configure the build (perhaps with optimization flags), or you need thttpd on a system for which there are no binary packages. On the other hand, binaries are usually fine for all other cases. I'll cover both approaches.

Assuming you have pkgsrc under /usr/pkgsrc/, installing from sources is trivial:

# cd /usr/pkgsrc/www/thttpd/
# make install && make clean

If you want to go the binary route, make sure that a prebuilt binary package is available for your platform. (See the NetBSD package FTP site.) In my case, I used NetBSD/i386 2.0 with the latest stable pkgsrc branch, pkgsrc-2005Q2, so the following did the trick:

# PKG_PATH=ftp://ftp.NetBSD.org/pub/NetBSD/packages/pkgsrc-2005Q2/
    NetBSD-2.0/i386/All pkg_add thttpd

No matter which procedure you choose, you will see something like the following after a successful installation:

===========================================================================
Installing files needed by thttpd-2.25bnb3:

        /usr/pkg/etc/thttpd.conf
            [/usr/pkg/share/examples/thttpd.conf]

===========================================================================
===========================================================================
The following files should be created for thttpd-2.25bnb3:

        /etc/rc.d/thttpd (m=0755)
            [/usr/pkg/share/examples/rc.d/thttpd]

===========================================================================

===========================================================================
$NetBSD: MESSAGE,v 1.2 2001/11/19 16:23:16 jlam Exp $

In order to use makeweb:

        chgrp <group> makeweb
        chmod 2755 makeweb

The suggested group is "www".
===========================================================================

Note that the exact output depends on your mk.conf settings. I used all the default settings, so the program installed under /usr/pkg/ with its configuration file stored as /usr/pkg/etc/thttpd.conf. Keep the correct paths in mind before proceeding.

Pages: 1, 2, 3

Next Pagearrow





Sponsored by: