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/WebServer/Documents/site1.com
ServerName site1.com
</VirtualHost>
<VirtualHost your.external.ip.address:80>
DocumentRoot /Library/WebServer/Documents/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/WebServer/Documents/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/WebServer/Documents/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
2:57 am
Possibly stupid question — how do I restart apache on the mac?
I tried service httpd restart — but got
service: unrecognized command: restart
similarly,
service httpd start
returns
service: failed to start the ‘httpd’ service
2:49 pm
Depends on how you installed apache.
For the default installation it is “sudo apachectl restart”
For the DIY installation it is “sudo /usr/local/apache2/bin/apachectl restart”
4:36 am
I’m a bit confused…perhaps there’s an error, here:
You state that the site contents will reside in
/Library/WebServer/Documents/site1.com
Then later you point the vhost DocumentRoot to
/Library/Apache2/htdocs/site1.com
Why are those different? Shouldn’t they both be the same?
8:14 pm
Graig, you are correct. My mistake, I’ve corrected it.
4:30 am
*sigh*
I’m getting “Forbidden
You don’t have permission to access / on this server.”
Where does Apache store its logs so I can figure out what’s going on?
I swear…on an XP box, I had Apache, MySQL, PHP, and Perl all installed and functional in less than 90 minutes. So far, I have ~8 hours on this stupid Mac and I’m only 30% done. Somehow that seems very, very wrong.
9:45 am
Craig, I’m sorry you feel this way. The logfiles are located where you configured them in httpd.conf or your virtual server config. Default value is /var/log/httpd/.
Because Mac is unix based security/permissioning is a bit more restrictive/controlable than a windows box. You have to make sure that the apache user (usually _www) at least has read access to the files/directory you want to use.
4:20 am
Craig,
Perhaps you are experiencing a “problem” with Apache’s directory default “Order deny,allow” and “Deny from all” clauses which are rather restrictive. (~line 177)
In order to get around that, (even though the file owner/permission all were set to reasonable access levels) I had to specify each virtual host’s directory directive to allow access from a URL. Something like:
VirtualHost testsite.local
ServerName testsite.local
DocumentRoot /Users/Shared/www/testsite/htdocs
# now set access permissions for DocumentRoot
Directory /Users/Shared/www/testsite/htdocs
AllowOverride FileInfo
Order allow,deny
Allow from all
/Directory
/VirtualHost
See the documentation before you jump in there: http://httpd.apache.org/docs/2.2/mod/core.html#directory
HTH