Hosting Multiple Websites with apache2


Multible Sites Hosting Multiple sites can be extremely beneficial, why have a separate server for each of your websites when you could combine them all onto one server. Now granted, if your website is using most of your server load then its probably not a good idea to put more websites onto this server. However most sites, especially static sites, will not require there own dedicated server.

When you buy web space from a provider like 123-reg.com you are not getting a dedicated server for your website, instead you are buying a Virtual Host, that is web space shared with others on a single server, and this works in the same way we are going to discuss in this tutoiral. So if you want to setup your own hosting company listen closely ;-)


Before we get to the interesting stuff we need to cover a little theory on how web-servers respond to client requests. So The client sends the request to the web-server in the form of a Network Packet, this has contains much information including, the source & destination address, (source being the client and destination being the server). When a basic server reads this it will send the requested page back to the source address.

If we want to host several sites we need to setup several Virtual Hosts, these are virtual web-servers designed to host different websites. In this scenario the client sends the same request to the server, however this time it reads the destination address and responds the appropriate website.

So how do we setup virtual hosts, well if you are using Apache 2 you will already have 1 Virtual Host to see the setup for this look in /etc/apache2/sites-available/default

nano /etc/apache2/sites-available/default

Now you should see a configuration page similar to this:

<VirtualHost *:80>
 ServerAdmin webmaster@localhost

 DocumentRoot /var/www

 <Directory />
   Options FollowSymLinks
   AllowOverride None
 </Directory>

 <Directory /var/www>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride None
   Order allow,deny
   allow from all
 </Directory>

</VirtualHost>

Now this is the standard site configuration for Apache2, as you can see its encapsulated in a Virtual Host already, this virtual host will return files in stores in the folder /var/www (shown in the DocumentRoot /var/www & <Directory /var/ww> fields). However there is no setting telling apache when to return this site to the user, in this case it will respond this to any user requests the page (without checking the destination field).

We need to add a Sever Name so Apache can distinguish between sites, to do this add

ServerName www.YOUR-DOMAIN.com

Under the server admin field in the config file. In this setup Apache will only return the site if the user enters www.YOUR-DOMAIN.com into their web browser, using the server IP address will now longer work.

Now its time to add our second site, for this example the first site is going to be stored in/var/www1 and the second is going to be stored in /var/www2. We need to create another virtual host, assign the correct document roots and add the correct server names, edit your default file to look something like this:

<VirtualHost *:80>
 ServerAdmin webmaster@localhost

 ServerName www.YOUR-DOMAIN-1.com

 DocumentRoot /var/www1

 <Directory />
 Options FollowSymLinks
 AllowOverride None
 </Directory>

 <Directory /var/www1>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride None
 Order allow,deny
 allow from all
 </Directory>

</VirtualHost>

<VirtualHost *:80>
 ServerAdmin webmaster@localhost

 ServerName www.YOUR-DOMAIN-2.com

 DocumentRoot /var/www2

 <Directory />
 Options FollowSymLinks
 AllowOverride None
 </Directory>

 <Directory /var/www2>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride None
 Order allow,deny
 allow from all
 </Directory>

</VirtualHost>




Now when the a client requests YOUR-DOMAIN-1.com they will see the site stores in /var/www1 and when a user requests YOUR-DOMAIN-2.com they will see the site stored in /var/www2. You can add as many of these virtual hosts as you need.

Hope this helps, if you have any further questions feel free to comment and I’ll get back to you asap.

, , , , , , , ,

  1. No comments yet.

You must be logged in to post a comment.

Comments are closed.