Skip to content

How to Setup VirtualHosts in Lighttpd Server

Steps
4
Category
Web Servers
Covers
Apache
On this page 4 steps

VirtualHosting is an implementation of hosting multiple domains on single server. It enabled to utilize maximum resources of server and reduces cost. Now a days mostly all web servers supports virtualhosting environment. In our earlier article we describe to install Lighttpd server on CentOS/RHEL. This article will help you to setup VirtualHosts in Lighttpd server.

For example we are using following domains-

  1. site1.tecadmin.net
  2. site2.tecadmin.net

Step 1 of 4Create Server Document Root

First create folders for both domains (if not exists)

# mkdir -p /sites/vhosts/site1.tecadmin.net/www
# mkdir -p /sites/vhosts/site2.tecadmin.net/www

For testing purpose we are creating an index.html file at both document roots

# echo "Welcome to Site1" > /sites/vhosts/site1.tecadmin.net/www/index.html
# echo "Welcome to Site2" > /sites/vhosts/site2.tecadmin.net/www/index.html

Step 2 of 4Update Main Configuration File

Now edit Lighttpd main configuration file /etc/lighttpd/lighttpd.conf and enable to include virtualhosts files. Uncomment following line by removing starting # symbol.

include_shell "cat /etc/lighttpd/vhosts.d/*.conf"

Step 3 of 4Create VirtualHost Configuration Files

Now start creating virutalhost configuration file for both domains or subdomains, First creating for site1.tecadmin.net

# vim /etc/lighttpd/vhosts.d/site1.tecadmin.net.conf
$HTTP["host"] == "site1.tecadmin.net" {

        server.document-root = "/sites/vhosts/site1.tecadmin.net/public"
        server.errorlog = "/var/log/lighttpd/site1.tecadmin.net.error.log"
        accesslog.filename = "/var/log/lighttpd/site1.tecadmin.net.access.log"
}

Now create configuration file for site2.tecadmin.net –

# vim /etc/lighttpd/vhosts.d/site2.tecadmin.net.conf
$HTTP["host"] == "site2.tecadmin.net" {

        server.document-root = "/sites/vhosts/site2.tecadmin.net/public"
        server.errorlog = "/var/log/lighttpd/site2.tecadmin.net.error.log"
        accesslog.filename = "/var/log/lighttpd/site2.tecadmin.net.access.log"
}

Step 4 of 4Verify Configuration and Restart Lighttpd

After making all above configuration, first verify the syntax of all configuration file including main configuration file with

# lighttpd -t -f /etc/lighttpd/lighttpd.conf

Syntax OK

If you found that all syntax is ok, lets restart service

# service lighttpd restart

Now test your both domain in browser and check that you are getting correct content on pages as created in step 1. For more details about VirtualHosts in Lighttpd visit its official site.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

  1. include_shell “cat /etc/lighttpd/vhosts.d/*.conf”

    echo “Welcome to Site1” > /sites/vhosts/site1.tecadmin.net/www/index.html

    vhosts.d does not exist ?
    include_shell “cat /etc/lighttpd/vhosts.d/*.conf”

    what was created was ………….
    mkdir -p /sites/vhosts/site1.tecadmin.net/www

    SO … are we looking at “vhost.d” OR “vhosts”

    regards,
    confused of croydon

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top