old-www/LDP/LG/issue74/arndt.html

249 lines
11 KiB
HTML

<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Micro web server: how to save CPU time and hard disk space LG #74</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<CENTER>
<A HREF="http://www.linuxgazette.com/">
<IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.png"
WIDTH="600" HEIGHT="124" border="0"></A>
<BR>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="lg_bytes.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue74/arndt.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="fillil.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
<P>
</CENTER>
<!--endcut ============================================================-->
<H4 ALIGN="center">
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Micro web server: how to save CPU time and hard disk space</font></H1>
<H4>By <a href="mailto:marndt@asmsoftware.de">Matthias Arndt</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<h2>Introduction</h2>
<p>
A personal web server. Today, almost any Linux user has one. Some folks do really
serve content with them; others use it for development of PHP or CGI programs.
Others like me just have it to read the documentation via the browser and to
play with it.
I decided that running the Apache web server is
overkill for my personal applications. Currently I have access to a CGI and
PHP capable provider so I do not need support for these on my own machine. Just plain
serving of files without having to run a huge Apache binary in background.
</p>
<p>
As a result, I decided to drop running my own Apache web server in favor of
having a simple micro web server that only answers requests when there are any.
It saves me some disk space and RAM, although that wasn't really a significant factor
since my computer has plenty of capacity. Mostly
I wanted to play around with new software and nifty small but usable solutions.
</p>
<h2>What do I exactly want to do with my web server?</h2>
<p>
Just a few ordinary things, nothing involving PHP or CGI:
</p>
<ul>
<li>serving a few static files and downloads to my friends</li>
<li>read my package documentation through http</li>
</ul>
<p>
This leads to another important thing: at least some sort of directory indexing
must be supported by the web server. That is, if the final URL component is a directory,
redirect to that directory (add the final slash), then serve up the index.html in that
directory. (The redirect is important so that relative links on the page will function
correctly.) Although this can be done with automated scripts run by cronjobs.
But I prefer a simple builtin solution. It doesn't have to be as complex as the
Apache indexing function although that one is very nice indeed.
</p>
<p>
In short: I can use almost any web server that supports the http protocol but it doesn't need many fancy features.
</p>
<h2>Do I need extensive configuration?</h2>
<p>
In fact no. All of these can be accomplished by symlinking external pieces into the web server's root directory.
No need for "Alias" directives or other complicated options. Just the web server root and I'm happy. Perhaps customizing the port the web server listens on.
</p>
<p>
But nothing more. A simple command line like this one should be sufficient for my purpose: "binary /path/to/webserver/root".
</p>
<h2>Standalone server or called via TCP wrapper?</h2>
<p>
I decided to use a TCP wrapper solution. The web server binary gets only called when there really is a request. No need to mess around
with init scripts. Just a simple line in /etc/inetd.conf and off we go.
</p>
<p>
However such a solution is not very performant. In fact, if you plan to have more than a few sporadic accesses to your server, go for a
standalone server that runs all the time.
</p>
<h2>micro_httpd</h2>
<p>
Beside a few really awkward solutions ( there are web servers written in Java, bash or awk out there), I decided to go for a compilable
solution.
</p>
<p>
I found a web server called micro_httpd at <A HREF="http://www.acme.com/software/micro_httpd/">http://www.acme.com/software/micro_httpd/</A>.
This one is written in plain C, takes just around 150 lines of code and does exactly what I want. Runnable from TCP wrapper, no CGI nor PHP, plain serving of files with indexing capability.
</p>
<p>
I just added a few more mime types in the code and it worked out of the box.
</p>
<p>
Grab the sources of micro_http and unpack them.
<ol>
<li>tar xvzf micro_http.tar.gz
<li>cd micro_httpd
<li>rework the source the file if needed, tweak the #define directives to suite your needs
<li>make
<li>su -c "make install"
</ol>
And now you should have a binary called micro_httpd in /usr/local/sbin/.
</p>
<p>
Become root and edit /etc/initd.conf with your favorite editor.
Add a line
<pre>
http stream tcp nowait wwwrun /usr/sbin/tcpd /usr/local/sbin/micro_httpd /var/httpd/wwwroot/
</pre>
to it and restart the Internet super-server inetd.
</p>
<p>
On my SuSE 7.2 Linux, I type "/etc/init.d/inetd restart" as root.
</p>
<p>
Make sure to substitute "/var/httpd/wwwroot/ in the example above with the correct path to your new document root.
</p>
<p>
Substitute the wwwrun with any valid user account, preferably one that has almost no rights on the system for security reasons.
</p>
<p>
Now try it out: put a few html files in your new WWW root and make them readable by the user account specified.
Then point your favorite browser to http://localhost/. You should get either an automated index or your index.html file.
</p>
<p>
Got this far? Great, your small and micro web server is up and running.
</p>
<p>
<b>Note:</b>The TCP wrapper does log all connects to the server to /var/log/messages. But don't expect a complete Apache-style log from it.
Just plain lines like this:
<pre>
micro_httpd[886]: connect from x.x.x.x (x.x.x.x)
</pre>
However with knowledge of the http protocol and the code it should be possible to code an advanced logging facility. I leave that one up to you.
</p>
<p>
In general, any web server that can be run from inetd can be setup like this one. So look around at <a href="http://freshmeat.net/">Freshmeat</a>.
</p>
<h2>Conclusion</h2>
<p>
If your needs are as simple as that, it takes a few minutes to switch from Apache to such a minimalistic solution.
</p>
<p>
It works pretty good although I'm aware that this solution will fail if there are too many requests. For a simple personal web server
without heavy traffic it should be sufficient.
</p>
<p>
At least I'm a bit happier now. Decide - perhaps such a solution would suit your needs as well?
</p>
<BLOCKQUOTE><EM>
[There's also Tux, a micro web server in a Linux kernel module. It works similar
to micro_http, and can chain to a bulkier web server for URLs it can't handle
(e.g., CGI scripts). But note that Tux and micro_http serve different niches.
Tux is for high-traffic sites that serve lots of simple files (e.g., images) and
must keep per-request overhead low to avoid overloading the system. micro_http
via inetd is for sites with light web traffic, where the greater overhead of running
a separate process for each request is overshadowed by the
</EM>no overhead at all<EM> when there are no requests. Of course, both
micro_http and Tux serve a third niche: nifty small usable solutions you can play
with. Or as LG contributing editor Dan Wilder would say, "small sharp tools that
each do one thing well in the honorable UNIX toolbox tradition."
<P> For more information about Tux, see
<A HREF="http://www.redhat.com/docs/manuals/tux/TUX-2.1-Manual/">Red Hat's Tux 2.1 manual</A>.
I thought Tux was in the standard kernel but I can't find it in 2.4.17, so you'll
have to look around for it.
-Iron.]
</EM></BLOCKQUOTE>
<!-- *** BEGIN bio *** -->
<SPACER TYPE="vertical" SIZE="30">
<P>
<H4><IMG ALIGN=BOTTOM ALT="" SRC="../gx/note.gif">Matthias Arndt</H4>
<EM>I'm a Linux enthusiast from northern Germany.
I like plain old fifties rock'n'roll music, writing
stories and publishing in the Linux Gazette, of course.
Currently I'm studying computer science in conjunction with
economics.</EM>
<!-- *** END bio *** -->
<!-- *** BEGIN copyright *** -->
<P> <hr> <!-- P -->
<H5 ALIGN=center>
Copyright &copy; 2002, Matthias Arndt.<BR>
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
Published in Issue 74 of <i>Linux Gazette</i>, January 2002</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<HR><P>
<CENTER>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="lg_bytes.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue74/arndt.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="fillil.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
</CENTER>
</BODY></HTML>
<!--endcut ============================================================-->