khttpd
The Linux 2.4.0 kernel's new kernel daemon speeds up access to static Web pages. This article takes a quick look at that new daemon.
Linux 2.4.x has a new kernel daemon; this kernel daemon is a Web server. It may seem strange to you that Linux should have a built-in Web server, but think about it this way:
-
Most Web pages are static.
-
Static Web pages are just an exercise in copying to the network.
-
NFS does nothing more than copy files to the network.
-
Why use a large user-space program that has to make a system call to copy a file to the network?
If you read this and understood it, you can see that this kernel "Web server" daemon is not an Apache replacement. On the contrary, if you want to have any kind of dynamic Web content, the kernel Web daemon won't cut it. It can only copy static files to the network.
The advantage is speedand lots of it. Apache is a large program that can handle all kinds of dynamic content very well. But with a static page, Apache will read it, process it (to make sure there's no dynamic content to contend with), and then make system calls and pass the page to the kernel to copy to the network.
The kernel Web daemon grabs the static file and copies it to the network; It's fast, clean, and simple. The kernel daemon is small and fast. But what happens if your Web site has several pages of dynamic content along with all the static pages? No problemthe kernel Web daemon (called khttpd) passes the request to Apache, which performs all the wizardry and, when the content is ready to copy to the network, hands it back to khttpd (because now it's static). Then off it goes.
If you want to run khttpd, you'll first need to insert the module. Inserting the module creates certain entries in the /proc table; specifically, it creates the directory /proc/sys/net/khttpd and the following files:
We'll take a quick look at each of these and their default values. Then I'll provide you a script you can use to change the values.
The clientport is where khttpd will look to find a Web server. Any page that is not static, any cgi script, or any other dynamic page will be sent to the localhost on this port (more on this comes later). The default is 80. You will want to change this to 8080 or 8000, or wherever you want to relocate Apache (details on what to change in your httpd.conf file come later).
The documentroot is the same as the DocumentRoot in your httpd.conf file. The default is /var/www.
The dynamic file lists page extensions that contain dynamic Web content. These must be listed. By default, only cgi-bin and .. are contained in this list. You may want to add extensions such as shtml, php, php3, phps, cfm, inc, and asp, among others. Any pages requested (such as a php *.inc file) will be copied to the network as isand this is probably not what you want. So, you'll need to echo each extension, one by one, into this file. You'll see this in the script later.
The logging variable just turns logging on and off. Currently, if you want logging, you'll need to grab a (unsecure) user-space logging daemon from http://www.fenrus.daemon.nl/. The default is 0 (off).
The maxconnect file contains a number that is the maximum simultaneous number of users permittedby default, 1000.
The perm_forbid file contains a number (mask) that is used by stat to determine if an access is forbidden. The default 16969 basically prohibits directory, sticky bit, and execute permissions.
The perm_required file contains a number (mask) used by stat to determine if access is permitted. The default number 4 requires that the file be world-readable.
The serverport file contains the port that the server (khttpd) will run on. By default, this is port 8080.
The sloppymime default of 0 (off) tells khttpd to be strict in its interpretation of what mime-type the file is. In this case, khttpd will process files only of mime-type text/html. Setting this to 1 will cause khttpd to treat all unknown mime-types as text/html.
The threads default is 2. This default is for large sites. A large site is a site that, if every page on the site is loaded into memory simultaneously, won't all fit in memory. If your site is small, you'll want to set this to 1. This determines the number of threads available per CPU, but it impacts memory usage.
The start, stop, and unload defaults all start as 0 (off). When you want to start khttpd, you need to put a 1 into start. Putting a 1 into stop will stop the khttpd daemon. Putting a 1 into unload will permit you to remove the khttpd module from memory.
Apache Configuration
When you start using khttpd, you need to make some minor changes to Apache. First, change the BindAddress variable from * to 127.0.0.1. This will prevent all but khttpd from accessing Apache (unless a user is accessing Apache directly from the localhost). Then change the Apache Port variable from 80 to 8080 or 8000, or whichever port you specified in the clientport khttpd variable previously, and restart apache.
rc.khttpd script
The following script can be run as is, or it can be run from rc.local during bootup:
#!/bin/sh /sbin/modprobe khttpd echo 8080 > /proc/sys/net/khttpd/clientport echo /home/httpd/htdocs > /proc/sys/net/khttpd/documentroot echo phtml > /proc/sys/net/khttpd/dynamic echo inc > /proc/sys/net/khttpd/dynamic echo shtml > /proc/sys/net/khttpd/dynamic echo php3 > /proc/sys/net/khttpd/dynamic echo phps > /proc/sys/net/khttpd/dynamic echo php > /proc/sys/net/khttpd/dynamic echo asp > /proc/sys/net/khttpd/dynamic echo cfm > /proc/sys/net/khttpd/dynamic echo 1 > /proc/sys/net/khttpd/threads echo 80 > /proc/sys/net/khttpd/serverport echo 1 > /proc/sys/net/khttpd/start
The kernel Web server daemon is now serving your pages.
You should notice an immediate increase in response time on your Web page accesses, especially if you're accessing via a 10Mbs or faster Ethernet connection.
But don't take my word for ittry the Web daemon for yourself. If you don't see an improvement, you may have other problems. I'd tell you more if there were more to tell, but khttpd is simplicity itself.