Lighttpd provides a lightweight web server that is capable of serving large loads while using less memory than servers like Apache. It’s commonly deployed on high traffic sites, including WhatsApp and xkcd.
Update your system:
apt-get update && apt-get upgrade
Note
The steps required in this guide require root privileges. Be sure to run the steps below as root
or with the sudo
prefix.
Install Lighttpd
Install the server from the Ubuntu package repository:
apt-get install lighttpd
Once the server is installed, make sure that it’s running and is enabled. Visit http://198.51.100.10:80 in your browser, replacing 198.51.100.10 with your IP address. If you configured lighttpd to run on an alternate port for testing, be sure to replace 80 with this port. You’ll see a placeholder page for lighttpd that contains some important information:
- Configuration files are located in
/etc/lighttpd
. - By default, the “DocumentRoot” (where all HTML files are stored) is located in the
/var/www
directory. You’ll be able to configure this later. - Ubuntu provides helper scripts to enable and disable server modules without directly editing the config file:
lighty-enable-mod
andlighty-disable-mod
.
Configure LighttpdPermalink
The main lighttpd configuration file is located at /etc/lighttpd/lighttpd.conf. This file provides a list of server modules to be loaded and allows you to change global settings for the webserver.
The first directive in the configuration is server.modules
, which lists modules to be loaded upon starting or reloading the lighttpd server. To disable a module, add a # at the beginning of the corresponding line to comment it out. Remove the # to enable the module. Modules can also be added to this list and will be loaded in the order they appear.
Following the server.modules block is a list of other settings to configure the server and its modules. Most directives are self-explanatory, but not all available options are listed by default and you may want to add them, depending on your needs. A few performance settings you may want to add yourself include:
server.max-connections
- Specifies how many concurrent connections will be supportedserver.max-keep-alive-requests
- Sets the maximum number of requests within a keep-alive session before the connection is terminatedserver.max-worker
- Specifies the number of worker processes to spawn. If you’re familiar with Apache, a worker is analogous to a child process.server.bind
- Defines the IP address, hostname, or path to the socket lighttpd will listen on. Multiple lines can be created to listen on different IP addresses. The default setting is to bind to all interfaces.
Enable and Disable Modules via Command Line
For ease of use, you may wish to enable and disable modules via the command line. Lighttpd provides a simple method to do this, so the configuration doesn’t need to be edited every time a new module is needed.
Run lighty-enable-mod
from the command line to see a list of available modules and a list of already enabled modules, as well as a prompt to enable a module. This can also be accomplished in one line. For example, to enable the auth authentication module:
lighty-enable-mod auth
This command creates a symbolic link to the module’s configuration file in /etc/lighttpd/conf-enabled
, which is read by a script in the main configuration file. To edit the configuration for a specific module, look for its .conf file in /etc/lighttpd/conf-available.
There are many additional modules that are included in separate Ubuntu packages. Some useful ones are:
- lighttpd-mod-mysql-vhost - Manages virtual hosts using a MySQL database. This module works well when you need to manage a large number of virtual hosts
- lighttpd-mod-webdav - Supports WebDAV extensions to HTTP for distributed authoring of HTTP resources
- lighttpd-mod-magnet - Controls the request handling module
When you have installed these packages you will be able to enable them using lighty-enable-mod.
Restart lighttpd to load changes:
systemctl restart lighttpd.service
Virtual Host Setup with Simple Vhost
This section covers configuration for simple virtual hosting. The simple-vhost module allows you to set up virtual hosts with respective document roots in user-defined folders named for the domains, below a server root. Ensure that all other virtual hosting modules are turned off before proceeding.
1. Enable simple-vhost:
lighty-enable-mod simple-vhost
2. Restart lighttpd to load your changes:
systemctl restart lighttpd.service
3. Modify the following settings in your /etc/lighttpd/conf-available/10-simple-vhost.conf file:
/etc/lighttpd/conf-available/10-simple-vhost.conf simple-vhost.server-root = "/var/www/html" simple-vhost.document-root = "htdocs" simple-vhost.default-host = "example.com"
Virtual Host Setup with Enhanced Vhost
Enhanced virtual hosting works slightly differently than Simple by building the document root based on a pattern containing wildcards. Be sure that all other virtual hosting modules are disabled before beginning.
1. Run the following command to enable the enhanced virtual hosting module:
lighty-enable-mod evhost
2. Restart lighttpd to load the configuration changes:
systemctl restart lighttpd.service
3. To accomplish the same directory structure with evhost as with simple-vhost above, you need to modify the /etc/lighttpd/conf-available/10-evhost.conf file:
/etc/lighttpd/conf-available/10-evhost.conf evhost.path-pattern = "/var/www/html/%0/htdocs/"
4. Modify the server.document-root in the main lighttpd configuration file:
/etc/lighttpd/lighttpd.conf server.document-root = "/var/www/html/example.com/htdocs"
5. Restart lighttpd to load the configuration changes:
systemctl restart lighttpd.service
To modify the host directory format lighttpd recognizes, define the pattern that gets passed to the directory in which the content lives. The following table shows what host directory format is used as the document root for each pattern.
Pattern | Host Directory Format | Document Root Path |
---|---|---|
%0 | Domain name and TLD | /var/www/html/example.com/htdocs |
%1 | TLD only | /var/www/html/com/htdocs |
%2 | Domain name without TLD | /var/www/html/example/htdocs |
%3 | Subdomain 1 name | /var/www/html/exampleSub/htdocs |
%4 | Subdomain 2 name | /var/www/html/exampleSub2/htdocs |
%_ | Full domain name | /var/www/html/exampleSub2.exampleSub.example.com/htdocs |
Create Virtual Host Directories
Whether using simple-vhost or evhost, you’ll need to create directories before lighttpd can use them to serve content. Once the required directives are configured as above, create the required directories, replacing example.com with your domain name:
mkdir -p /var/www/html/example.com/htdocs/
The following command will create two additional virtual hosts for .net and .org top level domains:
mkdir -p /var/www/html/{example.net/htdocs,example.org/htdocs}
The following command will create two additional virtual hosts for the subdomains from the evhost example:
mkdir -p /var/www/html/{exampleSub/htdocs,exampleSub2/htdocs}
Document Root PathRun Scripts with FastCGI
If you need your web server to execute dynamic content, you may run these scripts using FastCGI. To run a script, FastCGI externalizes the interpreter from the web server rather than running the scripts “inside” the web server. This is in contrast to approaches such as mod_perl, mod_python, and mod_php, but in high-traffic situations this way is often more efficient.
To set up FastCGI, make sure that an interpreter is installed on your system for your language of choice:
To install Python:
apt-get install python
To install Ruby:
apt-get install ruby
To install PHP 7 for CGI interfaces:
apt-get install php7.0-cgi
Lighttpd will send CGI requests to CGI handlers on the basis of file extensions, which can be forwarded to individual handlers. You may also forward requests for one extension to multiple servers and lighttpd will automatically load balance these FastCGI connections.