How to Create A Simple Web Server with darkhttpd

Spread the love

Darkhttpd is a lightweight, single-binary web server daemon for Linux. It provides a simple “zero config” way to deploy websites as fast as possible on your server. Here, we show you how to install darkhttpd on Ubuntu Linux, how it stacks up against popular web servers, and guide you through the process of creating your first website.

Content

Darkhttpd vs Apache vs Nginx

Darkhttpd isn’t the only web server you can install on your Linux machine. There are Apache, Nginx, Caddy, Lighttpd and many more. In this section, we’re going to compare darkhttpd against Apache and Nginx and see how it performs.

With regard to ease of use, darkhttpd beats both Apache and Nginx outright. Darkhttpd focuses on keeping everything on a single program. This means you can share your website without worrying about its config and your system’s init service.

In terms of security, darkhttpd uses a chroot for its files. It also has automatic timeouts for idle connections and basic logging. This is a far cry compared to Apache and Nginx. Both of which have a powerful rate-limiting filters and options to fine-tune the HTTP headers on their sites.

Darkhttpd’s simplicity also lends itself inflexible to custom user demands. The program’s developers designed it as a fast way to serve static web content. This means that, compared to Apache and Nginx, darkhttpd can’t run CGI scripts or act as a reverse proxy for your apps.

Category darkhttpd Apache Nginx
Ease of Use Requires no additional config to host websites. Requires both system and site-specific config to work. Requires site-specific config to work.
Overall Security Comes with basic chroot and logging features. Comes with rate limiters and security-focused HTTP headers. Comes with rate limiters and security-focused HTTP headers.
SSL Support No built-in SSL support. Comes with “mod_ssl” for SSL support. Comes with built-in SSL support.
Application Flexibility Can only work with static web content. Can work with both static and dynamic web content. Can work as a web server, load balancer, and reverse proxy.

Good to know: learn how data gets encrypted on the internet by generating self-signed SSL certificates with OpenSSL.

Preparing Your System for darkhttpd

Note: This tutorial is done on an Ubuntu 24.04 VPS with at least 512 MB of RAM. The instructions will work in most Linux distros too.

The first step to create a website with darkhttpd in Ubuntu Linux is to obtain the dependencies for Docker and Docker Compose.

Note: The following will install Docker for Ubuntu-based systems. If you’re coming from a different distro, check out our general guide to installing Docker in Linux.

Start by downloading the signing key for the Docker project:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Use your favorite text editor to create a new repository file:

sudo nano /etc/apt/sources.list.d/docker.list

Paste the following line of code inside your repository file:

deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu noble stable

Reload your machine’s package repositories and update your entire system:

sudo apt update && sudo apt upgrade

Install Docker, its Compose plugin, and Git using apt:

sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-buildx-plugin git

Installing and Running darkhttpd

Go to your home directory, then pull the latest version of darkhttpd to your system:

cd ~ && git clone https://github.com/emikulic/darkhttpd.git

Go to the inside of the Git repo, then create a new Docker Compose file using your favorite text editor:

cd darkhttpd && nano ./docker-compose.yml

Paste the following block of code inside your Compose file:

services:
  darkhttpd:
    container_name: darkhttpd-website
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - "./html:/var/www/htdocs:ro"
    ports:
      - "8080:80"

Save your new Compose file, then run the following command to build and run your darkhttpd Docker container:

sudo docker compose up -d

Create a “html” folder inside the darkhttpd Linux repository, then copy your static website files to it:

mkdir ./html/
cp -rv ~/my-website ./html/

Confirm that darkhttpd is running properly by listing the available containers in the system:

docker ps

On a side note: learn how you can move your existing Docker containers to a new system host.

Running darkhttpd over SSL with stunnel

At this point, you now have a running darkhttpd server at port 8080. However, to access this over the internet, you need to first encapsulate it in SSL through a reverse proxy.

To start, create a new DNS “A” record for your domain pointing to your darkhttpd’s IPv4 address. In my case, I will point my machine’s IPv4 address to the record: “web.myvpsserver.top.”

Fetch and install stunnel, the “core” snap package, and Certbot to your machine:

sudo apt install stunnel4
sudo snap install core && sudo snap install certbot --classic

If you’re using Fedora, you can run the following command to install stunnel and certbot:

sudo dnf install stunnel certbot

Register your darkhttpd instance to the Electronic Frontier Foundation:

sudo certbot register --agree-tos -m YOUR-EMAIL@ADDRESS.HERE

Generate a bare SSL certificate for your domain name:

sudo certbot certonly --standalone -d SUBDOMAIN.YOUR-DOMAIN.ROOT

Create a new configuration file for your stunnel reverse proxy:

sudo nano /etc/stunnel/stunnel.conf

Paste the following block of code inside your new config file:

output       = /var/log/stunnel4/stunnel.log
cert         = /etc/letsencrypt/live/SUBDOMAIN.YOUR-ROOT.DOMAIN/fullchain.pem
key          = /etc/letsencrypt/live/SUBDOMAIN.YOUR-ROOT.DOMAIN/privkey.pem
 
[https]
client       = no
accept       = 443
connect      = 8080

Save your new config file, then run the following command to start the reverse proxy:

sudo systemctl start stunnel4.service

In Fedora, you can run the following command instead to start your stunnel reverse proxy:

sudo systemctl start stunnel.service

Confirm that your static website is now working properly by opening your subdomain on a web browser.

Hosting your own simple website using darkhttpd is just the first step in exploring the wonderful world of self-hosting web services. Learn how you can create RSS Feeds for your social media using RSS-Bridge.

Image credit: Ilya Pavlov via Unsplash. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Ramces Red
Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.

Leave a comment