31,19 €
In Detail
Lighttpd is a secure, flexible, and most importantly, light web server designed and optimized for high performance environments. It is open-source and licensed under the revised BSD license. Its event-driven architecture optimized for a large number of parallel connections, its advanced features (FastCGI, CGI, Auth, Output Compression, URL Rewriting, and many more), and its small memory footprint compared to other web servers, make Lighttpd the perfect server software for every web server that suffers load problems or for serving static media separately from dynamic content.
Lighttpd supports the FastCGI, SCGI, and CGI interfaces to external programs, permitting web applications written in any programming language to be used with this server. Excellent performance for PHP, a particularly popular language, has received special attention. Additionally, Lighttpd has become popular within the Ruby on Rails community.
This is your fast guide to getting started and getting inside the Lighttpd web server. Written from a developer's perspective, this book helps you understand Lighttpd, and get it set up as securely and quickly as possible.
Who this book is for
This book is for System Administrators or Web Developers seeking a lean deployment platform for web applications or who want to switch to a "lighter" web server than Apache.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 269
Veröffentlichungsjahr: 2008
Copyright © 2008 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: October 2008
Production Reference: 1151008
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847192-10-3
www.packtpub.com
Cover Image by Vinayak Chittar (<[email protected]>)
Author
Andre Bogus
Reviewer
Peter Lavetsky
Development Editor
Swapna V. Verlekar
Technical Editors
Dhiraj Chandiramani
Rasika Sathe
Editorial Team Leader
Akshara Aware
Project Manager
Abhijeet Deobhakta
Project Coordinator
Abhijeet Deobhakta
Indexer
Monica Ajmera
Proofreader
Claire Lane
Production Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
Andre Bogus is a musician turned programmer. He has worked in different jobs from voice acting to programming to teaching to managing software projects. At the moment he works as a consultant and implementer for KOGIT GmbH, an Identity Management company based in Germany.
He found Lighttpd while searching for the ideal software for his personal web server and quickly learned the tricks to make it do what he wanted. He enjoys learning new things and telling others about them. When his full schedule allows it, he can be found on the #lighttpd IRC channel.
He wants to thank his wife, Ania, without whose support he would not have been able to finish this book. Also he appreciates his employer for allowing him to write besides his day job. The nice people at PACKT Publishing have also earned his gratitude by helping this book to become what it is.
Peter Lavetsky is a Senior Research and Development Analyst with Dealer.com, located in Burlington, VT. He has written multiple Lighttpd plugins as well as tuned many instances tailored to Dealer.com’s web serving needs. Peter currently works on integrating third-parties into the Dealer.com platform, including Google Base and Google AdWords. In his spare time he enjoys checkraising the turn and feeding the tiger shark.
This book explains downloading, installing, and configuring the Lighttpd HTTP server, illustrates how to extend it with modules and Lua code, shows a migration path from Apache httpd, gives case studies in setting up a number of popular web applications, and even demonstrates how to extend Lighttpd by writing our own modules.
The name Lighttpd (pronounced "Lighty") is an abbreviation pulling together Light (as in weight) and HTTPD (which is an abbreviation for Hypertext Transport Protocol Daemon, in short web server). Early versions called themselves LightTPD to emphasize the "lightweight" part, but this led to confusion over pronunciation and meaning, so the capitalization was reduced.
Chapter 1 gives directions how to obtain Lighttpd. Regardless, if we want to use a binary package or build from source, everything is there. In addition, dependencies, optional packages, and compilation options are examined. After working through this chapter, we should have an installed Lighttpd to work with.
Chapter 2 introduces all elements of the configuration language by example. Usable examples include sending the correct MIME type, setting up multiple domains, rewriting, and redirecting. Also the command line options are explained. For those who are not fluent in regular expressions, the chapter has an excursion. At the end of this chapter, we have our Lighttpd up and running.
Chapter 3 builds on the concepts of the second chapter and discusses the configuration various CGI-like interfaces, three modules for virtual hosting, also introducing the MySQL database, which is used in one of the modules.
Chapter 4 shows how to set up Lighttpd as a download or streaming server, covering optimizations for large downloads as well as guarding our site against denial of service attacks, dealing with proxies, and restricting download speeds for anonymous clients.
Chapter 5 extends our Lighttpd to learn more about our users: Geo-tracking the location from the client IP address, dissecting the page traversal behavior ("clickstream analysis") and other data points. Also responsible access logging practices are outlined.
Chapter 6 adds SSL support to our Lighttpd and walks through the steps to acquire or create the required certificates, whether we obtain a certificate from a public or corporate certificate authority, self-sign a certificate, or become our own certificate authority.
Chapter 7 helps us securing our Lighttpd by authorizing access, limiting traffic by IP to thwart denial-of-service attacks, and measuring our success by rigorously examination of our log files. Setting up log rotate and log parsers is also covered.
Chapter 8 concerns itself with limiting the potential damage a subverted Lighttpd could do to the system. The techniques to achieve this are reducing privileges and putting the whole Lighttpd in a secluded environment. Containing Lighttpd and a CGI backend in different environments is also demonstrated.
Chapter 9 shows a strategy to optimize our Lighttpd from system and configuration settings to the source code itself. The chapter also shows specific optimizations known to yield benefits across most systems.
Chapter 10 takes a pragmatic look on the migration path from Apache httpd. It shows how to port basic configuration, rewrite and redirect rules, how to deal with .htaccess files, and even discusses when not to migrate.
Chapter 11 revisits the CGI interfaces by getting various example applications from Ruby on Rails over WordPress, phpMyAdmin, trac, and AWstats to AjaxTerm up and running with our Lighttpd.
Chapter 12 adds the small and fast scripting language Lua to the mix, which can be used to extend the functionality of Lighttpd by mod_magnet or as a backend language by the Lua/FastCGI interface written by the same author as Lighttpd. Both options are discussed, along with an introduction to the language itself.
Chapter 13 gives a run down of extending Lighttpd by extending existing modules or even writing our own. With these modules, we can change the behavior of Lighttpd from request parsing to sending or altering content. This chapter is aimed at an average C programmer.
Appendix A lists the HTTP status codes that our Lighttpd can return on answering a request, giving directions which chapter or other source might have more information on each request.
Appendix B is the module and configuration index. Each configuration option for every Lighttpd module of the official distribution is explained here in one or two short sentences. Forgotten how a configuration option is written, what type it has or what it means? Look no further.
To work through this book effectively, you will need at least a computer running on one of the supported operating systems (Refer to Chapter 1 on installation) connected to the Internet. Basic knowledge about computers, the Internet, (especially the HTTP protocol), and one or more programming language is also helpful.
This book pulls together all the information and gives helpful examples instead of complex theories. As Lighttpd is mostly used in an environment, common interfaces are also shown.
So, if you are a web developer or an administrator, and you want to learn how you can install, configure, secure, optimize (or even extend), and generally get the most out of Lighttpd, you should read this book.
Now, before reaping the benefits of Lighttpd, we first need to download and install it.
Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply drop an email to <[email protected]>, making sure to mention the book title in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Visit http://www.packtpub.com/files/code/2103_Code.zip to directly download the example code.
The downloadable files contain instructions on how to use them.
Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide the location address or website name immediately so we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with some aspect of the book, and we will do our best to address it.
In this chapter, we will learn:
What is Lighttpd? Lighttpd, or Lighty, as it is affectionately called, is an extensible, modular, low-footprint, single-threaded, high performance, web server that will happily run on small servers, and outperform an Apache server or Microsoft IIS in most settings. Lighttpd powers many large sites, such as the YouTube video download servers and the image upload server of Wikipedia. At the time of this writing, Lighttpd has the fifth place in the netcraft web server top ten. The plugin architecture encourages developing custom modules and trying new ideas. The development community around Lighttpd is friendly, helpful and pragmatic, and the documentation, though a little scattered, is quite thorough, if you know where to look.
Lighttpd has very little dependencies considering the wealth of functionalities it provides. For most systems, getting Lighttpd is just a matter of downloading and installing a package. Before we go out and get one, we better know what we want. There are two branches of Lighttpd: a stable branch and a development branch.
The stable branch is very solid and changes at the most once every two months (if bug fixes are not counted, then about once a year), allowing the developers to concentrate on bug fixes. The development branch moves faster, with a new release every four to six weeks. The development snapshots contain new shiny features, but can also contain hidden bugs, break old features and can generally be less stable.
At the time of writing, version 1.4.19 is deemed to be the stable version, while pre-releases of the upcoming 1.5.0 version are distributed for more testing before the final release. Some systems might have packages of older versions, but anything older than the stable branch many contain known security holes.
For a live server, or if we want the latest versions, we usually compile Lighttpd from sources. For a development server, we might take the easy route and install a precompiled package to leave the worries about dependencies to whoever maintains the package database.
The last question is, on which system we should use Lighttpd? My pragmatic advice is to use what you have. For a development system, take the platform you currently work on. For integration and production platforms, this advice needs to be constrained a little—apart from Windows, which allows too little connections to be open in parallel, most operating systems are suitable for production use—the POSIX-implementing ones (Linux, every BSD, Solaris, Mac OS X, and so on) also benefit from numerous optimizations.
Now, without further ado, here is a list of binary packages per system:
System
Download address or command
Debian GNU/Linux, Ubuntu, Knoppix, other Derivatives
apt-get install libpcre3
apt-get install zlib1g
apt-get install mysql-common libmysqlclient12
apt-get install lighttpd lighttpd-doc
Fedora / Red Hat Novell / SuSE Linux other RPM-based distributions
yum install pcre
yum install zlib
wget http://www.kevindustries.com/media/kw/files/linux/lighttpd/RPMS/lighttpd-1.4.13-3.KWEL4.i386.rpm
Gentoo Linux
emerge lighttpd
Windows
http://www.kevinworthington.com:8181/?cat=20
To compile Lighttpd from source, download the latest source package from http://lighttpd.net/download/. Between Lighttpd versions 1.4.19 and 1.5.0, the build system has changed from Autotools to CMake. Before we can install it, we need the following:
Apart from these, there are some optional packages that expand the capabilities of Lighttpd:
Lighttpd was built using Autotools until version 1.5.0, in which the authors experimented with CMake (and other build systems). The Autotools build system has been around for some time. So, almost every system that has a sufficient toolchain can build Lighttpd.
Note that the building can and should be done as a normal user, while the installation must usually be done as a superuser, unless the target directory is owned by the normal user. The easiest way (provided we have sudo) is:
Before we enter this command line, we can set a few environment variables that will affect the build process. We can do this in a bash compatible shell using:
This will set the variable SOME_VAR to X and export it to the shell. Alternatively, we can just omit the export if we write the variable declarations at the beginning of our command, as in:
Here are the most important variables:
Variable name
Useful value
Description
CC
arm-gcc icc
Specify an alternate compiler if you cross-compile Lighttpd or have a more optimizing compiler compared to gcc.
CFLAGS LDFLAGS
-g -Os -L/usr/local/lib
These options go to the gcc compiler. Read up on gcc for further information.
PKG_CONFIG
/opt/pkg_config
We may need to specify where pkg-config is, if configure cannot find it.
FAM_CFLAGS FAM_LIBS
-I/opt/fam/include -L/opt/fam/lib
We can specify alternate C Flags and linker settings (for example, paths) for FAM.
LUA_CFLAGS LUA_LIBS
-I/usr/include/lua/ -llua
This tells configure where to find Lua (for example, if pkg-config is missing)
configure takes some options to select features. These options are usually expressed as:
Note that for every "with-something" option, there also is a "without-something" option that does the exact opposite. Here are the most important options:
Parameter (example)
Description
--help
This makes configure print a help screen and exit.
--prefix=/usr/ --prefix=/opt/lighttpd/
Specify your installation directory if you want to install Lighttpd at a location different from default/usr/local/.
--bindir=... --sbindir=... --datadir=... --libdir=... --sysconfdir=...
We can also set each directory individually for the installation process.
--host=PLATFORM --target=PLATFORM --build=PLATFORM
If we want to cross-compile Lighttpd or have different platforms to compile Lighttpd against, we can specify them here. Usually, we can leave these settings alone.
--enable-static --enable-shared
Makes configure build static or shared libraries to link to the Lighttpd executable. The same default is shared.
--enable-lfs
This option enables large files (above 2Gig). Set it if you host HD-movies or large genome sequence files.
--disable-ipv6
Lighttpd by default can use IPv6 in addition to the usual IPv4. Disabling it may reduce the size a little bit and quell our fears of possible bugs in the IPv6 implementation, but may leave out all users of next-gen Internet technologies in the cold.
--with-pcre
This is on by default if PCRE is available. You probably want it anyway, unless your target system is embedded.
--with-openssl
This enables SSL (usually using OpenSSL).
--with-kerberos5
This makes configure use the kerberos5 support supplied by OpenSSL.
--with-zlib --with-zlib=/usr/local/lib/
This adds libgz compression to Lighttpd (via mod_compress). If the path is omitted, configure will try to infer it.
--with-bzip --with-bzip=/opt/lib/
This adds bzip2 compression to Lighttpd (via mod_compress). See --with-zlib.
--with-fam --with-fam=/opt/fam/
This activates the use of the FAM/gamin stat cache which speeds up Lighttpd considerably on repeated requests for the same file.
--with-ldap
This allows Lighttpd to authenticate users (in mod_auth) against an LDAP directory.
--with-webdav-props --with-webdav-locks
These options enable properties and locks in WebDAV (mod_webdav).
--with-gdbm --with-memcache
These options enables the use of GDBM or memcached storage in mod_trigger_b4_dl, respectively.
--with-atttr
This makes Lighttpd support XFS' extended attributes to get the MIME type for a file (by mimetype.use-xattr).
--with-mysql --with-mysql=/opt/mysql/
This option adds MySQL support by mod_mysql_vhost. The optional path should contain mysql_config.
--with-lua --with-lua=/usr/src/lua
This enables the use of the Lua programming language to be embedded into lightTPD as mod_magnet.
--with-valgrind
This adds internal valgrind support. We only need this if we want to debug Lighttpd memory usage.
The make utility will use the Makefile that configure has generated to build and install Lighttpd. Configure Lighttpd to your needs, but the defaults will take care of most of them.
Starting with 1.5.0, the Lighttpd developers tried using CMake instead of Autotools to speed up the build process. However, it was apparently removed in the development snapshots, for which Autotools are used. If we come across a version with a CMakeLists.txt file, we can build it using CMake.
CMake can be run with the -i option to start it in the wizard mode so that we are queried on all options. This is probably the easiest way of setting up Lighttpd, but depending on how many installations we have we might want to do a command-line install. In this case, cmake -L gives us a list of options, which we can supply via the -D switch, as in:
Options of the type BOOL can take the values ON, OFF (or TRUE, False, Y, N, and so on). All other types are basically handled like strings. The naming of the options should be similar to the options for Automake in the preceding table.
Regardless of whether we use precompiled packages for our target operating system, or compile our own Lighttpd, the setup would not be much easier. A seasoned administrator will compile and install a basic Lighttpd in under 15 minutes (in fact even a less weathered one can do, since this is what I timed on my last attempt to install 1.4.19).
Now that we have installed our Lighttpd, let us get it up and running.
