Apache Virtual Host Setup
Learn how to set up a virtual host in an Apache-like server and in GNU/Linux.
Setting up a web server like Apache gives us only one domain, which is http://localhost
. This domain is associated with the 127.0.0.1
IP address.
One host per server —localhost
As we stated above, we are only allowed to have on the website by a web server. To get around this limitation, we’ll create different directories, each for one website on the localhost:
http://localhost/time-tracker
http://localhost/smartsapp
We have two directories in http://localhost
. We can add as many as we want.
Although this solution is helpful it has its own issues as well:
- It does not reflect the reality of the production server, where each site has its own domain (
http://time-tracker.com
). - We may encounter issues with relative paths.
To avoid issues in a production server, it would be better if we have a local environment that is almost the same as the production environment.
Multiple hosts per server —virtual host
To allow more than one website to work on one system or web server, each with their own domain name, we’ll need to activate the virtual host feature on that server. For instance, both www.amezon.cd
and www.facebook.cd
can be hosted on the same web server.
There are many types of virtual host methods, but the most widely used one is the name-based virtual host method. The name-based virtual host method allows one IP address (for example, 127.0.0.1
) to host more than one website (host name). This approach allows for an unlimited number of servers, ease of configuration and use, and requires no additional hardware or software.
Three steps to create a virtual host
Step 1: Create a new virtual host
We’ll need to create a virtual host for each domain that we want.
The configuration file responsible for this is httpd-vhosts.conf
. In my installation, it is located at /opt/lampp/etc/extra/httpd-vhosts.conf
Now, let’s open it with the gedit editor or whichever editor we want:
sudo gedit /opt/lampp/etc/extra/httpd-vhosts.conf
We’ll enter our password and begin editing as follows:
<VirtualHost *:80>
ServerAdmin sarah@masika.com
DocumentRoot "/opt/lampp/htdocs/time-tracker"
ServerName local.tracker.com
ServerAlias www.local.tracker.com
ErrorLog "logs/tracker.com-error_log"
CustomLog "logs/tracker.com-access_log" common
</VirtualHost>
In the DocumentRoot
directive, we’ll specify where our project is located. ServerName
and ServerAlias
are the domain names we want the server to respond to. So, in our case, the server will respond to http://local.tracker.com
or www.local.tracker.com
.
Step 2: Register the new virtual hostname
This must be done in the hosts
file. In Debian GNU/Linux, it is located at /etc/hosts
:
sudo nano /etc/hosts
We’ll enter our password if asked the following:
127.0.0.1 local.tracker.com
We’ll save the file (Ctrl + X
and Y
):
Step 3: Enable virtual host configuration
By default, the virtual host is disabled in httpd.conf
. We’ll open it to allow virtual hosts:
sudo nano /opt/lampp/etc/httpd.conf
We’ll do a search to quickly find the virtual hosts
line (ctrl + W
and write Include etc
). We’ll uncomment (delete the #
sign before the line) this line:
Include etc/extra/httpd-vhosts.conf
We can now restart our server:
sudo /opt/lampp/lampp restart
We can now access our site at local.tracker.com
.
Wrap up
Virtual hosting is a technique used to allow one web server to host more than one domain. The name-based virtual hosting type is the most popular.
We can create a virtual host on Debian GNU/Linux following these three steps:
- Create a new virtual host in the
httpd-vhosts.conf
file. - Register it in
hosts
. - Enable the virtual host in
httpd.conf
.
Get hands-on with 1400+ tech skills courses.