Setting up a web server like Apache only gives us one domain, which is http://localhost
associated with the 127.0.0.1
IP address or another address.
localhost
Since we are only allowed to have one website by a web server, we can bypass this limitation by creating different directories for each website on the localhost.
http://localhost/time-tracker
http://localhost/smartsapp
We have two directories in
http://localhost
. You can add as many as you want.
While this solution may work, note that:
http://time-tracker.com
).To avoid issues in a production server, it would be better to have a local environment similar to the production one.
To allow more than one website on one system or web server, each with their own domain name, we need to activate the virtual host feature on that server. For instance, www.amezon.cd
and www.facebook.cd
can both be hosted on the same web server.
There are many types of virtual host methods, but the most commonly used one is a name-based virtual host. It allows one IP address (e.g., 127.0.0.1
) to host more than one website (host name). This approach allows an unlimited number of servers, ease of configuration and use, and requires no additional hardware or software.
For each domain you want, you’ll need to create a virtual host.
The configuration file responsible for this is httpd-vhosts.conf
.
In my installation, it is located at:
/opt/lampp/etc/extra/httpd-vhosts.conf
gedit
editor using the following command:sudo gedit /opt/lampp/etc/extra/httpd-vhosts.conf
<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>
DocumentRoot
directive, we specify where our project is located. ServerName
and ServerAlias
is the domain name we want the server to respond to. In my case, the server will respond to http://local.tracker.com
or www.local.tracker.com
.hosts
file. In Debian GNU/Linux it is located at /etc/hosts
:sudo nano /etc/hosts
127.0.0.1 local.tracker.com
Ctrl + X
and Y
).httpd.conf
. Open it to allow virtual host:sudo nano /opt/lampp/etc/httpd.conf
virtual hosts
line (ctrl + W
and write Include etc
).#
sign before the line) this line.Include etc/extra/httpd-vhosts.conf
sudo /opt/lampp/lampp restart
local.tracker.com
.Virtual hosting is a technique used to allow one web server to host more than one domain. The most used virtual hosting type is the name-based one.
You can create a virtual host on Debian GNU/Linux following these three steps:
Create a new virtual host in httpd-vhosts.conf
file.
Register it in hosts
.
Enable virtual host in httpd.conf
.