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
[code lang="apache"]/private/etc/hosts[/code]
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.
[code lang="apache"]mate /private/etc/hosts[/code]
Alternatively, you could:
- Open finder
- click on go
- select “Go to folder”
- enter in /private/etc/
You will see the following:
[code lang="apache"]## # 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[/code]
Underneath this, enter your local IP address (127.0.0.1) and the name of the site you want to set up, e.g.
[code lang="apache"]127.0.0.1 mark-kirby.dev[/code]
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
[code lang="apache"]/private/etc/apache2/extra/httpd-vhosts.conf[/code]
using the methods described above.
You will see this first -
[code lang="apache"]# # Use name-based virtual hosting. # NameVirtualHost *:80[/code]
Don’t touch it!
Then you will see this -
[code lang="apache"]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 [/code]
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:
[code lang="apache"]DocumentRoot "/Users/username/Sites" ServerName localhost [/code]
Finally, add your entry, e.g.
[code lang="apache"]DocumentRoot "/Users/username/Sites/mark-kirby" ServerName mark-kirby.dev [/code]
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
[code lang="apache"]# Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf[/code]
And uncomment the include so it looks like this:
[code lang="apache"]# Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf[/code]
Save the file – you may need to authenticate.
4 – Restart Apache
This still won’t work, until you restart apache.
To restart apache:
- Go to system preferences
- Select “sharing”
- Uncheck the box “Web Sharing” – apache will stop
- 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:
[code lang="apache"]
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
[/code]
Tags: PHP



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’?
Ross – of course it should have, thanks, I’ve updated that now! Glad you found the tutorial useful.
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?
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.
Thanks Quinn, stupid mistake, now corrected
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/ [...]
Thanks! This information really helps me to solve my virtual hosts problems.
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.
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.
Why it took me so long to find this site I’ve no idea, but I’m glad I did. In addition to the main post, there are some helpful tidbits from other commenters so thanks to everyone for contributing.
I’ve been trying to get this to work off and on for months. I’d try for a couple of days and give up, come back, try again and give up for a week. Learned a lot in the process including two things I really needed and didn’t know:
1. Edit /private/etc/hosts to get your virtual host to work.
2. Type ‘apachectl -S’ in the terminal to troubleshoot problems with the httpd-vhosts.conf file syntax; it will spit out any syntax errors that you need to change. Go change them and watch your site come up. Joy.
Hopefully my rant helps someone else.
Adam
Why 127.0.0.1 ?
Why not 192.168.0.1 ?
nlex – 127.0.0.1 will always be your localhost, regardless of your ip address or even if you are on a network or not
Hello! Thanks for the great tutorial. My virtual hosts seem to be working but there’s one bug. I’ve got a local development site called ‘nadesignlab’ that contains a directory called ‘icons’. This directory contains a file called ‘icon.gif’. When I browse to this file directly using the following URL, I get a ‘Not Found’ error:
http://nadesignlab/icons/icon.gif
When I try to access the file in the following manner, I have success:
http://localhost/nadesignlab/icons/icon.gif
I was wondering if you have any insight into what may be happening. Thanks!
Is there any way to make these new virtual hosts accessable on a network? Thanks!
192.168.0.1 is your modem/router address. Pretty common when using Netgear modems. They either use 192.168.0.1 or 192.168.1.1
If you were allowing friends to connect from outside then you would be using a 192.168.*.* (Your LAN Address)to direct them to that PC on the LAN if you wanted them to connect to your pc.
As Mark said 127.0.0.1 is your localhost address
Mark,
Great tutorial!!! Got me up and running!! I have question about the ServerAlias.
I tried doing this and when I enter the address in the browser I get a redirect as if it tried going through the internet.
I have a folder called “testphp” in /Users/username/Sites/. My host file has this entry:
127.0.0.1 testphp.dev.
Then, my vhost file has:
DocumentRoot “/Users/username/Sites/testphp”
ServerName testphp.dev
ServerAlias http://www.testphp.dev
Any thoughts/suggestions?
Michael – hi, sorry for the late reply. Firstly, you don’t need the server alias so get rid of that. hen go to testphp.dev (not http://www.testphp.dev, forget all about www for this) and check again. It should work fine.
great tutorial – worked like a charm
Thanks for taking the time to post it.
Thanks for the great tips. I got this working perfectly with only one minor fix. I was getting a “403 Forbidden” error on everything I tried to access on my virtual hosts.
I had to add this in:
Allow from all
inside of my tags. Now it works like a charm. Aparently the default settings on Apache are set up to deny all on anything besides the DocumentRoot defined in httpd.conf, but this took care of it fine.
On April 15th, 2009 at 9:15 am, 13 steps » Turning your Mac into a webserver said
[...] web sharing in you preferences. If you intend on hosting multiple sites, here’s a rundown on how to configure virtual hosts. PHP comes included with Leopard but is disabled by default. You could enable it, however, [...]
The permissions fix…
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
…needs the directory tag closing ie:
Doesn’t it?
After hours of tearing my hair out over this you’ve provided a simple and easy to follow tutorial. Many thanks!
Much more straight forward than most I’ve seen. I just have one question and I know it will seem silly to most, but bear with me since I’m new to Macs. What do you mean when you say authenticate, because I tried running down an explaination of how to do that somewhere else and I’m not getting anything but again a reference to authenticate. When I tried this without it, I got a permission denied instead of a request for password or the like.
hm. how can i access this form another machine via LAN??
The easiest Virtual Host tutorial ever, worked first time.
Happy days, thanks!!
Thank you so much for making this as easy as possible. I have searched for hours on how do this. But they were all long winded and did nothing but confuse you. Thanks for keeping it real.
Thanks for the nice comments, glad to be of help. Fossli – to access a site on your LAN you would need to change the hosts file on the machine you want to access from, point the url you want to use to the ip address of the machine you want to host the site on, and set the site up on that machine with the virtual host. So basically repeat step 2 for every machine on your network that will access the locally hosted site, and instead of 127.0.0.1 you use the IP address of the computer hosting the site (e.g. 192.168.0.10).
On June 28th, 2009 at 1:13 pm, Sätt upp en lokal utvecklingsmiljö i Leopard - steg för steg | andreask.se said
[...] Setting up virtual hosts on OS X Leopard [...]
Wow, that’s great. Mark’s 24th June post is a very useful tip if you need to debug IE on PCs. Thanks for taking time to explain this in plain english.
Ok. thanks kirby.mark. I get a new ip every day…that’s why i asked. i can see the solution is to get one static ip-adress…
thanks
Apache require that you name the virtual host if theres more than one.
Here is what needs to be placed on the first line of your vhosts.
NameVirtualHost *
Worked for me.
Hey thanks for the tutorial, any reason why my request would be resolving to /Users/username/Library/Caches/TemporaryItems/fbtmp/example.com
Great tutorial!!! Thank You. Got it Working.
Thanks very much for taking the time to write this. I couldn’t get my virtual hosts working with my MacPorts Apache2 installation until I read your tutorial and realized I had to edit the hosts file.
Thanks so much for this great tutorial. It worked fabulously!!!
Wow, you just made my life easier! Before I’d read this, I’d been setting up a virtual host according to my IP address, which changes everytime I login to the router!
Now I’m using 127.0.0.1, I’ll never have to worry about that again once I’ve setup a site, so thanks very much for your help!