Configure Virtual Hosting With Apache2 On Ubuntu 12.10

Configuring Apache to utilize named based hosting, or “virtual” hosting is an easy, cost-effective way to host multiple websites on the same server.  You can even host multiple websites on the same IP address, because the packet header is name based, and Apache will serve up the appropriate webpage based on the name request.  First things first, make sure you’ve installed Apache ( check out my other tutorials here and here if you’re new to Apache. )  Assuming you’ve successfully installed and configured Apache, we can go ahead and start configuring our virtual hosts and start serving up multiple websites.

First create a new folder that will house the virtual website.  It’s best to name the folder something descriptive, especially if you plan to host multiple websites.

sudo mkdir /var/vhosts.com

Now set the permissions appropriately.

sudo chmod 755 /var/vhosts.com/

You have two options at this point: you can either maintain individual configuration files for each website in the /etc/apache2/sites directory, or you can manage the sites in one file, which is the apache2.conf file.  I personally prefer to utilize the apache2.conf file over individual configurations, but it’s up to you.  Open up the apache2.conf file, and scroll all the way down until you see the “Include sites-enabled” line.  This is where we will start creating our virtual hosts.

<VirtualHost *:80>
ServerName www.vhosts.com
ServerAlias vhosts.com *.vhosts
DocumentRoot /var/vhosts.com
ErrorLog logs/vhosts.com-error_log
CustomLog logs/vhosts.com-error_log common
</VirtualHost>

The ServerName directive is set to our domain, vhosts.com, and the ServerAlias directive tells Apache to listen for name based requests for the primary Top Level Domain, and any sub-domains that we may create down the road – hence the * placeholder.  DocumentRoot is simply the location of the folder that will hold the website and it’s files ( php / js / css, etc ), which we created earlier.  Don’t forget to add the ErrorLog and CustomLogs directives, otherwise you won’t have any logging for your website.

Now that you’ve created this virtual host, apache needs to be restarted to re-read the configuration file and start serving requests.

sudo /etc/init.d/apache2 restart

And that concludes our quick tutorial on configuring name based hosting with Apache.  You can add as many websites as your server can handle using this method, simply repeat the steps above.  If you’re curious about using name based hosting under Microsoft IIS, you can check out this tutorial here as well.

About the author: frankshafer

Overall easy going guy, living on California's beautiful Central Coast. Currently working as an IT Consultant for a great company that provides outsourced IT support to local businesses.

3 comments

  1. Juampa says:

    Hi,

    do I need to add my new virtualhost server name to my etc/hosts? Something like:

    127.0.0.1 vhosts.com

    Good article, btw

  2. Nope! the /etc/hosts file should contain only the computer name and loopback, external/internal DNS records and that’s it.

    Let me know if that helps Juampa – appreciate the kind words :)

Leave a Reply

%d bloggers like this: