Lighttpd Server with SSL

Lighttpd is one of the speediest webserver which will make you feel like you got a highspeed internet connection with low price. However , i will explain you how to create ssl certificate and use it in lighttpd vhost for accessing particular websites with particular certificate.

Imagining you have your websites document root in /var/www/ you can create multiple sites with help of this article.

If you have used /var/www/ as document root you will have created two folders web and logs in there where logs will keep all error logs and web will keep all your websites files. Now for ssl certificate you will need another folder named ssl.

mkdir /var/www/

Now create certificate and key.

sudo openssl req -x509 -nodes -days 1460 -newkey rsa:2048 -keyout /var/www/ -out /var/www/

You will be asked for some information. You will have to write your domain name in Common Name Section which you want as https or ssl like below.


Certificate creation is OK but if you want to get your self signed certificate valid for a long time you have to change above lines orange colored numbers. 1460 days means 4 years. If you want to get validity of that certificate of around 20 years you need to put there 7300 that means 365×20.

Now we will need to combine both key and crt file to act as one.

cat /var/www/ /var/www/ > site1.pem

You can create pem file that means combined certificate by one command.

openssl req -new -x509 -keyout /var/www/ -out /var/www/ -days 1460 -nodes

Now let us use that certificate in our sites vhost.

Edit /etc/lighttpd/ :

nano /etc/lighttpd/

Make sure it looks like below :

$HTTP["host"] =~ "(^|.)$" {
        server.document-root = "/var/www/"
        server.errorlog = "/var/www/"
        ssl.engine = "enable"
        ssl.pemfile = "/var/www/"

Now save the file and restart lighttpd:

service lighttpd restart

Now you can access your sites by ssl certificate or https.

If You want to redirect all http to https You have to add below lines on the configuration and restart lighttpd server.

$HTTP["scheme"] == "http" {
    $HTTP["host"] == "" {
            url.redirect = (".*" => "$0")

Now restart and check the website.

service lighttpd restart

Above configuration will only work when you will set “443” port mentioned in lighttpd default configuration.

Let your servers host-name or FQDN is “server1.domain.tld”. Now create a folder in /etc/lighttpd/ssl

mkdir /etc/lighttpd/ssl

Now create an ssl keu+crt or directly pem file there by using FQDN server1.domain.tld

sudo openssl req -x509 -nodes -days 1460 -newkey rsa:2048 -keyout /etc/lighttpd/ssl/server.key -out /etc/lighttpd/ssl/server.crt

Now combine the key+crt to pem file

cat /etc/lighttpd/ssl/server.key /etc/lighttpd/ssl/server.crt > server.pem

Now open lighttpd config file.

nano /etc/lighttpd/lighttpd.conf

and put below codes before “include” of other added domains.

include_shell "/usr/share/lighttpd/"
include_shell "/usr/share/lighttpd/"

$SERVER["socket"] == ":443" {
     ssl.engine = "enable"
        ssl.pemfile = "/etc/lighttpd/ssl/server.pem"

include ""
include ""

You can also do this by a different way. Here is a perfect http to https config for a ghost blog.

$HTTP["host"] =~ "(^|.)DOMAIN.TLD$" {

server.document-root = "/var/www/DOMAIN.TLD/web/"
cache.enable = "enable"

$SERVER["socket"] == ":443" {
ssl.engine = "enable" = "/var/www/DOMAIN.TLD/ssl/DOMAIN.TLD.crt"
ssl.pemfile = "/var/www/DOMAIN.TLD/ssl/DOMAIN.TLD.pem" = "DOMAIN.TLD"

$SERVER["socket"] == ":80" {
        $HTTP["host"] =~ "(.*)" {
                url.redirect = ( ".*" => "https://%0" )
#You can avoide below for normal websites like wordpress,drupal,joomla,normal html-php etc because below lines are for ghost blog.
proxy.server  = ( "" => (
     ( "host" => "111.222.333.444", "port" => "XXXX")

Now you will be able to add multiple domains with different certificate for each.

Thank You.