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:
- A fresh install of the Ubuntu/Debian server.
- A root or sudo account.
- 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 upgrade
Proceed to install the Lighttpd package using apt. Lighttpd server is available on the Debian/Ubuntu repositories.
Once the installation process is complete, start the service using systemctl as:
Starting web server: lighttpd.
Check if the Lighttpd service is running:
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:
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:
- server.document-root – Defines the default document root for your web server.
- server.port – the default listen port for the webserver.
- server.username – specifies the username for starting and stopping the server. If not specified, it defaults to the root user.
- server.groupname – the group of the user for managing the server.
- 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.
- server.bind – specifies the IP address or hostname on which the server will listen. By default, the server will bind to all addresses.
- 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:
Standard options in this block include:
- header-strict – if enabled, this option defines the maximum number of characters allowed in an HTTP request header.
- host-strict – this option is similar to header-strict. However, it limits the max characters in an HTTP request Host header.
- 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.
- 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:
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:
To view the enabled modules, list the files in the directory as:
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.
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.
Next, enable the fastCGI module on the Lighttpd server:
sudo lighttpd-enable-mod fastcgi-php
Next, edit the PHP configuration file and change the listen PHP listen port to 9001
Locate the following block
Change the value to:
Save and close the file.
Restart the fmp service as:
The next step is to edit the FastCGI config file.
Locate the following entries:
"socket" => "/var/run/lighttpd/php.socket"
Change the entries to as shown below:
"port" => "9001"
Save and close the file.
Finally, reload the services:
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.