How to Install Lighttpd on Ubuntu / Debian

Posted on

How to Install Lighttpd on Ubuntu / Debian

How to Install Lighttpd on Ubuntu / Debian

Lighttpd is a lightweight web server prized for its speed, minimal resource consumption, and straightforward configuration. This makes it an excellent choice for hosting small to medium-sized websites, embedded systems, or serving as a reverse proxy to enhance the performance of larger, more complex web applications. Its event-driven architecture allows it to handle numerous concurrent connections efficiently.

This article provides a step-by-step guide on how to install and configure Lighttpd on a Debian or Ubuntu server. We’ll walk you through the installation process and demonstrate how to set up Lighttpd to serve a basic website. By the end of this tutorial, you’ll have a functioning Lighttpd server ready to host your web content.

Prerequisites

Before proceeding with the installation, ensure you have the following:

  • A Debian or Ubuntu server (a virtual machine or physical server).
  • A user account with sudo privileges.
  • A stable internet connection.

Step 1: Update the system packages

The initial step involves updating your system’s package list and upgrading any outdated packages. This ensures you have the latest versions of software and dependencies, minimizing potential conflicts during the Lighttpd installation.

Open a terminal and execute the following commands:

$ sudo apt update
$ sudo apt upgrade

The apt update command refreshes the package list from the repositories. The apt upgrade command then upgrades any installed packages to their newest versions. Respond with "y" when prompted to confirm the upgrade.

Step 2: Install Lighttpd

With the system packages updated, you can now install Lighttpd itself. Use the apt install command:

$ sudo apt install lighttpd

This command downloads and installs the Lighttpd package along with any required dependencies. You’ll be prompted to confirm the installation; type "y" and press Enter.

Step 3: Configure Lighttpd

After the installation, you’ll need to configure Lighttpd to serve your website. The main configuration file is located at /etc/lighttpd/lighttpd.conf. You can modify this file to customize Lighttpd’s behavior.

For a basic website setup, you’ll typically want to define the document root, which specifies the directory where your website files are stored.

Open the /etc/lighttpd/lighttpd.conf file with a text editor (e.g., nano, vim):

$ sudo nano /etc/lighttpd/lighttpd.conf

Locate the server.document-root directive (or add it if it doesn’t exist) and set its value to the desired directory. For example:

server.document-root = "/var/www/html"

This line tells Lighttpd to serve files from the /var/www/html directory.

You can also change the port that Lighttpd listens on. By default, it uses port 80 (the standard HTTP port). To change it to, for example, port 8080, add the following line to the lighttpd.conf file:

server.port = 8080

After making changes to the configuration file, save it and exit the text editor.

Next, create the document root directory if it doesn’t already exist:

$ sudo mkdir /var/www/html

Now, create a simple HTML file in the document root directory. This will serve as your website’s index page. For example, create a file named index.html with the following content:

<!DOCTYPE html>
<html>
    <head>
        <title>My Website</title>
    </head>
    <body>
        <h1>Welcome to my website!</h1>
    </body>
</html>

Step 4: Start Lighttpd

With the configuration set up and the website files in place, you can now start the Lighttpd service.

$ sudo systemctl start lighttpd

This command starts the Lighttpd web server. You can also enable Lighttpd to start automatically on boot:

$ sudo systemctl enable lighttpd

Step 5: Test the website

