Configure Lighttpd 1.4 with Redis caching for high-performance websites

Intermediate 25 min Apr 04, 2026 197 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up Lighttpd web server with Redis caching to dramatically improve website performance through intelligent content caching, reducing server load and response times for high-traffic applications.

Prerequisites

  • Root or sudo access
  • Basic Linux command line knowledge
  • Understanding of web server concepts

What this solves

Lighttpd with Redis caching creates a high-performance web serving stack that dramatically reduces response times and server load. This combination is ideal for content-heavy websites, API endpoints, and applications that serve the same content to multiple users frequently.

Step-by-step installation

Update system packages

Start by updating your package manager to ensure you get the latest versions of all components.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Lighttpd web server

Install Lighttpd along with essential modules for caching and FastCGI support.

sudo apt install -y lighttpd lighttpd-mod-webdav
sudo dnf install -y lighttpd lighttpd-fastcgi

Install Redis caching server

Install Redis server which will serve as the caching backend for Lighttpd.

sudo apt install -y redis-server redis-tools
sudo dnf install -y redis redis-tools

Install Redis module for Lighttpd

Install the Redis module that allows Lighttpd to communicate with Redis for caching operations.

sudo apt install -y lighttpd-mod-trigger-b4-dl
sudo dnf install -y lighttpd-mod_trigger_b4_dl

Configure Redis for optimal caching

Configure Redis with settings optimized for web caching, including memory limits and eviction policies.

# Set maximum memory for cache (adjust based on your system)
maxmemory 256mb
maxmemory-policy allkeys-lru

Enable persistence for cache recovery

save 900 1 save 300 10 save 60 10000

Optimize for web caching

tcp-keepalive 300 timeout 0

Security settings

bind 127.0.0.1 protected-mode yes requirepass your_redis_password_here

Logging

loglevel notice logfile /var/log/redis/redis-server.log

Start and enable Redis service

Enable Redis to start automatically on boot and start the service immediately.

sudo systemctl enable redis-server
sudo systemctl start redis-server
sudo systemctl status redis-server

Create Lighttpd main configuration

Configure Lighttpd with performance optimizations and Redis caching integration.

server.modules = (
    "mod_indexfile",
    "mod_access",
    "mod_alias",
    "mod_redirect",
    "mod_rewrite",
    "mod_compress",
    "mod_expire",
    "mod_setenv",
    "mod_fastcgi",
    "mod_trigger_b4_dl"
)

Basic server settings

server.document-root = "/var/www/html" server.upload-dirs = ("/var/cache/lighttpd/uploads") server.errorlog = "/var/log/lighttpd/error.log" server.pid-file = "/var/run/lighttpd.pid" server.username = "www-data" server.groupname = "www-data" server.port = 80

Performance tuning

server.max-connections = 1024 server.max-fds = 2048 server.max-worker = 4 server.max-request-size = 2048

Index files

index-file.names = ("index.php", "index.html", "index.htm", "default.htm")

MIME types

mimetype.assign = ( ".pdf" => "application/pdf", ".sig" => "application/pgp-signature", ".spl" => "application/futuresplash", ".class" => "application/octet-stream", ".ps" => "application/postscript", ".torrent" => "application/x-bittorrent", ".dvi" => "application/x-dvi", ".gz" => "application/x-gzip", ".pac" => "application/x-ns-proxy-autoconfig", ".swf" => "application/x-shockwave-flash", ".tar.gz" => "application/x-tgz", ".tgz" => "application/x-tgz", ".tar" => "application/x-tar", ".zip" => "application/zip", ".mp3" => "audio/mpeg", ".m3u" => "audio/x-mpegurl", ".wma" => "audio/x-ms-wma", ".wax" => "audio/x-ms-wax", ".ogg" => "application/ogg", ".wav" => "audio/x-wav", ".gif" => "image/gif", ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png", ".xbm" => "image/x-xbitmap", ".xpm" => "image/x-xpixmap", ".xwd" => "image/x-xwindowdump", ".css" => "text/css", ".html" => "text/html", ".htm" => "text/html", ".js" => "text/javascript", ".asc" => "text/plain", ".c" => "text/plain", ".cpp" => "text/plain", ".log" => "text/plain", ".conf" => "text/plain", ".text" => "text/plain", ".txt" => "text/plain", ".dtd" => "text/xml", ".xml" => "text/xml", ".mpeg" => "video/mpeg", ".mpg" => "video/mpeg", ".mov" => "video/quicktime", ".qt" => "video/quicktime", ".avi" => "video/x-msvideo", ".asf" => "video/x-ms-asf", ".asx" => "video/x-ms-asf", ".wmv" => "video/x-ms-wmv", ".bz2" => "application/x-bzip", ".tbz" => "application/x-bzip-compressed-tar", ".tar.bz2" => "application/x-bzip-compressed-tar", "" => "application/octet-stream" )

Compression for better performance

compress.cache-dir = "/var/cache/lighttpd/compress/" compress.filetype = ("application/javascript", "text/css", "text/html", "text/plain")

Expire headers for static content

expire.url = ( "/images/" => "access plus 1 months", "/css/" => "access plus 1 weeks", "/js/" => "access plus 1 weeks" )

Configure Redis caching module

Create a separate configuration file for Redis caching integration with Lighttpd.

# Redis caching configuration

Note: This requires custom implementation as Lighttpd doesn't have native Redis support

We'll use trigger_b4_dl for basic caching functionality

Enable trigger before download for cache logic

trigger-before-download.gdbm-filename = "/var/cache/lighttpd/trigger.db" trigger-before-download.memcache-hosts = ( "127.0.0.1:11211" )

Static file caching with etags

static-file.etags = "enable"

Advanced expire rules for different content types

expire.url = ( "/static/" => "access plus 1 months", "/media/" => "access plus 1 months", "/css/" => "access plus 1 weeks", "/js/" => "access plus 1 weeks", "/images/" => "access plus 1 months", "/fonts/" => "access plus 1 months" )

Cache headers for API responses

setenv.add-response-header = ( "Cache-Control" => "public, max-age=3600", "Vary" => "Accept-Encoding" )

Create cache directories with proper permissions

Create necessary cache directories and set appropriate ownership for the web server user.

sudo mkdir -p /var/cache/lighttpd/{compress,uploads}
sudo chown -R www-data:www-data /var/cache/lighttpd
sudo chmod -R 755 /var/cache/lighttpd

Create log directory

sudo mkdir -p /var/log/lighttpd sudo chown www-data:www-data /var/log/lighttpd sudo chmod 755 /var/log/lighttpd
Never use chmod 777. It gives every user on the system full access to your files. Instead, fix ownership with chown and use minimal permissions like 755 for directories and 644 for files.

Enable Redis cache configuration

Enable the Redis cache configuration module in Lighttpd.

sudo lighttpd-enable-mod redis-cache
sudo systemctl reload lighttpd

Create a test website

Create a sample website to test caching functionality with dynamic and static content.




    
    
    Lighttpd + Redis Cache Test
    


    

Lighttpd with Redis Caching

Current time:

This page tests caching functionality.

sudo mkdir -p /var/www/html/{css,js,images}
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 644 /var/www/html/*
sudo find /var/www/html -type d -exec chmod 755 {} \;

Create sample CSS and JavaScript files

Add static assets to test compression and caching of different content types.

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 20px;
    background-color: #f4f4f4;
}

h1 {
    color: #333;
    text-align: center;
}

p {
    line-height: 1.6;
    color: #666;
}

img {
    display: block;
    margin: 20px auto;
    border-radius: 8px;
}
document.addEventListener('DOMContentLoaded', function() {
    const timeElement = document.getElementById('time');
    const now = new Date();
    timeElement.textContent = now.toLocaleString();
    
    // Test AJAX request for cache testing
    console.log('Page loaded at: ' + now.toLocaleString());
});

Configure Redis connection script

Create a PHP script to demonstrate Redis integration for dynamic caching.

sudo apt install -y php-fpm php-redis
sudo dnf install -y php-fpm php-redis
connect('127.0.0.1', 6379);
    $redis->auth('your_redis_password_here');
    
    $cache_key = 'page_views';
    $views = $redis->get($cache_key);
    
    if ($views === false) {
        $views = 1;
    } else {
        $views++;
    }
    
    $redis->set($cache_key, $views, 3600); // Cache for 1 hour
    
    header('Content-Type: application/json');
    header('Cache-Control: public, max-age=300'); // Cache for 5 minutes
    
    echo json_encode([
        'page_views' => $views,
        'timestamp' => date('Y-m-d H:i:s'),
        'cached_until' => date('Y-m-d H:i:s', time() + 300)
    ]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Cache connection failed']);
}
?>

Configure PHP-FPM for Lighttpd

Configure FastCGI to handle PHP requests through Lighttpd.

## FastCGI programs have the same functionality as CGI programs,

but are considerably faster through lower interpreter startup

time and socketed communication

fastcgi.server += ( ".php" => (( "bin-path" => "/usr/bin/php-cgi", "socket" => "/var/run/lighttpd/php.socket", "max-procs" => 2, "idle-timeout" => 20, "bin-environment" => ( "PHP_FCGI_CHILDREN" => "4", "PHP_FCGI_MAX_REQUESTS" => "10000" ), "bin-copy-environment" => ( "PATH", "SHELL", "USER" ), "broken-scriptfilename" => "enable" )) )
sudo lighttpd-enable-mod fastcgi-php

Start and enable services

Enable and start both Lighttpd and ensure Redis is running properly.

sudo systemctl enable lighttpd
sudo systemctl start lighttpd
sudo systemctl status lighttpd
sudo systemctl status redis-server

Configure caching performance optimization

Optimize Redis memory settings

Configure Redis for optimal memory usage and cache eviction policies specific to web caching.

redis-cli -a your_redis_password_here CONFIG SET maxmemory-policy allkeys-lru
redis-cli -a your_redis_password_here CONFIG SET maxmemory 512mb
redis-cli -a your_redis_password_here CONFIG REWRITE

Monitor cache performance

Create monitoring scripts to track cache hit rates and performance metrics.

#!/bin/bash

Redis cache statistics

echo "=== Redis Cache Statistics ===" redis-cli -a your_redis_password_here INFO stats | grep -E "keyspace_hits|keyspace_misses|used_memory_human|connected_clients"

Lighttpd status

echo -e "\n=== Lighttpd Status ===" sudo systemctl is-active lighttpd sudo netstat -tlnp | grep :80

Cache directory sizes

echo -e "\n=== Cache Directory Usage ===" du -sh /var/cache/lighttpd/*

Recent access logs

echo -e "\n=== Recent Access Patterns ===" sudo tail -n 10 /var/log/lighttpd/access.log 2>/dev/null || echo "No access log found"
sudo chmod +x /usr/local/bin/cache-stats.sh

Verify your setup

Test the caching functionality and verify both services are working correctly.

# Check service status
sudo systemctl status lighttpd
sudo systemctl status redis-server

Test Redis connection

redis-cli -a your_redis_password_here ping

Test web server response

curl -I http://localhost/ curl -I http://localhost/cache-test.php

Check cache hit statistics

redis-cli -a your_redis_password_here INFO stats | grep keyspace

Monitor cache performance

/usr/local/bin/cache-stats.sh

Test compression

curl -H "Accept-Encoding: gzip" -v http://localhost/ | head

Common issues

SymptomCauseFix
Lighttpd won't startConfiguration syntax errorsudo lighttpd -t -f /etc/lighttpd/lighttpd.conf
Redis connection refusedRedis not running or wrong passwordsudo systemctl start redis-server and check password
Permission denied errorsIncorrect file ownershipsudo chown -R www-data:www-data /var/www/html
PHP files not executingFastCGI module not enabledsudo lighttpd-enable-mod fastcgi-php
Cache not workingCache headers not setCheck expire configuration and cache headers
High memory usageRedis maxmemory not setSet appropriate maxmemory in Redis config

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure performance optimization for businesses that depend on uptime. From initial setup to ongoing operations.