Linux Applications

Configure Lighttpd

Lighttpd or Lighty is a free and open-source web server developed for high-performance environments. Lighttpd server is lightweight, high-speed, and secure. It is mainly provided as an alternative to web servers such as Apache because it consumes very few resources such as CPU and memory.

Lighttpd can handle more than 10000 parallel connections on a single server; it is often an excellent option for high-traffic sites.

This guide will walk you through installing and configuring the Lighttpd server.

Requirements.

To follow along with this guide, you will require the following:

  1. A fresh install of the Ubuntu/Debian server.
  2. A root or sudo account.
  3. Internet connectivity.

Install Lighttpd server

The first step is to install the Lighttpd server on our system. Open the terminal and update the system repositories.

$ sudo apt-get update
$ sudo apt-get upgrade

Proceed to install the Lighttpd package using apt. Lighttpd server is available on the Debian/Ubuntu repositories.

$ sudo apt-get install lighttpd

Once the installation process is complete, start the service using systemctl as:

$ sudo service lighttpd start
Starting web server: lighttpd.

Check if the Lighttpd service is running:

$ sudo service lighttpd status

Open the browser and navigate your machine’s IP address to access the webserver. You should see the default Lighttpd page as:

Configuring Lighttpd

After installation, it is good to customize the Lighttpd server for various options and needs. In this section, we will discover important configuration files and directives.

The main configuration file for the Lighttpd server is located in /etc/lighttpd/lighttpd.conf. It contains a collection of modules that are applied during server startup. If you want to make changes to the Lighttpd server, this is the place to do it

Basics

To view the contents of the file, you can use your favorite text editor or use the cat command as:

$ cat /etc/lighttpd/lighttpd.conf

The Lighttpd configuration file starts with the server.modules block. This module contains a list of server modules that are applied during server startup.

You enable a module by adding it to the list separated by commas.

To disable a module, add a pound sign (#) at the start of the module name. Adding a # sign will comment out the line, which will be ignored when the server starts.

The server will load each enabled module in the order it appears.

After the server.modules block comes to a list of settings that modify the behavior of the webserver. Most of these settings are straightforward and descriptive. Such settings include:

  1. server.document-root – Defines the default document root for your web server.
  2. server.port – the default listen port for the webserver.
  3. server.username – specifies the username for starting and stopping the server. If not specified, it defaults to the root user.
  4. server.groupname – the group of the user for managing the server.
  5. server.errorlog – sets the path to the error log file.

Although not enabled by default, you can add the following settings to modify various settings for the server.

  1. server.bind – specifies the IP address or hostname on which the server will listen. By default, the server will bind to all addresses.
  2. server.max-connections – defines the maximum number of synchronized connections.

The next block are HTTP parse request options. The values in this block follow a syntax as shown below:

server.http-parseopts = ( "option-name" => "enable", ... )

Standard options in this block include:

  1. header-strict – if enabled, this option defines the maximum number of characters allowed in an HTTP request header.
  2. host-strict – this option is similar to header-strict. However, it limits the max characters in an HTTP request Host header.
  3. url-path-backslash-trans – if this option is enabled, the server will translate a backslash to a forward slash. This is useful when you are running the server on a Windows machine.
  4. url-path-dotseg-remove – if enabled, the server will resolve and remove the . and .. in path segments.

The above are some common HTTP parse options for the Lighttpd server. You can learn more by checking the documentation.

Enable Lighttpd modules

One way to enable and disable modules for the Ligttpd server is to edit the configuration file. However, you can use the command-line utility, which removes the need to edit the configuration file.

To enable a module from the command line, enter the command below followed by the module’s name to enable.

For example, to enable rewrite, use the command:

sudo lighttpd-enable-mode rewrite

Once you invoke the lighttpd-enable-mod command, the utility will create a symlink to the module’s config file. Modules enabled are located in the /etc/lighttpd/conf-enabled directory.

For example, enable the auth module as:

$ sudo lighttpd-enable-mod auth

To view the enabled modules, list the files in the directory as:

$ sudo ls -la /etc/lighttpd/conf-enabled

Each enabled module ends with .conf, as shown in the example above. To view the available modules, locate the conf-available directory:

Disable Lighttpd modules

To disable modules, you can use the lighttpd-disable-mod command followed by the module’s name to deactivate.

$ sudo lighttpd-disable-mod auth
Disabling rewrite
Run "service lighttpd force-reload" to enable changes

Enabling Dynamic Content

By default, the Lighttpd server will not serve any dynamic content such as PHP, Ruby, Python, or other supported languages. You need to allow the server to execute scripts by enabling the FastCGI module to solve this.

In this example, we will show you how to enable PHP support on the server.

Start by installing the required packages.

sudo apt install php7.4 php7.4-cgi php7.4-fpm

Next, enable the fastCGI module on the Lighttpd server:

sudo lighttpd-enable-mod fastcgi
sudo lighttpd-enable-mod fastcgi-php

Next, edit the PHP configuration file and change the listen PHP listen port to 9001

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

Locate the following block

listen = /run/php/php7.4-fpm.sock

Change the value to:

listen = 127.0.0.1:9001

Save and close the file.

Restart the fmp service as:

sudo service php7.4-fpm restart

The next step is to edit the FastCGI config file.

sudo nano /etc/lighttpd/conf-enabled/15-fastcgi-php.conf

Locate the following entries:

"bin-path" => "/usr/bin/php-cgi",
"socket" => "/var/run/lighttpd/php.socket"

Change the entries to as shown below:

"host" => "127.0.0.1",
"port" => "9001"

Save and close the file.

Finally, reload the services:

sudo service lighttp restart
sudo service php7.4-fpm restart

That is it for this tutorial.

Closing

In this tutorial, you learned how to set up the Lighttpd server on your machine and enable support for PHP and FastCGI on Lighttpd.

Stay tuned for more tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list