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 www.company1.com and www.company2.com) 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):

/etc/apache2/sites-available/site1
<VirtualHost *:80> 
ServerName       site1.adminnation.com
ServerAdmin      <a href="mailto:webmaster@adminnation.com">webmaster@adminnation.com
ServerSignature  email
DocumentRoot    /var/www/site1.adminnation.com
CustomLog         logs/site1.adminnation.com.access.log combined
ErrorLog             logs/site1.adminnation.com.error.log
</VirtualHost> 
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/site1.adminnation.com 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/site1.adminnation.com and put in “site1.adminnation.com” as the text
mkdir -p /var/www/site1.adminnation.com 
echo "site1.adminnation.com" > /var/www/site1.adminnation.com/index.html 
Now remember we created this file in the “sites-enabled” directory, so to enable it do the following
 ln -s /etc/apache2/sites-available/site1.adminnation.com 
/etc/apache2/sites-enabled/site1.adminnation.com 
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 “site1.adminnation.com” complete.
Now repeat steps 1-5 replacing all references to site1.adminnation.com with site2.adminnation.com 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
 127.0.0.1 site1.adminnation.com site2.adminnation.com 
Now in your browser go to http://site1.adminnation.com and then go to http://site2.adminnation.com – 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