This is the personal blog of Neil Ang. Simple and interesting technology articles written by a developer for developers. Feel free to comment on posts or link to this site. Constructive feedback is always welcomed.

How to setup virtual hosts on Mac OS X 10.5 (Leopard)

Posted: 20 September 2008

Whilst developing a new project it can be convenient to setup a virtual host on your local machine. For example, if I wanted to have a local development/demonstration version of a website I'm developing (e.g. http://learnhowtojuggle.info), I could setup the following virtual host: http://learnhowtojuggle.dev.

N.B. Only follow this tutorial if you are comfortable with editing system configurations and are aware of the risks. If you have TextMate installed, you can click the file path links in these instructions to quickly open them on your local computer (More Information).

1. Modify the virtual hosts configuration

Use terminal to find and modify your virtual hosts configuration file.

mate /private/etc/apache2/extra/httpd-vhosts.conf

Append to this bottom of this file an entry for your virtual domain:

<VirtualHost *:80>
ServerAdmin webmaster@learnhowtojuggle.dev
DocumentRoot "/Users/neil/Sites/learnhowtojuggle"
ServerName learnhowtojuggle.dev
ServerAlias learnhowtojuggle.dev
ErrorLog "/private/var/log/apache2/learnhowtojuggle.dev-error_log"
</VirtualHost>

Make sure the DocumentRoot you enter actually exists. For convenience, I've placed mine in my Site folder. You can now save and close this file.

2. Check Apache knows about virtual hosts

Next we will need to modify the Apache configuration file to make sure it includes the virtual hosts file.

mate /private/etc/apache2/httpd.conf

Locate the following line and remove the # sign (also known as an "octothorpe" ) from in front of it.

Include /private/etc/apache2/extra/httpd-vhosts.conf

Save and close this file as well.

3. Restart Apache

For any changes to take effect on the Apache server, you will need to restart it.

sudo httpd -k restart

4. Update hosts file

Finally, the easiest way to let our browser know to use our local server when it get a request for our virtual host is to modify the hosts file.

mate /private/etc/hosts

Add the following entry:

127.0.0.1 learnhowtojuggle.dev

After you save and close this file, navigate to http://learnhowtojuggle.dev in your browser to see the newly created virtual host on your local machine.

Additional Configuration

Since I am serving my new virtual host out of my Sites folder, and I want to use my .htacess file for URL rewriting, I had to update the Apache configuration again to allow permission for this.

The configuration for my Site folder was located at /private/etc/apache2/users/neil.conf (yours will be different based on your system profile name). I then set my permissions to be pretty loose since it is only my local machine.

Example configuration:

<Directory "/Users/neil/Sites/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>

After you update your directory permissions, you will need to restart apache again for these to changes to take effect.

Your comments

No comments have been made. Why not be the first?

Post a comment

Comment Guidelines

  • Have no more than 2 links, otherwise your comment will be flagged as spam.
  • Links are automagically generated.
  • <em>text</em> to make text italic.
  • <strong>text</strong> to make text bold.

JavaScript needs to be enabled to comment.