Customize and build a local lighttpd test server
While fiddling around writing a simple CGI-based web application, I found myself needing a web server for testing purposes.1 A key requirement for that server (lighttpd) being that it needs to be built and installed without root permissions and run locally from my home directory, the setup-process is a bit more involved than would be a default installation. In this post, I therefore describe how to install, configure and run lighttpd given the above conditions.
The post consists of three sections. In the first section, I discuss the various build and installation steps by means of a makefile. The second section shows how to interact with the server. (Readers eager to run lighttpd right away should download the makefile, put it in a clean working directory, and directly jump to section two.) Section three concludes.
Build and configure lighttpd
To install in and run lighttpd from a non-default directory, I need to build the server from source and carry out some additional configuration steps. More specifically, setting up lighttpd entails
- building lighttpd from source
- setting core server variables
- specifying required modules
- creating the main server confguration file
The setup process is not very complicated as of itself, the most demanding task being to create additional directories (which we would not need when installing to the default location) and the main configuration file. Yet, to make the customized build, install and configuration process repeatable, I wrote a makefile2 which automates the steps mentioned above.
|
|
The makefile consists of seven blocks. In the first block (lines 4-23), I define several default variables that govern the build and installation process, the most important being PREFIX
, which sets Lighttpd’s installation path (in this case to $HOME/opt/lighttpd
). I leave the details about the remaining variables to the interested reader.
The second block (lines 24-27) defines Lighttpd’s root directory (which is set to depend on PREFIX
) and the port to which to listen (port 1024
).
The third block (lines 28-43) consists of make
-related housekeeping (the details of which are left again to the interested reader).
Block four (lines 44-58) deals with downloading the source tarball and extracting the files needed to build lighttpd. This step requires a working internet connection. To skip the download, point make
to the tarball’s location by overriding the default value of the variable TARBALL
when invoking make
(i.e. make TARBALL=path/to/tarball
).
The fifth block is the core step for automatically building the lighttpd test-server (lines 59-88). Automatic customization is achieved by the makefile calling lighttpd’s make
targets with variables modified for the present use case. As defined in line 71, install depends on the targets install_lighttpd (which wraps lighttpd’s default install target) and config_files (consisting of configuration tasks required for lighttpd to run). config_files and its dependencies are shown in the next block.
The sixth block describes the configuration tasks needed for running lighttpd with CGI support (lines 89-142). The tasks involve
- copying the module configuration files (located in
$(EXTRACT_DIR)/doc/config/
) to the directory where lighttpd has been installed (this location being$HOME/opt/lighttpd/
in my case) - creation of the main configuration file
lighttpd.conf
(lines 103-131) - within
$HOME/opt/lighttpd
, creating arun
directory (needed to store the server’s process ID), as well aslog
,htdocs
, andhtdocs/cgi-bin
, which will contain the server logs, index file, and CGI applications respectively (lines 133-141).
Finally, block seven will create a start and stop script (see lines 143-160). Recall that the lighttpd.conf
created by the makefile sets the port number to 1024. Lighttpd blocking this port while running, attempts to start it twice or more will not work unless a different port number (and hence different configuration file) is specified for each instance.
Run locally
Assuming a clean working directory containing the above makefile, download, build, and install lighttpd ($HOME/opt/lighttpd
) in a shell:
# download, configure and build
make
# install
make install
To start the server, run:
bash start_lighttpd.sh
# alternatively, assuming the start and stop scripts have execution
# permissions
./start_lighttpd.sh
To check whether lighttpd is actually serving content, navigate to 127.0.0.1:1024
. The server should return “Welcome. This is lighttpd”.3
curl 127.0.0.1:1024
Welcome. This is lighttpd.
Stop lighttpd:
bash stop_lighttpd.sh
The server can be uninstalled by calling the uninstall target. This will remove the lighttpd
directory in $HOME/opt/
as well as the start and stop scripts.
make uninstall
Conclusion
Developing a CGI application requires a suitable web server. For my use case, the server should run locally and be installed in $HOME
(instead of e.g. /usr/local/bin
). As quite prominently display in the main code listing, makefiles provide an ideal way to structure and run (customized) build processes. In fact, make && make install
takes care of (almost) everything.
-
Developing a CGI web application may at first seem rather uncommon in light of the plethora of current technologies and web frameworks (e.g. JavaScript; Ruby on Rails, Spring, Django). My application however consists only of a remote procedure on the server which is called via a URL and returns a json-formatted response. Given this use-case and my limited knowledge about web frameworks, I preferred to stick to the simplicity of CGI. ↩︎
-
Incidentally discovering the numerous marvels of
make
(for more on this, see the documentation e.g. here). ↩︎ -
For simplicity, the index.html created automatically contains no markup. ↩︎