[ Bloke.com || Linux || JavaScript || Java || Volleyball || Link Me ]
Free: [ Guestbook || MessageBot || Plugins || Counter || AusPrices || Advertise ]

Home - Linux - thttpd


One of the best web servers I ever found. Fast and Simple. Great for static content. I split my web server in two apache and thttpd. thttpd does all the images, apache does all the fancy stuff. What a huge difference. This has actually saved us buying at least two more machines (although another way to think of it is that apache is deficient, and would have cost us!).

Start and Stop scripts I set up all my thttpd stuff in /www/thttpd (for my own reasons).


cd /www/thttpd
ulimit -n 16767
echo start images
$thttpd -d /www/doc/flamingtext -nor -nos -p 80 -h -c '**.cgi' -i /www/thttpd/pids/images.pid -l /etc/httpd/logs/images.log
#-t /www/thttpd/images.throttle


cd /www/thttpd
echo stop images
kill -USR1 `cat pids/images.pid`
Note the use of USR1 in the stop script. What this does is "unlisten" on port 80, and closes the connections normally. This way you can do /www/thttpd/stop ; /www/thttpd/start

If you don't use USR1, then thttpd will shut the connections down, and then stop listening, so then when you attempt to start the socket might still be in use.

I guess that could still be true, hmmm, I'll have to think about a good solution for that. One way would be in "start". If it fails returning an error code, then sleep some time and start it again. If you don't use -D, then it will return 0 of successful startup.

If you are running a busy server you will want to increase the number of fd's available .. this is done with the ulimit -n xxx in the start script, but before then you need to do : echo 32767 > /proc/sys/fs/file-max which I do at the end of /etc/rc.d/rc.local (red hat 7.x)

I also use thttpd on multiple IP addresses. It's best to group them where the caching would be the most (ie serving out the same images). But I just modify start/stop to add another server in, and stop another server.

Patch: thttpd 2.20c doesn't compile on red hat 7.2

change libhttpd.c

#include <sys/time.h>
#include <time.h>
Easy enough :-)

Lingering close patch
Also people have seen this as a tmr_ problem and a fdwatch() problem. But the real problem is that more than one lingering timeout is created. When a connection is marked to linger, a timeout is set to leave the connection there to read the bytes from the client until the timeout goes off. But if the connection errors before then, then a second timer is kicked off, and as a result, the fd is closed more than once, which causes a few different problems.

Apply the patch to 2.22beta4 and it will also work fine. This patch has been incorporated into 2.23beta1 (well, Jef used a different version).

DBM patch If you have a lot of "members", (using http_auth), then this patch is for you. Since thttpd reads the .htpasswd file each time to figure out if the user is valid or not. This uses DBM modules to speed this up. My patch certainly could be a lot better, but I'm waiting for feedback from large sites on how they use password files as to how to best optimize this.

Included is a program in contrib/dbm that will convert your .htpasswd file to a dbm file.

Note that this patch replaces the usage of .htpasswd, rather than complementing it. And as thttpd doesn't read .htaccess, nor does this patch. It also does not do heirachical password protection. You need a passwd file in each of the directories, but I should probably add this soon.

tar xvfz thttpd-.tar.gz
cd thttpd-
patch -p1 < thttpd-dbm.patch 
make install
That's it!

Included is a program in contrib/dbm called pass2dm that will convert .htpasswd files to dbm files. For gdbm, the dbm files are .htpasswd.dir and .htpasswd.pag, so you can just run it like

pass2dbm .htpasswd
but if you wanted to rename to some other file, you would use:
pass2dbm .htpasswd .dbm-passwd
or whatever you want (doesn't need to be a '.' file), and of course you can change the .htpasswd to be the name of your file.

I didn't test global passwd files, and I didn't test updating the db on the file, but the later should be okay because it re-reads the header file each time.

patch for 403 error, when should be 404
When you use -nos, and access a file that is not there, the user gets back a 403 (Forbidden) error. So this patch contains a simple fix to output a 404 message (File not Found)
In 2.22beta4 (and some other versions), you get a warning about atoll being defined twice. This patch renames it to myatoll (as per jef's suggestion).

MSIE bandwidth patch This patch adds a config.h option to disable the output of extra characters when an error message is generated. In my case I generate a lot of temporary images, so 404 are common, as are illegal referer. In this case, I don't really care that they even see a HTML file since it's usually a GIF.

I take a fresh copy and configure it as normal: then I change -O2 to -g in the Makefile, and run make clean and then make. Then to run in the debugger, I set my start script to something like:

echo start sigs
echo run -D -d /www2/doc -nor -nos -p 80 -h -c '**.cgi' -i /www/thttpd/pids/sigs.pid -l /etc/httpd/logs/sigs.log
gdb $thttpd2
#$thttpd -d /www2/doc -nor -nos -p 80 -h -c '**.cgi' -i /www/thttpd/pids/sigs.pid -l /etc/httpd/logs/sigs.log
and so when I run it:
[root@jack cameron]# /www/thttpd/start
run -D -d /www2/doc -nor -nos -p 80 -h -c **.cgi -i /www/thttpd/pids/sigs.pid -l /etc/httpd/logs/sigs.log
GNU gdb Red Hat Linux 7.x (5.0rh-15) (MI_OUT)
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux"...
(gdb) run -D -d /www2/doc -nor -nos -p 80 -h -c **.cgi -i /www/thttpd/pids/sigs.pid -l /etc/httpd/logs/sigs.log
Starting program: /www/src/thttpd/thttpd-2.22beta5cg-me/thttpd -D -d /www2/doc -nor -nos -p 80 -h -c **.cgi -i /www/thttpd/pids/sigs.pid -l /etc/httpd/logs/sigs.log
Noticed that I added -D, which puts it into debug mode, which basically stops it from running daemon(), and backgrounding the process (making more painful to debug).

In older versions of thttpd, syslog didn't work for me, and I was too lazy to figure out why, I usually did some spastic writing to a /tmp file to debug it... In more recent version of thttpd, it works perfectly.

Building php into thttpd.

Info from http://lxr.php.net/source/php4/sapi/thttpd/README

This version doesn't work with anything but thttpd-2.21b.

$ tar -xvfz thttpd-2.21b.tar.gz
$ tar -xvfz php-4.1.2.tar.gz
$ cd php-4.1.2
$ ./configure --with-thttpd=../thttpd-2.21b
$ make
$ su
# make install
# exit
$ cd ../thttpd-2.21b
$ ./configure
$ make
$ su
# make install
# exit

Speed: I haven't really started on this, but some pointers.

By default (on redhat linux and others), thttpd, and syslogd don't work together (you need to make some changes). Check out http://halplant.com:88/server/thttpd_FAQ.html, but basically comment out the line in syslog.c

// #define USE_INET
In newer versions of thttpd (thttpd-2.22beta5), syslog.c no longer exists, and it works perfectly without any changes!

Signal On systems that don't have sigset(), the signal handles don't reinstantiate themselves. for example, for solaris systems. Strangely on redhat (and assume other linux kernels), the signal handler is not reset (SIG_DFL) as per the documentation.

More patches to come: '?' patch in files....


Last Change: Monday, 10-Jan-2005 07:41:50 EST


The information provided within these pages is provided AS IS, and without any warranty. Following these directions may (but not limited to) crash your computer, delete all the information on your hard disk, open up security holes or cause your house to burn down. I made these pages to provide some information about the setup that I have done, but I did not proofread it for correctness, and in most cases did not test it. There are commands in these pages that would definately delete or corrupt all the data on your computer (especially the dualboot section). In fact it happened to me.... So you are on your own!

Cameron Gregory