Mark Kirby

Setting up virtual hosts on OS X Leopard

A couple of months ago I explained how to set up PHP 5 on OS X Leopard - it was very easy! We also configured PHP so that the default folder was your Users/home folder, instead of Library/Webserver.

If you have lots of sites to test, you might want to be able to type a url into the browser, and see the site directly. To do that you need to modify your Mac’s hosts file, and set up a Virtual Server.

1 - Preparation

Decide upon the URL you want to use, I’m going for mark-kirby.dev in this example.

Decide where you want the files to go that will appear for this url, and place them there on your computer. I’m using /Users/username/Sites/mark-kirby for this example.

2 - Edit the hosts file

Open the file

/private/etc/hosts

Since this file is hidden, you will need to need to check the option “open hidden files” if your text editor has one, or use the terminal to open the file directly.

mate /private/etc/hosts

Alternatively, you could:

  1. Open finder
  2. click on go
  3. select “Go to folder”
  4. enter in /private/etc/

You will see the following:

## # 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

Underneath this, enter your local IP address (127.0.0.1) and the name of the site you want to set up, e.g.

127.0.0.1 mark-kirby.dev

Save the file - you will have to authenticate - and now when ever you type http://mark-kirby.dev into a browser it will give you your local folders - EVEN if mark-kirby.dev is an actual website on the internet. Browsers always check the hosts file first.

2 - Add the virtual host in apache

If you type mark-kirby.dev into your browser now, you will still see your default folders, you need do some more work.

Open the file

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

using the methods described above.

You will see this first -

# # Use name-based virtual hosting. # NameVirtualHost *:80

Don’t touch it!

Then you will see this -

# # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <virtualhost> block. # </virtualhost><virtualhost *:80 > ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/www/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common </virtualhost>
<virtualhost *:80 > ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/www/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common </virtualhost>

First I’ll quickly explain what each line means:

  • ServerAdmin - the email address of the server admapachestrator e.g. mark-kirby@mark-kirby.dev, this will show up on error pages
  • DocumentRoot - the location of the files to show e.g. /User/username/Sites/mark-kirby
  • ServerName - the domain name you want to use e.g mark-kirby.dev
  • ServerAlias - an alternative name which will point to the same place e.g. www.mark-kirby.dev
  • ErrorLog - the location of a log file to output errors to, e.g. “/User/username/Sites/logs/mark-kirby-error-log”
  • CustomLog - the location of a file where all other logs should be written, followed by a format - just stick with common - e.g. “/User/username/Sites/logs/mark-kirby-access-log common”

So really, you only need DocumentRoot and ServerName if this is a test server only you will access.

Firstly comment out the examples - very important, don’t miss this step!

Then add a default entry so localhost will still take you to the root of your Sites folder:

<virtualhost *:80 > DocumentRoot "/Users/username/Sites" ServerName localhost </virtualhost>

Finally, add your entry, e.g.

<virtualhost *:80 > DocumentRoot "/Users/username/Sites/mark-kirby" ServerName mark-kirby.dev </virtualhost>

Save this, you’ll need to authenticate.

3 - Activate virtual hosts in apache

If you haven’t done so, by default the file you just edited, won’t be enabled in Apache.

To change this open up another hidden file - /private/etc/apache2/httpd.conf

Find the line

# Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf

And uncomment the include so it looks like this:

# Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf

Save the file - you may need to authenticate.

4 - Restart Apache

This still won’t work, until you restart apache.

To restart apache:

  1. Go to system preferences
  2. Select “sharing”
  3. Uncheck the box “Web Sharing” - apache will stop
  4. Check it again - apache will start

Test your site - you should be in luck!

5 - Permission problems

If you have any problems with permissions on your new site, you could try Jeremy Keiths solution - he adds a directive to his httpd-vhosts.conf file which allows everything, it looks like this:

<directory /Users/*/Sites/> Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews AllowOverride All Order allow,deny Allow from all

Tags:

9 Responses to “Setting up virtual hosts on OS X Leopard”

  1. On April 20th, 2008 at 4:20 pm, Ross Boardman said

    Great article. Thanks for your efforts. This is by far the beest explanation I found after a few hours of searching for a concise set of instructions.

    Should the line: “And comment out the include so it looks like this:”

    Be ‘Uncomment’ as opposed to ‘comment out’?

  2. On April 21st, 2008 at 9:33 am, kirby.mark said

    Ross - of course it should have, thanks, I’ve updated that now! Glad you found the tutorial useful.

  3. On May 11th, 2008 at 3:37 pm, Chris V said

    Thanks for the great tutorial. My question is related to setting up the virtual host directory to run Cgi scripts written in Python. Should the python scripts live inside DocumentRoot? I know they can, but I read somewhere it is a bad idea to have python in DocumentRoot for security reason. The whole reason I have the virutal hosts for each site is so I can do my development for each site in its own directory and use the browser address point to each one. Is there a recommended way to set up a test environmet for each site I am working on?

  4. On May 15th, 2008 at 7:45 pm, Quinn Taylor said

    I found that the syntax for CustomLog in the examples is incorrect.

    Rather than:
    CustomLog “/User/username/Sites/logs/mark-kirby-access-log common”
    it should be:
    CustomLog “/User/username/Sites/logs/mark-kirby-access-log” common

    This is evident if you run ‘apachectl -S’ to check the vhosts configuration.

  5. On May 23rd, 2008 at 10:41 am, kirby.mark said

    Thanks Quinn, stupid mistake, now corrected :-)

  6. On July 19th, 2008 at 7:10 pm, KinRou Blog » Setting up virtual hosts on Leopard said

    [...] http://mark-kirby.co.uk/2008/setting-up-virtual-hosts-on-os-x-leopard/ [...]

  7. On August 29th, 2008 at 3:53 pm, Martin said

    Thanks! This information really helps me to solve my virtual hosts problems.

  8. On October 9th, 2008 at 7:21 pm, Gabe said

    Thanks, helpful. Another minor error: in the ‘Permission problems’ section, you say that Jeremy Keith “adds a directive to his hosts file.” This didn’t work for me, so I checked JK’s own site: the directive should of course go in the httpd-vhosts.conf file.

  9. On October 27th, 2008 at 9:48 pm, Luke Dorny said

    Removing the smartquotes in the code sections would be extremely helpful. took me forever to notice that the smartquotes were pasted in, instead of regular code-quotes.

    Thanks for the article.

Leave a Reply

This site was created with Wordpress, using my own template.