To host multiple site with different domain names you’ll need to configure Apache to understand this and to switch sites when different domain name requests come in. For this configuration example, we’ll assume that you have your web sites located in separate folders in the ‘/Library/WebServer/Documents’ directory. Each web site has a sub-folder of its own under that folder, like this:

/Library/WebServer/Documents/site1.com
/Library/WebServer/Documents/site2.com

In this example the domain names for the two sites are site1.com and site2.com. We’re going to set up virtual hosts for those two sites using those domain names

The configuration for virtual host has moved outside of the httpd.conf file into a separate include file. To actively include this file you’ll need to uncomment the following line in your httpd.conf:

Include extra/httpd-vhosts.conf

Open your ‘httpd-vhosts.conf‘ file in ‘/etc/httpd/extras‘ in your editor and go to section 3 almost at the bottom. You should see an example of a virtual host there. Each line of that example will begin with a hash (#). The hash character marks the line as a comment, so the example is not executed. Add the following lines below that example (change ‘your.external.ip.address’ into the ip-address your server is configured with):

NameVirtualHost your.external.ip.address:80
 
<VirtualHost your.external.ip.address:80>
  DocumentRoot /Library/WebServer/Documents/site1.com
  ServerName site1.com
  <Directory /Library/WebServer/Documents/site1.com>
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
 
<VirtualHost your.external.ip.address:80>
  DocumentRoot /Library/WebServer/Documents/site2.com
  ServerName site2.com
  <Directory /Library/WebServer/Documents/site2.com>
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

If you also want to use www.site1.com you’ll have to add an extra line like:

<VirtualHost your.external.ip.address:80>
  DocumentRoot /Library/WebServer/Documents/site1.com
  ServerName site1.com
  ServerAlias www.site1.com
  <Directory /Library/WebServer/Documents/site1.com>
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

That’s all there is to it! Save and close the file and restart Apache. That will tell the Apache server everything it needs to know in order for it to serve the pages using the domain names.

You can set many other configuration settings for your domain based webserver like webmaster email and logfiles like:

<VirtualHost your.external.ip.address:80>
  DocumentRoot /Library/WebServer/Documents/site1.com
  ServerName www.site1.com
  ServerAdmin webmaster@site1.com
  ErrorLog /var/log/httpd/site1.com.error_log
  CustomLog /var/log/httpd/site1.com.access_log combined
  <Directory /Library/WebServer/Documents/site1.com>
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

You might want to read the official Apache documentation on Name-based virtual hosting to see all the configuration possibilities.

Next step: Configuring HTTPS with virtual hosts