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 (terokarvinen.com, botbook.com…) on a single IP address (109.74.201.133).

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/pyora.example.com.conf
$ cat /etc/apache2/sites-available/pyora.example.com.conf
<VirtualHost *:80>
 ServerName pyora.example.com
 ServerAlias www.pyora.example.com

 DocumentRoot /home/xubuntu/publicsites/pyora.example.com

 <Directory /home/xubuntu/publicsites/pyora.example.com>
   Require all granted
 </Directory>
</VirtualHost>

$ sudo a2ensite pyora.example.com
$ sudo systemctl restart apache2

Create Web Page as a Normal User

$ mkdir -p /home/xubuntu/publicsites/pyora.example.com/
$ echo pyora > /home/xubuntu/publicsites/pyora.example.com/index.html

Test

$ curl -H 'Host: pyora.example.com' 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
127.0.0.1 localhost
127.0.1.1 xubuntu
127.0.0.1 pyora.example.com
# ...

Now we can try it with firefox.

http://localhost

http://pyora.example.com

Do you get a different site from localhost and pyora.example.com? 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 *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Recent Posts

  • Picks

  • Boxing Clock for AndroidOcton8 Diving T-ShirtsShaking Tower Panda Android GameLearn Chinese with Android
  • Student projects