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/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:
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):
<VirtualHost your.external.ip.address:80>
DocumentRoot /Library/Apache2/htdocs/site1.com
ServerName site1.com
</VirtualHost>
<VirtualHost your.external.ip.address:80>
DocumentRoot /Library/Apache2/htdocs/site2.com
ServerName site2.com
</VirtualHost>
If you also want to use www.site1.com you’ll have to add an extra line like:
DocumentRoot /Library/Apache2/htdocs/site1.com
ServerName site1.com
ServerAlias www.site1.com
</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:
DocumentRoot /Library/Apache2/htdocs/site1.com
ServerName www.site1.com
ServerAdmin webmaster@site1.com
ErrorLog logs/site1.com.error_log
CustomLog logs/site1.com.access_log combined
</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



2:34 am
Hi folks,
I just spent a few hours trying to figure out why the virtual host wasn’t working in my local machine. Well, it turn out that you have Mac that “site1.com” is not to be looked for on the net, but on the local machine instead.
To do this your /etc/hosts file (yes, it doesn’t have extension) should look like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 site1.com
7:29 am
hi, i could configure the virtual hosts for a static ip using the procedure u suggested !! thanks!!
could you also please shower some light on how to do the same if one has dynamic ip address?
7:33 am
There are 2 possible solutions:
use: NameVirtualHost *:80
or if you’ve got a hostname from your dyndns service
NameVirtualHost yourname.dyndns.org