How do I setup a virtual host using Mac OS X Server and Apache?

From Mac OS X Server FAQ

Jump to: navigation, search

Contents

Overview

In the Apache world, the term Virtual Host refers to the practice of serving more than one website from a single machine. For example, if you hosted both the websites www.company1.com and www.company2.com from your machine, each of these sites would be referred to as a Virtual Host.

There are three main steps involved in creating a new Virtual Host in Apache:


  • Edit the DNS
  • Create the site folder
  • Actually configure the Virtual Host


Edit the DNS

Imagine your server already hosts the website for your company, www.acme.com, and you'd like to add a Virtual Host where you can post employee-specific information, called staff.acme.com.

Obviously, for the host to resolve when people enter it into their browser, you need to edit the DNS zone data for acme.com and add an Address or CNAME record to associate the hostname staff with the IP address of your server.

Specific instructions for doing this are beyond the scope of this article. If you don't know how to add records to your DNS zones, start here.


Create the site folder

You need to create a folder which will be the "document root" for your Virtual Host. In other words, this folder will represent the top level of that website.

There are various schools of thought on the best place for this folder:

Some people like to store their web sites in /Library/WebServer (which is Apple's default location), while others contend that this location is not appropriate for anything beyond the default single-site configuration. Some like to keep their sites in some subfolder of /var, as per the default Apache setting, but there are arguments as to why this is not a suitable location either. Personally, I like to create a folder called /websites or /www and have my sites as subfolders of this, with each Virtual Host's folder named after the FQDN of that site.

Whichever location you choose, take care in the permissions you set for the Virtual Host's folder. The Apache web server runs as the user 'www', so this user must have read access to the files in order to be able to serve them up. But for security reasons, you'll want to avoid giving 'www' more access than it needs (such as write access to your site files). In addition, those who will upload content to the site will obviously need to be given appropriate access privileges to do so.


Add the Virtual Host

In Mac OS X Server Admin parlance, an Apache Virtual Host is known as a "Site". You can add one by opening Server Admin, connecting to your server, choosing the Web service, Settings, Sites, and clicking the button with the plus sign (+).

You'll be required to fill in the FQDN of the site, as well as the path to the folder you created above. You'll probably also want to visit the Options tab for the site and disable the performance cache. Peruse the options for your site and configure them to your liking, and when you are done click the left-arrow button to return to the list of sites. Enable your new site by clicking the checkbox, and finally Save all your changes.


Conclusion

You can now go to your web browser and test your newly-configured Virtual Host. If all goes well, you will receive the correct homepage upon typing in the FQDNs of the various sites configured on your server.

What's Next

If the Virtual Host you configured was www.example.com, a lot of times you also want it to respond if the user enters simply example.com into their browser. DNS records alone are not enough to accomplish this; you must tell Apache about all the alternative names for the Virtual Host. Unfortuantely, Server Admin does not yet provide a way to do this, so you need to modify a configuration file by hand. Read this.

Personal tools
Toolbox