How to set up Apache virtual host on Debian GNU/Linux

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.

One host per server: 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:

  • It does not reflect all the realities in the production server, where each site has its own domain (http://time-tracker.com).
  • You may encounter issues with relative paths.

To avoid issues in a production server, it would be better to have a local environment similar to the production one.

Multiple hosts per server: virtual host

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.

How to create a virtual host

Step 1: Create a new virtual host

  • 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

  • Now, open it with gedit editor using the following command:
sudo gedit /opt/lampp/etc/extra/httpd-vhosts.conf
  • Enter your password and start 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 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.

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
  • Enter your password if asked:
127.0.0.1   local.tracker.com
  • Save the file (Ctrl + X and Y).

Step 3: Enable virtual host conf

  • By default, the virtual host is disabled in httpd.conf. Open it to allow virtual host:
sudo nano /opt/lampp/etc/httpd.conf
  • Do a search to quickly find the virtual hosts line (ctrl + W and write Include etc).
  • Uncomment (delete the # sign before the line) this line.
Include etc/extra/httpd-vhosts.conf

  • You can now restart your server:
sudo /opt/lampp/lampp restart 
  • Go to your browser and you can now access your site at local.tracker.com.

Summary

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:

  1. Create a new virtual host in httpd-vhosts.conf file.

  2. Register it in hosts.

  3. Enable virtual host in httpd.conf.

Free Resources