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.
Now set the permissions appropriately.
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.
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.
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.