Setting up Lighttpd for Simple Sites

Mon, 21/01/2019 - 10:29 -- James Oakley
Lighttpd

From time to time, I want to run a simple website on a server as cheaply and simply as possible.

That usually means running on a VPS (Virtual Private Server) with as little memory as possible. It therefore means not using a web hosting control panel such as cPanel, because that comes with its own RAM requirements (usually, 1 GB as an absolute minimum) and may have license fees as well.

To do this, I've become increasingly fond of Debian (because its aptitude package-installer is more light-footed than Centos' yum equivalent). And then of using lighttpd as web serving server. Compared to Apache, lighttpd is faster, uses less memory, and is easier to set up on a simple server with only a few sites. (Lighttpd is pronounced 'lighty').

It doesn't have an easy equivalent for Apache's mod_rewrite, and has no equivalent for .htaccess files. It handles rewriting fully, but you have to hardcode the rewrite rules you want into the configuration files. For a complex site, this requires some skill, so this may not be the best software for a complex CMS with search-engine friendly rewriting throughout. That said, the very fact that all the configuration is hard-coded at startup is part of what makes it fast. It can handle even the most complex of sites. For instance, there is documentation on the Drupal site explaining how to run a full Drupal site on lighttpd. I've done it, and it's fast.

I'm going to offer a simple guide as to how to get up and running on lighttpd on Debian, to run a handful of sites on a small server. It will cover the things you want to do most frequently, but won't cover more complex use cases.

Preliminaries

First of all, there are several things you need to do that will not be covered in this guide.

  • Install Debian (or your preferred flavour of Linux; if it doesn't use aptitude, some of the rest of this guide may need modifying)
  • Install MySQL / MariaDB if your sites will be using it.
  • Install PHP, either manually (download, configure, make, make install) or from a repository.

Note 1: If you install PHP from the repository, run apt-get install php-cli php-cgi (or whichever available version you want) rather than simply apt-get install php, as this latter also installs Apache as a dependency which we don't want.

Note 2: On Debian the MySQL socket is located, by default, at /var/run/mysqld/mysqld.sock; you may need to put a line with the mysqli.default_socket directive into your php.ini.

Install Lighttpd

Next, install lighttpd.

This is easy.

apt-get install lighttpd

The main configuration file gets put in /etc/lighttpd/lighttpd.conf, and the root directory for files to be served by the webserver is /var/www/html.

If all you want to do is run static html sites, accessed by URLs on the hostname of your VPS, then make sure you have an A record pointing from the VPS hostname to the VPS IP, and you're done.

However you probably want to do some of the following additional steps.

Allow Lighttpd to Process PHP Pages

If you want lighttpd to serve more than static .html pages, you need to tell it how. This includes using PHP to process the code in a .php page, and serving up the result.

The way to do this is to configure lighttpd to use FastCGI as its PHP handler. There are two steps to this.

Step 1: Enable FastCGI in your lighttpd configuration:

lighty-enable-mod fastcgi

Within the /etc/lighttpd folder, there are two subdirectories. One is named conf-available, which contains templates for a number of additional configuration files you may want to use. These, if needed, can be edited to make them work exactly how you want. The other is named conf-enabled; these are the additional configuration files that lighttpd is actually parsing and using. Those configuration filenames all start with a number, to ensure they load in the correct order (where that matters).

When you run the above line, lighttpd creates a symlink from /etc/lighttpd/conf-enabled/10-fastci.conf to /etc/lighttpd/conf-available/10-fastcgi.conf. Now you've enabled the fastcgi module, the corresponding configuration file is now enabled too, in its default form. You can edit this, by editing the version in conf-enabled (or the version in conf-available - they're the same as this is a symbolic link).

Step 2: Edit the FastCGI configuration to point PHP files to your PHP installation:

Edit /etc/lighttpd/conf-enabled/10-fastci.conf and add the following lintes at the end:

fastcgi.server = ( ".php" => ((
  "bin-path" => "/usr/bin/php-cgi",
  "socket" => "/tmp/php.socket"
)))

You can then fun /etc/init.d/lighttpd force-reload to load these new settings.

If you're settings were wrong, lighttpd will have crashed. You can easily check: systemctl status lighttpd will tell you if it's still running. Assuming it didn't crash, you can test it by placing a file at /var/www/html/info.php containing this:

<?php
phpinfo();

