Lighttpd virtualhost configuration ~ name-based virtual hosting

by on October 18, 2007 · 1 comment· LAST UPDATED February 7, 2008

in , ,

Q. How do I configure lighttpd web server as virtual host for serving multiple web site from a single public IP address (name-based virtual hosting)?

A. Virtual hosting is a method that servers such as webservers use to host more than one domain name on the same server, sometimes on the same IP address.

There are two basic methods of accomplishing virtual hosting .
(A) name-based virtual hosting : You use multiple host names for the same webserver IP address. For example domain nixcraft.com and theos.in uses same IP address called 65.111.211.111.

(B) ) IIP address / ip-based virtual hosting

How do I configure Lighttpd for name-based virtual hosting?

Let us say your setup is as follows:

  • Public IP address: 72.12.5.10
  • Domain names: domain1.com and domain2.net
  • Default Document Root: /home/lighttpd/default/http
  • Default Document Root for domain1.com: /home/lighttpd/domain1.com/http
  • Default Document Root for domain2.net: /home/lighttpd/domain2.net/http

First, create required directories:
# mkdir -p /home/lighttpd/default/http
Next, open lighttpd.conf file:
# vi /etc/lighttpd/lighttpd.conf
Setup default document root:
server.document-root = "/home/lighttpd/default/http/"
Setup public IP address:
server.port = 80
server.bind = "72.12.5.10"

At the bottom of the file, add:
include "domain1.com.conf"
include "domain2.net.conf"

Save and close the file.

Create domain1.com virtual host configuration

Create required directories:
# mkdir -p /home/lighttpd/domain1.com/http
# mkdir -p /home/lighttpd/domain1.com/logs

Open /etc/lighttpd/domain1.com.conf file:
# vi /etc/lighttpd/domain1.com.conf
Add following configuration directive:

$HTTP["host"] =~ "domain1\.com" {
            server.document-root = "/home/lighttpd/domain1.com/http"
            accesslog.filename         = "/home/lighttpd/domain1.com/logs/access.log"
}

Save and close the file.

Create domain2.net virtual host configuration

Create required directories:
# mkdir -p /home/lighttpd/domain2.net/http
# mkdir -p /home/lighttpd/domain2.net/logs

Open /etc/lighttpd/domain2.net.conf file:
# vi /etc/lighttpd/domain2.net.conf
Add following configuration directive:

$HTTP["host"] =~ "domain2\.net" {
            server.document-root = "/home/lighttpd/domain2.net/http"
            accesslog.filename         = "/home/lighttpd/domain2.net/logs/access.log"
}

Save and close the file.

Restart the lighttpd web server:
# /etc/init.d/lighttpd restart

Make sure document root is owned by your web server user such as www-data or lighttpd:
# chown -R lighttpd:lighttpd /home/lighttpd/

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 1 comment… read it below or add one }

1 Marki February 25, 2008 at 11:36 pm

It is not a good idea to have document-root owned (or writable) by the webserver user. Then every error in scripts (php/cgi/whatever) can be misused very easily.
Only make document-root readable by the webserver. Add write permission only to dirs which really needed (uploads).

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: