Monday, July 30, 2012

How To Configure Multiple Websites on a Single Server

Apache virtual host, as explained by Apache, refers to the practice of running more than one web site (such as and on a single machine. Virtual hosts can be “IP-based” or “name-based”. By default, Ubuntu already has this capability enabled, so things are much easier to configure these days. Enough with the intro, this is how you do it:

Create the folders that will host your new sites. By default, Apache in Ubuntu servers from /var/www. So create these:
mkdir /var/www/site1
mkdir /var/www/site2
mkdir /var/www/site3

Copy the current default setting found in /etc/apache2/sites-available/default and name it the same as your new site.

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site2
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site3

Edit the new config files for each site using your preferred text editor. Add the line ServerName server1 right below the ServerAdmin line and change both DocumentRoot and Directory to point to your new sites.

This is what it should look like (you’ll do exactly the same for each of your 3 new sites, repeat this step for as many new sites as you’ll be creating):

<VirtualHost *:80> 
ServerAdmin      <a href="">
ServerSignature  email
DocumentRoot    /var/www/
CustomLog         logs/ combined
ErrorLog             logs/
Note that this is far simpler than the default presented from Ubuntu, that’s because Apache is very flexible and you can include many directives and configurations under vhost files that is appropriate for your site.  For the purpose of this tutorial serving static content that differs per site, the above is good enough. As we don’t need the default any more you can safely remove this from the sites-available directory by simply deleting the symbolic link called 000-default.

Key lines are as follows:
  • ServerName is very important for virtual hosting – Apache uses this to distinguish between the different virtual hosts/sites served from this Apache web server.  This is the line that matches the “Host:” request header your browser sends as part of the request.
  • ServerAdmin is an email address that people will use if they see the default error pages, and we enable that feature by saying that when an error displays, display the email address too (ServerSignature).
  • DocumentRoot is set to /var/www/ so create this area with the correct permissions of the person or group that will manage this website (i.e. it shouldn’t be “root” or “apache” owned if it was a production site).
  • CustomLog and ErrorLog reference the log files (CustomLog is the access log and ErrorLog is self explanatory).
 Create an index.html file in /var/www/ and put in “” as the text
mkdir -p /var/www/ 
echo "" > /var/www/ 
Now remember we created this file in the “sites-enabled” directory, so to enable it do the following
 ln -s /etc/apache2/sites-available/ 
Restart Apache to pick up the change (this can be done on a running server without interruption to service using the following):
 /etc/init.d/apache2 graceful 
A graceful restart waits for existing connections to die, and new ones are loaded with the new configuration.
That’s “” complete.
Now repeat steps 1-5 replacing all references to with and you have 2 sites available on different hostnames but served from the same Apache web server.
Test the set up

To test this set up on your own Linux setup add the following lines to /etc/hosts 
Now in your browser go to and then go to – you should see the text change when you visit those sites.  From here you can create more complex set ups based on this simple configuration of Apache.

Congratulations, you have done it, 3 sites running off of the same Apache server. Hopefully this tutorial helped you. 

No comments:

Post a Comment