Go to http://vpshost.example.com/info.php in your browser (substituting vpshost.example.com for the fully qualified hostname of your server) and you should see the output of phpinfo.

Note: If you want PHP errors to be reported, you have to specify the full path of the error log file. You are probably used to php creating a file named, by default, error_log in the home directory of the website in question. With lighttpd, you need to create a directive in php.in ithat specifies a full path, e.g.

  error_log = /var/log/lighttpd/php_error_log

Enable Access Logging

If you want lighttpd to log visits to your website(s) you need to enable the module responsible for this.

This is similar to enabling FastCGI (above), except there is no further configuration required beyond simply enabling it:

lighty-enable-mod accesslog

/etc/init.d/lighttpd force-reload

Log entries will appear in /var/log/lighttpd/access_log. You can alter this in /etc/lighttpd/conf-enabled/10-accesslog.conf if you wish.

If you're running more than one website on this server, you may want to log each website in a different file. That comes under virtual host settings, which we're about to get to.

Virtual Hosts: Getting Set Up

There are no additional modules to enable for this; you simply creative a configuration section for each virtual host that only applies when the HTTP host is what you need, and put the necessary directives in there. I'm about to explain how to do that.

Firstly, let's organise the files a little. You could put all of this into /etc/lighttpd/lighttpd.conf. However it potentially makes for a large configuration file. If we instead put each virtual host into its own file it makes it easy to manage:

  • Individual virtual hosts can be disabled by changing the name of the file to one that lighttpd won't pick up.
  • Backing up the files necessary to host a given virtual host can simply include the appropriate configuration file as well (more on backups in a later post).
  • Subsequent virtual hosts can be set up by copying the configuration file for an existing one, and carefully amending the relevant lines. This is (arguably) easier than copying the required section of a large file.

Three steps to get this set up:

  1. Create a new directory called /etc/lighttpd/vhosts.
  2. Create an empty file in there named blank.conf. (We're about to ask lighttpd to include all the configuration directive contained in every .conf file in our new directory. It needs to find at least one such file, or it will throw an error). You can delete this once you have an actual virtual host conf file
  3. Add this line to the end of your /etc/lighttpd/lighttpd.conf file:

  include_shell "cat vhosts/*.conf"

Once again, run /etc/init.d/lighttpd force-reload to make sure the new configuration directives take effect.

Virtual Hosts: How to add each one

Let's say you want to host a website at the domain subdomain.example.com. Let's say you've created an account on your server with username 'example', home directory /home/example, and a folder called /home/example/public_html that will hold the files for your website.

Here's the minimum configuration you need. Put this in a file named /etc/lighttpd/vhosts/subdomain.example.com.conf.

$HTTP["host"] =~ "^subdomain\.example\.com$" {
  server.document-root = "/home/example/public_html"
  server.errorlog = "var/log/lighttpd/subdomain.example.com/error.log"
  accesslog.filename = "/var/log/lighttpd/subdomain.example.com/access.log"
}

A few things need explaining

  • The braces ({ … }) mark a section of your configuration that only applies when the HTTP host is subdomain.example.com.
  • =~ means that the host will be matched using regular expressions. You could use == instead to match exactly, but for reasons that will become clear regular expressions are more versatile so advised at this stage. See the section on <operator> in the official docs.
  • Because we're using regular expressions, the dots in subdomain.example.com need escaping to match the literal '.' character, as opposed to 'any character'. This is why you see '\.' instead of just '.'
  • The ^ and the $ mean the beginning and the end of the host, and ensure that you match the whole host. For example, without the '^', this section would also apply to new-subdomain.example.com, which is probably not the behaviour you want.
  • If you wanted to match domain.com and www.domain.com, this is the kind of thing that's easy with regular expressions: HTTP["host"] =~ "^(www\.)?domain\.com".

You'll see we've put the logs in a directory especially for this virtual host. Lighttpd won't load this configuration until that directory exists and is writeable to the server:

mkdir -p /var/log/lighttpd/subdomain.example.com
chown www-data.www-data /var/log/lighttpd/subdomain.example.com

You should now be able to run /etc/init.d/lighttpd force-reload, and away you go!

Other Steps

There is lots more you can do with lighttpd:

  • Set up access control to use HTTP basic auth.
  • Restrict access to certain IP addresses.
  • Serve https websites.
  • Set up 404 pages

and so on.

Some of these may be the basis of future guide pages, but I hope this has been helpful.

Blog Category: 

Add new comment

Additional Terms