Polls

Topics

Howto: Lighttpd web server setting up virtual hosting

Posted by Vivek on Thursday July 6, 06 @12:13 am

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,

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 stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in...

Discussion on This Article:

  1. Calvin Chan Says:

    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 Says:

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

  3. rxKaffee Says:

    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 Says:

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

    Thanks.

  5. kunal Says:

    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 Says:

    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 Says:

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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

~ Last updated on: October 9, 2006

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.