Name Based Virtual Hosts on Apache – Multiple Websites to Single IP Address

Usually, you have a single IP adress and many websites to hosts. With Apache, you can have many domain names (,…) on a single IP address (
Here is a brief list of commands and configuration files for name based virtual hosting. Prerequisites: command line interface, Apache basics.
These quick notes are partly written from memory.

Install and Configure Web Server

Install web server and replace default web site

$ sudo apt-get -y install apache2
$ echo "Default"|sudo tee /var/www/html/index.html

Add New Name Based Virtual Host

$ sudoedit /etc/apache2/sites-available/
$ cat /etc/apache2/sites-available/
<VirtualHost *:80>
 DocumentRoot /home/xubuntu/publicsites/
 <Directory /home/xubuntu/publicsites/>
   Require all granted
$ sudo a2ensite
$ sudo systemctl restart apache2

Create Web Page as a Normal User

$ mkdir -p /home/xubuntu/publicsites/
$ echo pyora > /home/xubuntu/publicsites/


$ curl -H 'Host:' localhost
$ curl localhost

In real life, you rent a name from a provider, such as NameCheap or Gandi. Here, we can locally simulate name service

$ sudoedit /etc/hosts
$ cat /etc/hosts localhost xubuntu
# ...

Now we can try it with firefox.
Do you get a different site from localhost and Well done, you’re doing name based virtual hosting.
What next? How about adding some more virtual hosts? You can have as many as you wish – even if you only have a single IP address.

Posted in Uncategorized | Tagged , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *