Friday, March 31, 2017

Lighttpd and Simple Virtual Hosts Configuration.

I manage some websites for car clubs I belong to. They had been paying for web hosting and had some volunteers who knew not quite enough administrating the sites. Having some prior experience with these small club sites, I volunteered to host them and admin them.

With the first site, the pages were a train wreck of PHP and making even a small change on the existing pages caused the site to crash. Way back around the turn of the century, I had a few Linux user’s groups that wanted a website, so I worked with Cynthia Manuel of Flamingo Internet Navigators to make a template for a web site that would be easy to maintain and easy to add static content. She developed templates that relied on Server Side Includes (SSI) and Cascading Style Sheets (CSS) to make administration and content additions simple and easy, so I ported all the content over to that template and hosted the site myself. Later, another car club needed the same assistance and I ported it over as well.

The web server I chose initially was the venerable Apache web server. Setting up SSI was a little complicated, but not too bad, but Apache seemed like overkill and virtual hosts were a pain to configure. Enter the Lighttpd webserver.

Configuration for Lighttpd was pretty straightforward until it came time for virtual hosts, so here’s what I did to enable SSI and Virtual Hosts (vhosts) on the Lighttpd webserver. Part of the initial difficulty was that all the documentation available seemingly addressed older versions and it appears that the configuration options have changed quite a bit.

As of March, 2017, this guide applies to Lighttpd version 1.4.45. I currently have it running in a Mageia6 32-bit virtual machine. That makes backups easy and as simple as cloning the machine. As well, it makes moving it to a new host easy.

The HTML files for each website are kept at /var/www/html/xxxx and /var/www/html/yyyy. In this way, if anyone just uses the IP address instead of the domain name, they only get the standard default page: It works!

To configure lighttpd for my use, it’s necessary to enable the modules I will be using by simply editing /etc/lighttpd/modules.conf and un-commenting the entries for SGCI and mod_simple_host.

To create the actual virtual hosts, I created a new directory, /etc/lighttpd/vhosts.conf and in that directory, I created two files, xxxx.conf and yyyy.conf.

In those files, I added the following information:

$HTTP["host"] =~ "(^|\.)xxx\.com$" {
server.document-root = "/var/www/html/xxxx"

$HTTP["host"] =~ "(^|\.)yyyy\.org$" {
server.document-root = "/var/www/html/yyyy"

To make those files available to the webserver, I needed to add the following to /etc/lighttpd/lighttpd.conf

include "vhosts.conf/xxxx.conf"
include "vhosts.conf/yyyy.conf"

There is, of course, much greater flexibility in writing these configuration files and many more features that can be enabled in Lighttpd and they are about as easy as my example. If you need more flexibility with virtual  hosts, it is possible to have the virtual hosts kept in a mysql database.


Lighttpd Homepage

Lighttpd Docs

How to Support Configuration per Virtual Host
A more elaborate procedure to configure multiple virtual host with differing configurations.

No comments: