#004

Quick HTTP server for local development on OpenBSD

Last updated:

Whenever I want to preview my website before pushing it to the hosting server, I run

$ firefox file:///path/to/page/index.html

This worked fine until I decided to use absolute paths in the navigation bar links, e.g., /gallery.html instead of ../gallery.html, causing firefox to look for the file /gallery.html1 instead of /path/to/page/gallery.html. When looking for an alternative, I found the one-liner

$ python3 -m http.server

which can be run from the root directory of the website to serve the files on port 8000 of localhost. This is really cool…, but it requires python. I could try with perl, which is shipped with OpenBSD, but I would probably need to install a module such as HTTP::Server::Simple. So, I turned to see if I could achieve something similar with httpd(8), OpenBSD’s HTTP daemon. It turns out, it is quite simple. It is not a one-liner, but a configuration file like the following is enough:

$ cat ./httpd.conf
chroot "."

server "local" {
    listen on localhost port 8000
    root "."
    no log
}

With this, serving a website locally is as simple as running

$ cd /path/to/page
# httpd -df /path/to/httpd.conf

and navigating to http://localhost:8000 in the browser.


  1. Even if this file did exist, firefox wouldn’t find it as it uses unveil(2) on OpenBSD.