While covering Linux and servers, this blog has touched on many topics. Most of them concern strictly web applications, basic use of the Penguin, and how software files are organized on a server. We have said a little less about the fact that once a site lands on a computer connected to the network, it would be good if users could find it. And we do not mean ranking in Google (SEO, which is an entirely different and very broad topic). What we mean is that given the address of a site, you should simply be able to open it. Contrary to appearances, this is not that obvious, especially on machines that can host many sites.
Some will naturally think of domains and the DNS protocol, and while we will inevitably touch on that subject, it is not the point of this text. We would rather focus on where we ended the previous paragraph: a server can hold many sites, even unrelated ones, and a user wants to reach one particular site. How do we do that?
This is what the mechanism usually called virtual hosts, or vhosts, is for, and that is what we will discuss today. We did so indirectly in the article about using a server for beginners, but today we will expand on the topic and show examples. We will also demonstrate how to reach a site without a domain, or when you want a domain to point to something other than what it actually points to.
What is a host?
Let us start by answering the question of what a host is in the first place, without the adjective "virtual" for now. Those who know English well will say at once that it means someone who receives guests, and this also captures the meaning of the term in computing well. It is a machine or server that stores ("hosts") certain resources, which it can then make available over a network, whether global or local. It can be anything, and it does not have to be a website. It also applies to plain file sharing or to a service such as SSH, for example.
In our context, however, we will most often treat as a host a computer that serves the files of a web application, and not necessarily just one. What matters is that we are talking about a device that actually exists. Note that this is not necessarily "physical". It is possible (and very commonly used) for one computer to create virtual machines that are hosts in their own right. But a "host" that is a virtual machine should not be confused with a "virtual host". That is an entirely different thing.
What is a virtual host and how does an HTTP server handle it?
Imagine we have a server at the IP address 10.20.30.40 that contains an application with an ERP-class system (enterprise resource planning, like ComMan, for example). It is the only system on this machine, and the address MyERP.com leads to it. When a user types this address into the browser, a domain name server tells the computer that the host it is looking for is at 10.20.30.40. The browser then goes to that address, reaching our server, where the HTTP server returns the contents of the site's files.
This scenario is simple, but very... unrealistic. First, few applications today consist of a single component with a single address. This may or may not matter, because when a system is split into a frontend and a backend, the latter is usually hidden from outside access. But not always, which means two applications on one server under different addresses. That is also the second reason: most servers are meant to host many web applications under different addresses using one machine and one HTTP server (that is, the application that serves the sites it manages). On the one hand because it is cheaper that way, and on the other because sometimes a server is "too big" for a single application and can simply be given something else to do so that resources are not wasted.
Let us look at it this way:
A user who wants to visit MyERP.com must first learn the IP address of the server that stores the site. They can do this through one of the available DNS servers (Domain Name Server), which has something like tables mapping domain names to IP addresses. This is how the user's browser learns that the server it is interested in has the "number" 10.20.30.40. With this knowledge the browser goes to that machine, and that is where the role of DNS ends. Its job was to point to the right computer, and only the first time (or once in a while). After that the browser caches this address, which speeds up later trips.
So a request arrives at 10.20.30.40, introducing itself as a request for MyERP.com, and this is extremely important information. The server uses it to recognize to which of the virtual hosts (vhosts for short) defined on it it should route the request, much like a router. Each of them has its own configuration data and a designated folder where it keeps the files that make up the application. It is a tenant, so to speak, in a huge apartment building called the HTTP server. It is also worth noting that typing 10.20.30.40 directly into the address bar does not solve the problem. We will find the server, but it cannot decide which site we mean, and as a result the user gets an error.
Since sometimes it is better to show something with a metaphor, let us try one. Imagine a young man, Chris (the user and their browser), who wants to invite Sophie (the website) to a ball. From his friends (the DNS server) he learns that Sophie's house (the domain name) is at 46 Linden Street (the IP address). So our Chris goes to Linden Street, knocks on the door and after a moment sees a burly man with a stern expression (the HTTP server), who introduces himself as the master of the house. Undeterred, the young man says he has come to see the man's daughter. The man asks, "Which one?" because he has three: Kate, Sophie and Martha (the virtual hosts). Chris says he means Sophie, so her father calls her, and she comes out to the boy and listens to the invitation to the ball.
As you can see, the concept of a virtual host is fairly simple: it is something like one of the named resources on the server that can be reached through a URL. A somewhat harder task awaits those who have to define such virtual hosts, because it is not always easy, especially since it is done differently on every kind of HTTP server.
Apache configuration
This is the second most popular application HTTP server in use, but the one most sought after by beginners (though not only by them, and I will say it outright: there is nothing shameful about that). This comes from its simple configuration (at least compared with its competitors), although you should be aware that it is considered slower than, say, NGINX. Still, everything depends on the nonfunctional requirements of the system to be hosted on such a server. The server itself is often called Apache 2, because the second version has been the leading one for many years. Once I asked an administrator for an Apache server and got Apache version one, even though "two" had been available for many years.
Here virtual hosts are defined in the /etc/apache2/sites-available folder (we use the default Linux paths), usually by creating a separate text file for each vhost. This makes it easier to enable and disable them later.
Below is an example of the file myerp.com.conf, which defines the vhost myerp.com:
<VirtualHost *:80>
DocumentRoot /var/www/myerp.com
ServerName myerp.com
ServerAlias www.myerp.com
ServerAdmin admin@myerp.com
ErrorLog ${APACHE_LOG_DIR}/myerp.com-error.log
CustomLog ${APACHE_LOG_DIR}/myerp.com-access.log common
<Directory /var/www/myerp.com>
Allow from all
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Each definition goes inside a VirtualHost node, giving a specific address and port, or using an asterisk to mean any IP address that led the user to our server anyway. The first two directives are the most important: DocumentRoot sets the location of the application files, while ServerName sets the address that calls up those files. As you can guess from the code, you can also specify an alias, an administrator and the location of the log files.
The Directory node is another important definition. It is not mandatory, but it is used, among other things, to set AllowOverride All, which allows rules to be overridden through .htaccess files in the application itself, so characteristic of the Apache server. Besides that, as you can see, you can also open up access from anywhere.
However, putting the configuration in the sites-available folder is only half the battle. You also have to enable the configuration (or disable it if you no longer need it). There are commands for this:
sudo a2ensite myerp.com.conf sudo a2dissite myerp.com.conf
Configuration changes must be applied by restarting the server itself, in a way that depends on the particular Linux distribution. For example:
sudo systemctl reload apache2
You can find more information in this easy-to-follow tutorial from DigitalOcean.
XAMPP configuration
XAMPP is a well-known tool, especially on Windows, that combines the Apache HTTP server, a MySQL server (although I personally recommend installing that one separately) and a PHP interpreter module. It makes building applications much easier and spares programmers from configuring many parameters themselves.
As I wrote, this package uses the Apache server under the hood, so creating virtual hosts here looks very similar to what we showed above. The difference is that here the default location of the virtual hosts file (on Windows) is C:\xampp\apache\conf\extra\httpd-vhosts.conf, and it is a single file covering all such entries. Because yes, in native Apache too (and in NGINX, which we will discuss shortly) you can put all vhosts in one file, it is just less convenient to manage later on a production server.
On this occasion it is worth mentioning that in local environments like this you should not forget to keep access to localhost/foldername addresses, which is very helpful when developing many applications on one computer. The following vhost definition will help:
<VirtualHost *:80> ServerAdmin localhost@example.com DocumentRoot "C:/xampp/htdocs" ServerName localhost.com ServerAlias www.localhost.com ErrorLog "logs/localhost-error.log" CustomLog "logs/localhost-access.log" common <Directory "C:/xampp/htdocs"> Allow from all Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
MAMP configuration
MAMP is the equivalent of XAMPP on macOS. By the way, XAMPP itself is also available on Apple machines, but MAMP is even simpler and... sometimes irritating in the free version because access to some configuration files is a bit harder. But don't worry, you can enable virtual hosts here too, on a similar principle as in XAMPP. You do have to remember to enable the mechanism itself in /Applications/MAMP/conf/apache/httpd.conf, where you need to uncomment this line:
Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
As you may guess, it points to the location of the file with the virtual host definitions, where things look the same as described above. It is also worth mentioning that MAMP lets you switch from Apache to NGINX. Which brings us to the next point.
NGINX configuration
According to a survey from June 2022, this is the most popular HTTP server in the world (33.6%, while second-place Apache has a 31.4% share) when it comes to production environments. This is where NGINX shows its speed and capabilities, which is why it is widely used for serving websites, as a load balancer, as a reverse proxy server and in other cases. So why did I suggest that Apache is the most popular server among beginners? Because it has a slightly simpler structure and way of defining configuration. It is also not the case that someone who uses NGINX is automatically a professional while the "Apache people" are weaker. Both HTTP servers are good, each just has certain advantages and disadvantages that do not always have to be decisive. If performance is the deciding factor, there is no denying that NGINX is preferred.
As for virtual hosts, in NGINX we are talking about something called server blocks. The good news is that, as a rule, they are defined very similarly to Apache: we are interested in the /etc/nginx/sites-available folder, where we create a file for each site, and they look more or less like this:
server {
listen 80;
listen [::]:80;
root /var/www/myerp.com;
index index.html index.htm index.nginx-debian.html;
server_name myerp.com www.myerp.com;
location / {
try_files $uri $uri/ =404;
}
}
As you can see, the structure looks a bit more compact, but the differences under the hood are greater, and the way of defining, for example, redirect paths in NGINX is completely different. To enable the vhost, though, here too you have to link the appropriate file in the sites-enabled directory (this time with the standard linking command):
sudo ln -s /etc/nginx/sites-available/myerp.com /etc/nginx/sites-enabled/
and then test the configuration and restart NGINX:
sudo nginx -t sudo systemctl restart nginx
Here too we recommend the tutorial by DigitalOcean, which explains the whole process step by step.
Caddy configuration
As the Go programming language became widespread, the developer ecosystem gained tools, some of which came into wider use. One of them is Caddy, a simple web server that is easy to extend and, interestingly, provides an encrypted protocol automatically. It is presented as a more secure server and, above all, one that is simpler to configure, even than Apache.
I would not overstate the simpler part, though. Like any tool it requires getting familiar with it and trying things yourself, during which some people will be put off by Caddy and others will be delighted by it. This server is used, among others, by /kbin, and creating virtual hosts here looks quite simple:
myerp.com {
root * /var/www/myerp
file_server
}
You can read more in this article and, of course, in the official documentation. And you can get to know Caddy itself through the example on the pages of, who else, DigitalOcean.
What is /etc/hosts?
Let us go back for a moment to the explanation of how a user gets to a site in the first place, and focus on the first part: contact with the DNS server. We are assuming that the example domain MyERP.com is registered and publicly available. In that case we should indeed follow this path. However, as always, you can find at least three exceptional situations where you have to approach it differently.
The first is when the site we want to reach is already at a new IP address (for example, it has been moved to another server), but the DNS server does not know yet. There is nothing strange about that. The so-called propagation of changes across DNS servers can take from a dozen or so minutes up to 48 hours, so changing a domain name to point to another server may not be visible to everyone right away. In addition, DNS servers may already be returning the correct records while our own cache is still active and we cannot, or do not know how to, clear it for the moment.
The second situation is similar, but concerns a failure of the DNS servers or the inability to reach them. Such errors do happen. This category also includes, for example, bypassing some kind of block, or playing a prank on a coworker, though we do not recommend the latter, of course.
The third situation is also interesting, because the domain may not exist publicly. You can agree within a company that a site will be available at myerp.local, a URL used only within that environment. We do not want to, or cannot, register this domain publicly and so put it on the DNS servers. And yet the browser will somehow have to find out where to go after this address is typed in.
When we raise one of the problems above with someone who can help, we will often hear "add entries to /etc/hosts". It is a system file used to locally specify a fixed IP address assigned to a given domain name. It is thus a local, simplified version of a DNS server, available only to us and taking precedence over DNS servers. For example, if we assign 91.185.189.246 to the address youtube.com in this file, then after typing "youtube.com" into the browser we will actually land on Feedybacky.
The structure of this file is simple, though it is worth mentioning that we can edit it only with administrator privileges. Here is an example fragment of /etc/hosts:
120.50.66.171 system.test.com 127.0.0.1 local.site1 127.0.0.1 local.site2
These rules tell us that if a user on this computer accesses the address system.test.com, they will end up at the IP address 120.50.66.171. It also has two virtual hosts configured on its machine, that is, on localhost (the usual name for 127.0.0.1): local.site1 and local.site2. As you can see, this confirms that you can assign many domains or pseudo-domains to one IP address.
The /etc/hosts file itself is in exactly this location on Linux and macOS systems. But Windows also has such a file on disk, just in a folder that is a bit harder to remember: C:/Windows/System32/drivers/etc/hosts.
Summary
We hope this text has shed at least some light on virtual hosts. They are nothing scary, and in fact they are a required mechanism when designing modern software and when managing an HTTP server that holds many sites. At this point some may wonder who configures vhosts, and the answer is: administrators, with support from instructions written by programmers. There is also no point in kidding ourselves that coders will set everything up from A to Z. They often need help from administrators, who in turn need precise guidelines on what is to be achieved.
Best regards and thank you, Jakub Rojek.