To verify that Lighttpd is working correctly, open a web browser and navigate to the server’s IP address or domain name. If you changed the port to 8080, you’ll need to include the port number in the URL (e.g., http://192.168.1.100:8080).

For example, if the server’s IP address is 192.168.1.100 and you’re using the default port 80, you would open a web browser and navigate to http://192.168.1.100.

If everything is configured correctly, you should see the "Welcome to my website!" message from the index.html file you created.

<!DOCTYPE html>
<html>
    <head>
        <title>My Website</title>
    </head>
    <body>
        <h1>Welcome to my website!</h1>
    </body>
</html>

Conclusion

This article has guided you through the process of installing and configuring Lighttpd on a Debian or Ubuntu server. You’ve learned how to update system packages, install Lighttpd, configure the document root, start the service, and test the website. Lighttpd is now ready to serve your web content.

For more in-depth information and advanced configuration options, refer to the official Lighttpd website: https://www.lighttpd.net/.

Alternative Solutions

While the above method provides a standard approach to installing and configuring Lighttpd, there are alternative solutions that might be more suitable depending on your specific needs and preferences. Here are two such alternatives:

1. Using Docker

Docker allows you to containerize applications, providing a consistent and isolated environment. You can use a pre-built Lighttpd Docker image or create your own. This approach simplifies deployment and ensures that Lighttpd runs the same way regardless of the underlying operating system.

Explanation:

Docker containers encapsulate an application and its dependencies into a single package. This eliminates the "it works on my machine" problem and makes it easier to deploy Lighttpd across different environments. Using a Docker image for Lighttpd means you don’t have to manually install and configure it on your host system.

Code Example:

First, install Docker on your Ubuntu/Debian system, if you haven’t already:

sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker

Then, use the following docker-compose.yml file (or a similar Docker configuration) to run Lighttpd:

version: "3.8"
services:
  lighttpd:
    image: linuxserver/lighttpd
    container_name: lighttpd
    ports:
      - "80:80"
    volumes:
      - ./www:/config/www
    restart: unless-stopped

In this example:

  • image: linuxserver/lighttpd specifies the Docker image to use (a popular, well-maintained Lighttpd image).
  • ports: - "80:80" maps port 80 on the host machine to port 80 in the container.
  • volumes: - ./www:/config/www mounts the ./www directory on the host machine to /config/www inside the container, where Lighttpd expects to find website files.

Create a directory named www in the same directory as your docker-compose.yml file and place your index.html (or other website files) inside it. Then, run the following command to start the container:

docker-compose up -d

This will download the Lighttpd image (if it’s not already present) and start the container in detached mode. You can then access your website at http://localhost (or the server’s IP address).

2. Using Ansible for Automated Configuration

Ansible is an automation tool that allows you to define infrastructure as code. You can create an Ansible playbook to automate the installation and configuration of Lighttpd, ensuring consistent deployments across multiple servers.

Explanation:

Ansible playbooks describe the desired state of your system. They contain tasks that perform specific actions, such as installing packages, configuring files, and starting services. Using Ansible to manage Lighttpd installation allows for repeatable and consistent deployments, especially useful when managing multiple servers.

Code Example:

First, install Ansible on your control machine:

sudo apt update
sudo apt install ansible

Then, create an Ansible playbook named lighttpd.yml:

---
- hosts: all
  become: true
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
    - name: Install lighttpd
      apt:
        name: lighttpd
        state: present
    - name: Configure lighttpd document root
      lineinfile:
        path: /etc/lighttpd/lighttpd.conf
        regexp: '^server.document-root'
        line: 'server.document-root = "/var/www/html"'
      notify: restart_lighttpd
    - name: Ensure document root directory exists
      file:
        path: /var/www/html
        state: directory
        owner: www-data
        group: www-data
        mode: '0755'
    - name: Copy index.html
      copy:
        src: index.html
        dest: /var/www/html/index.html
        owner: www-data
        group: www-data
        mode: '0644'
    - name: Enable lighttpd service
      systemd:
        name: lighttpd
        enabled: yes
        state: started
  handlers:
    - name: restart_lighttpd
      systemd:
        name: lighttpd
        state: restarted

In this playbook:

  • hosts: all specifies that the playbook should run on all hosts defined in your Ansible inventory.
  • become: true indicates that tasks should be executed with sudo privileges.
  • The apt tasks install the Lighttpd package.
  • The lineinfile task configures the document root in /etc/lighttpd/lighttpd.conf.
  • The file task ensures that the document root directory exists.
  • The copy task copies your index.html file to the document root.
  • The systemd tasks enable and start the Lighttpd service.
  • The handlers section defines a handler to restart Lighttpd when the configuration file changes.

Before running the playbook, ensure you have an index.html file in the same directory as the playbook. Then, run the playbook using the following command:

ansible-playbook lighttpd.yml -i <inventory_file>

Replace <inventory_file> with the path to your Ansible inventory file, which defines the target servers.

Both Docker and Ansible provide more advanced and scalable approaches to deploying and managing Lighttpd, offering benefits such as consistency, automation, and portability. Choose the solution that best fits your infrastructure and deployment needs.