Adding virtual hosts to your Apache installation on OS X

A question I got a lot from people when I started documenting my efforts was how to setup Apache so it could use multiple domains on the same server. So here is how I’ve used it.

The file we’ll need to edit is the Apache configuration file. It is located in ‘/Library/Apache2/conf’ and is called ‘httpd.conf’. For this configuration example, we’ll assume that you have your web sites located in separate folders in the ‘/Library/Apache2/htdocs’ directory. Each web site has a sub-folder of its own under that folder, like this:

'/Library/Apache2/htdocs/site1.com'
'/Library/Apache2/htdocs/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

Open your httpd.conf file 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/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 want to use www.site1.com you’ll have to add another entry like:

<VirtualHost your.external.ip.address:80>
   DocumentRoot /Library/Apache2/htdocs/site1.com
   ServerName 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:

<VirtualHost your.external.ip.address:80>
   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.

3 Responses to “Adding virtual hosts to your Apache installation on OS X”

  1. snozle Says:

    hello, I’ve followed these instructions, but I can’t seem to get them right. When I point my browser to the URL snozle.com it goes to my hosting provider’s site, but when I direct it to http://www.snozle.com it goes to my site. I have my DNS servers configured:
    @ A ipaddress
    * A ipaddress
    www. A ipaddress

    Also, my second entry, bondosco.com just doesn’t work and directs me to snozle.com.

    Finally, I am no longer able to access my phpmyadmin folder by going to
    myserverip/phpmyadmin. When I do that it simply gives me a 404.

    The virtual host section of my httpd.conf file is below:
    NameVirtualHost *:80

    DocumentRoot /Library/Apache2/htdocs/snozle
    ServerName snozle.com

    DocumentRoot /Library/Apache2/htdocs/mediawiki-1.5.1
    ServerName bondosco.com

  2. Murray Says:

    Hello,

    I’ve had multiple sites working off my mini (10.4.11) for years. I’ve just bought a new iMac (10.5.2) and I’m trying to get it configured so I can move the site off the mini.

    I seem to have everything configured as on the mini but I keep getting the 403 Forbidden error.

    The only differences I can see is that some paths have changed:

    mini: /private/etc/httpd/httpd.conf
    iMac: /private/etc/apache2/httpd.conf

    The httpd.conf on the iMac also uses an include file for the virtual hosts config but I have that commented out.

    I noticed this (http://docs.info.apple.com/article.html?artnum=306884) on Apple’s support site but it seems to be for a different problem. At any rate the files described in the article are all there anyway. Is there some other user I need to add?

    Any suggestions? Thanks.

    Murray =:-)

  3. another steve Says:

    Hi Murray, no doubt you’ve sorted the problem now but just in case anyone else has a similar problem it’s easily solved by editing the httpd.conf file and changing the following entry:

    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all

    To:

    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all ## Changed this from “Deny from all” ##