How to configure virtual hosts on Lighttpd?

0
179

Recently I have taught you how to install Lighttpd on Debian 10. Well, we will take advantage of that installation to also teach you how to create virtual hosts on Lighttpd.

Why create a Virtualhost on Lighttpd?

One of the great advantages of creating a Virtualhost is that we can have in a single server, different sites or web applications.

Then, using our computer we will be able to access several applications with even different names. This is especially useful in servers that have to serve many web applications in a single computer. It is also useful if you are a web developer and need to deploy several applications locally for testing.

So let’s do it.

Install Lighttpd on Debian

The first step is to install and configure Lighttpd on Debian 10.

How to install Lighttpd on Debian 10?

Once you have completed the installation we can continue.

Creating virtual hosts on Lighttpd (I)

The first thing we must do is create a folder for each Virtualhost we want to create. This is basic to have every website or web application apart.

Also, we must take into account that it is recommended that the name of the folder is the same as the name of the virtual host. In my case, I have chosen “www.test.osradar.lan” and “www.test2.osradar.lan“. You can choose whatever you want.

:~$ sudo mkdir -p /var/www/html/www.test.osradar.lan
:~$ sudo mkdir -p /var/www/html/www.test2.osradar.lan

Then, for all applications and websites to run properly, you need to change the permissions to the virtual host folders.

:~$ sudo chmod -R 755 /var/www/html/www.test.osradar.lan
:~$ sudo chmod -R 755 /var/www/html/www.test2.osradar.lan

Also, you have to change the owner of the folders. In the case of Debian, ownership must be given to the user and group www-data.

:~$ sudo chown -R www-data:www-data /var/www/html/www.test.osradar.lan
:~$ sudo chown -R www-data:www-data /var/www/html/www.test2.osradar.lan

To demonstrate the proper functioning of the virtual hosts, create an index.html file in each folder and add the following for the first host:

:~$ sudo nano /var/www/html/www.test.osradar.lan/index.html
<html>
<body>
welcome to www.test.osradar.lan
</body>
</html>

And

:~$ sudo nano /var/www/html/www.test2.osradar.lan/index.html
<html>
<body>
welcome to www.test2.osradar.lan 
</body>
</html>

For the second one.

So, save the changes and close the file.

Creating virtual hosts on Lighttpd (II)

The folders and files are ready, now we have to make some changes for Lighttpd to interpret them.

It’s a good idea to create a folder called vhosts.d in /etc/lighttpd/ and place all the virtual host configuration files there. As with the folders, it’s a good idea to have one file for each virtual host.

:~$ sudo mkdir -p /etc/lighttpd/vhosts.d/

However, for all these changes to be made, the Lighttpd configuration file must be modified. So let’s do it.

:~$ sudo nano /etc/lighttpd/lighttpd.conf

And at the end of the file add the following:

include_shell "cat /etc/lighttpd/vhosts.d/*.conf"
1.- Adding the virtual host configuration directory
1.- Adding the virtual host configuration directory

Save the changes and close the file.

Now we will create a configuration file for each virtual host. And in the first one we will add the following.

:~$ sudo nano /etc/lighttpd/vhosts.d/www.test.osradar.lan.conf
$HTTP["host"] =~ "(^|.)test.osradar.lan$" {
    server.document-root = "/var/www/html/www.test.osradar.lan"
    server.errorlog = "/var/log/lighttpd/www.test.osradar.lan-error.log"
    accesslog.filename = "/var/log/lighttpd/www.test.osradar.lan-access.log"
}
2.- Creating a new virtual host on Lighttpd
2.- Creating a new virtual host on Lighttpd

And for the second:

:~$ sudo nano /etc/lighttpd/vhosts.d/www.test2.osradar.lan.conf
$HTTP["host"] =~ "(^|.)test2.osradar.lan$" {
    server.document-root = "/var/www/html/www.test2.osradar.lan"
    server.errorlog = "/var/log/lighttpd/www.test2.osradar.lan-error.log"
    accesslog.filename = "/var/log/lighttpd/www.test2.osradar.lan-access.log"
}
3.- Creating the second virtual host on Lighttpd
3.- Creating the second virtual host on Lighttpd

Save the changes and close the editor.

You can also use the following command to check the syntax of the configuration files:

:~$ sudo lighttpd -t -f /etc/lighttpd/lighttpd.conf

The result should be something like:

Syntax OK.

Then, restart lighttpd.

:~$ sudo systemctl restart lighttpd

And that’s it.

To test it, you need to modify some parameters in the client computer.

:~$ sudo nano /etc/hosts

And add the following

[Server_IP] www.test.osradar.lan
[Server_IP] www.test2.osradar.lan

Save the changes and close the editor.

Now open your favorite web browser and go to www.test.osradar.lan and you will see the following:

4.- Testing the first Virtual host
4.- Testing the first Virtual host

Now, go to www.test2.osradar.lan:

5.- The second virtual host
5.- The second virtual host

So, everything is OK. And that is it.

Conclusion

In conclusion, in this post, you have learned how to create virtual hosts using Lighttpd as a web server. This not so well known server is useful to serve heavy applications without problems.

Please share this post and join our Telegram channel.

LEAVE A REPLY

Please enter your comment!
Please enter your name here