How to Host multiple Websites on Apache2

March 29, 2012 in Apache Configuration

How to Host multiple Websites on Apache2

Let’s say that we own 2 or more domain names (hostpage1.com and hostpage2.com). We also have 2 web pages (hostpage1 and hostpage2). But we have only 1 webserver and 1 public IP (for this example i will use local IP but it will be the same if you own a public static IP)

Setting up the Domain names.

First thing is to create an “A record” in the DNS of your Domain Names to point “www” to your public or local IP address of your Web Server. I use local DNS in this example. So i will point the “www” for both of my domain names to 192.168.0.31. So it will be something like that:

www.hostpage1.com –> 192.168.0.31

www.hostpage2.com –> 192.168.0.31

Now if i ping www.hostpage1.com or www.hostpage2.com i will receive reply from the same ip (in my case 192.168.0.31). This IP is the IP of my Web Server. Test that everything is ok by typing in a terminal:

ping www.”the name of your domain name”

You should get reply from the Public IP of your web server:

In this example i use the following:

ping www.hostpage1.com

ping www.hostpage2.com

Setup directories for webpages

Now I will create 2 directories for my Web Pages (Hostpage1 and Hostpage2) in the directory named /var/ do that by opening a terminal and typing:

cd /var/

sudo mkdir hostroot

cd hostroot

sudo mkdir hostpage1

sudo mkdir hostpage2

Now i have 2 directories (hostpage1 and hostpage2) inside the “hostroot” directory. I will copy my 2 homepages, for my 2 domain names, inside my 2 directories and i will name both pages, as index.php(in this 2 directories you should copy your websites). So it will be something like the following

hostroot

|–>hostpage1

|    |–>index.php

|–>hostpage2

     |–>index.php

Configure Apache

The last thing i have to do is to edit “/etc/apache2/sites-available/default” do that by typing in a terminal:

sudo gedit /etc/apache2/sites-available/default

add the following lines at the end of this document

<VirtualHost *:80>

DocumentRoot /var/hostroot/hostpage1

ServerName www.hostpage1.com

</VirtualHost>

<VirtualHost *:80>

DocumentRoot /var/hostroot/hostpage2

ServerName www.hostpage2.com

</VirtualHost>

Save the file and exit.

now restart Apache2 by typing in a terminal

sudo /etc/init.d/apache2 restart

Open a browser and navigate to http://www.hostpage1.com and http://www.hostpage2.com.

You can do that to host multiple sites in your Web Server.

A video tutorial will be available soon so stay tuned!