Howto: Lighttpd web server setting up virtual hosting

Lighttpd logo

Last time I wrote about setting and installing lighttpd web server.

Virtual hosting allows sharing a lighttpd web server so that you can have multiple domains hosted on single web server. For example:
=> www.nixcraft.com
=> www.cyberciti.biz
=> www.theos.in etc

Our setup

Lighttpd supports both Name-based and IP-based virtual hosts. Let us see how to configure two web sites for:
=> www.nixcraft.com
=> www.theos.in

First create a directory for each domain:

# mkdir -p /home/lighttpd/nixcraft.com/http
# mkdir -p /home/lighttpd/theos.in/http
# chown lighttpd:ftpuser1 /home/lighttpd/nixcraft.com/http
# chown lighttpd:ftpuser2 /home/lighttpd/theos.in/http

Replace ftpuser1 and 2 with actual ftp username.

Also create a log directory for each domain:
# mkdir /var/log/lighttpd/nixcraft.com
# mkdir /var/log/lighttpd/theos.in

Only allow a web server to access our logs:
# chown -R lighttpd:lighttpd /var/log/lighttpd

Open lighttpd configuration file:
# vi /etc/lighttpd/lighttpd.conf

Add support for domain nixcraft.com:
$HTTP["host"] =~ "(^|\.)nixcraft\.com$" {
server.document-root = "/home/lighttpd/nixcraft.com/http"
server.errorlog = "/var/log/lighttpd/nixcraft/error.log"
accesslog.filename = "/var/log/lighttpd/nixcraft/access.log"
server.error-handler-404 = "/e404.php"
}

Add support for domain theos.in:
$HTTP["host"] =~ "(^|\.)theos\.in$" {
server.document-root = "/home/lighttpd/theos.in/http"
server.errorlog = "/var/log/lighttpd/theos.in/error.log"
accesslog.filename = "/var/log/lighttpd/theos.in/access.log"
server.error-handler-404 = "/e404.php"
}

Where,

  • $HTTP["host"] =~ "(^|\.)theos\.in$" : It will match request for both www.theos.in and theos.in domain
  • server.document-root = "/home/lighttpd/theos.in/http" : Server document root. You must set ftp/ssh user home directory to this root only
  • server.errorlog = "/var/log/lighttpd/theos.in/error.log" : Server access log file
  • accesslog.filename = "/var/log/lighttpd/theos.in/access.log": Server error log file
  • server.error-handler-404 = "/e404.php" : Web server error 404 handler file

Save and close the file. Restart the server:
# /etc/init.d/lighttpd restart

Upload file(s) to respective web-root and test your configuration with a web browser.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 14 comments… read them below or add one }

1 Calvin Chan 05.15.07 at 6:56 am

Hi,

I have been trying to setting up this virtual host for my server. However, I am running into this problem where the error log is not stored in the right directory. I have followed everything in your guide. The only difference is that instead of using host, I am using ip based address. It seems like the error will store in the the directory that list ed last first.

For example:

$SERVER["socket"] == “a.b.c.d:80″ {
server.document-root = “/var/www/html/a.b.c.d/”
server.errorlog = “/var/log/lighttpd/a.b.c.d/error.log”
}

$SERVER["socket"] == “1.2.3.4:80″ {
server.document-root = “/var/www/html/1.2.3.4/”
server.errorlog = “/var/log/lighttpd/1.2.3.4/error.log”
}

The error will store in 1.2.3.4 directory. Do you know if there’s a solution to that? Any help would be greatly appreciated. Thanks in advance.

Calvin

2 vivek 05.15.07 at 12:49 pm

I’m sorry but I don’t get your problem. Can you explain more..???

3 rxKaffee 05.23.07 at 7:53 pm

It seems that you can only define server.errorlog once in lighty configuration. ie. one errorlog for the whole server and all vhosts.

4 ligesh 07.03.07 at 4:41 am

Yes. The server.errorlog doesn’t work on a per virtualhost basis. Is that a bug or by design?

Thanks.

5 kunal 07.22.07 at 2:29 pm

Please check the permissions of your log files

is the user is root or lighttpd or the user u have created.

Must note that logfolder/files have lighttpd as owner not root

6 Jain 07.23.07 at 10:15 am

Hi after all the settings done as aforesaid when i try to run my site in browser it give me an error while if i open my site trough IP of the server then it vl open

in server.bind i m using IP address and and i m trying to open my site http://www.ques.example.com.
server.port = 90

when in browser i type http://1.2.3.4:90 it will open my default page

but if i type http://www.ques.example.com then gives me error problem loading page.

How to handle this error.
pls help me

7 Jain 07.23.07 at 10:22 am

opening site in browser with port number
http://www.ques.example.com:90

8 shiato han 07.17.08 at 9:05 am

can lighttpd do this kind of virtual hosting (www.domain.com/~user/)?

shiato
linux newbie

9 Terry 07.21.08 at 2:55 pm

Regular reader of articles. Just one thing I can’t get sorted out here. What I haven’t done yet that is preventing Lighttpd from running. Error: “can’t find name apache” A clue to sort this out would be great!

10 Roon 10.27.08 at 12:59 pm

Thanks for this, I have migrated from apache to lighttpd on my VPS and it has meant that I don’t have to upgrade my VPS memory any more!

Saved me some money!

11 Dan Wincet 01.10.09 at 5:04 am

Calvin Chan, rxKaffee, and ligesh are right, lighttpd only uses the server.errorlog which appears last in the config file, regardless of any conditionals.

i wish you would test your config to make sure it works before posting bogus “tutorials” like this.

12 dreams 04.08.09 at 4:43 pm

I am all confused. I have installed lighttpd on my embedded linux box. If this box is connected to the pc via USB, I am able to access the webserver. But if I configure the server for some static ip, say a.b.c.d and try to access it by typing http://a.b.c.d:80, I do not get anything at all.

Do we have to do anything (some change in the .conf file). What is ipv6 and ipv4 all about?

13 Hitur Petur 06.17.09 at 11:33 am

A quick note why might this exacmple not match.
$HTTP["host"] =~ “(^|\.)nixcraft\.com$” {

http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:Configuration
A port number might be appended to the end…so..

$HTTP["host"] =~ “(^|\.)nixcraft\.com(\:[0-9]*)?$”

Is probably a better guess. This worked for me. Cheers.

14 firmit 06.21.09 at 6:47 am

I had to add my virtual hostname in /etc/hosts as well:
127.0.0.1 localhost
127.0.1.1 vhost1 # which is defined in /etc/lighttp/vhosts.conf

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Ubuntu Linux Live CD: Save data & desktop information on USB device

Next post: Moving Outlook email (data) to Linux Evolution mail client