Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


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

Abel Lifaefi Mbula

Setting up a web server like Apache only gives us one domain, which is http://localhost associated with the 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.


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 (
  • 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, and 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., 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>
    DocumentRoot "/opt/lampp/htdocs/time-tracker"
    ErrorLog "logs/"
    CustomLog "logs/" common
  • 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 or

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:
  • 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


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.



View all Courses

Keep Exploring