How to host multiple websites with Apache

The Apache web server has a convenient feature called Name-based Virtual Hosting. This function allows us to have a single LAMP Stack server configured on one IP address, but serve a different set of files depending on which domain is being requested.

This sounds more complicated than it is. Say we had example1.com and example2.com, both of which are to be separate websites, but both domains point to the same IP address. Apache’s Name-based Virtual hosting makes this possible. In fact, this feature forms the basis of 90% of this planet’s shared hosting business.

Let’s see how to do this in CentOS 6 and 7.

Tweaking the Apache configuration file

By default Apache will serve files from a single directory specified in /etc/httpd/conf/httpd.conf. So no matter which host name requests files, everyone will get the same as what’s set in Section 2, “Directory”:

<Directory "/var/www/html">

Leave that line intact.

Further down in the same file, under Section 3 (usually at the very end) you’ll find examples for the Virtual host configuration. It’s very simple really: first we need to switch this functionality on (it’s switched off by default), and in the second step we’ll configure which directory shall be served for which domain.

Enabling Name-based Virtual Hosting

The first line we need to add (or un-comment) is this one, outside any angle bracket groups:

NameVirtualHost *:80

This will enable our feature, and in doing so tell Apache on which port to listen for web requests. As a result, the default directory specified above is now ignored.

Underneath we can now define as many Virtual Host sections as we need, like this:

# Test Host
<VirtualHost *:80>
     ServerName test
     DocumentRoot /var/www/test
</VirtualHost>

The first line starting with the hash symbol is just a comment, to remind us what we’re doing here. The section in angle brackets tell Apache what it should do when requests for a server called “test” are coming in, namely serve files from the directory /var/www/test.

You can add as many sections as you like in the above format, each of which specifying a different directory on the server. So in our example, we could add test2, test3, test4, and so forth – or choose whatever name you want to use.

Those files need to have their ownership permissions tweaked accordingly, so they can be read and written to by the Apache user (be that apache:apache or equivalent).

If this is a configuration on our local network, the system accessing this server needs to either resolve the DNS of “test” to the IP of your server, or you can alternatively (and probably more easily) tweak the hosts file on said system.

Here’s an example for a real world domain tweak. Say we had a domain example1.com and example2.com. As mentioned above, both domains need to point to the IP of our server. Then we’d define two sections like this:

# Test Host
<VirtualHost *:80>
     ServerName example1.com
     DocumentRoot /var/www/example1
</VirtualHost>

# Test Host
<VirtualHost *:80>
     ServerName example2.com
     DocumentRoot /var/www/example2
</VirtualHost>

The location and name of those directories is arbitrary; they can reside anywhere on the system and can be called anything you like.

Restart Apache

With those changes in place, don’t forget to restart the web server with

service httpd restart

And that’s it! You’ve now got multiple domains setup on your Apache web server.

Happy browsing 🙂





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.