Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Running Snow Leopard, I have enabled the build in apache(with indexing) and php, and installed and enabled mysql. I have setup my sites in the ~/Sites directory, named:

  • www.site1.dev
  • www.site2.dev

It all works,localhost shows the index linking to them.

Now I want to set up virtual hosts in the most simple way. I would like that typing www.site1.dev in the url bar serves me the site in that directory.

So I outcommented the include vhosts rule in the httpd.conf file, and in the httpd-vhosts.conf pasted this code based on the apache docs:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /Users/me/Sites
UseCanonicalName off

VirtualDocumentRoot /Users/me/Sites/%0
AllowOverride All

RewriteEngine On
</VirtualHost>

Yet my browser can't find the server. What am I doing wrong? I did notice that in my sharing system prefs the IP my ISP gives me, and their domain is included in the link created there to access my sites folder. I would expect a simple localhost there not an ipaddress....

share|improve this question

1 Answer 1

You need to update you httpd-vhosts.conf to something like this:

<VirtualHost *:80>
    DocumentRoot "/Users/me/Sites/www.site1.dev"
    ServerName www.site1.dev
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/Users/me/Sites/www.site2.dev"
    ServerName www.site2.dev
</VirtualHost>

Having each domain separately in your config allows you to have separate server options for each, e.g. Rewrite config, separate log files, etc.

Then, update your /etc/hosts and add this at the end:

127.0.0.1   www.site1.dev
127.0.0.1   www.site2.dev

This step is particurarily important as this is where your OS looks first when resolving domain names. Otherwise it will not know what www.site1.dev is.

Then simply restart the server by disabling Web Sharing and enabling it again.

To make sure your config works you can check apache logs (access_log and error_log) via Console.app (Files > /private/var/log > apache2).

share|improve this answer
    
This is the hard way you had to do it in apache1, apache2 supports automatic virtual hosts, so you can just drop a directory in, and it will be served. See this tutorial –  newnomad Jun 7 '11 at 15:43
    
Yes, but gives you more control. You can skip first part, but still need to do the second - /etc/hosts. –  Michal M Jun 7 '11 at 15:44
    
I can see how the tutorial you linked doesn't include hosts config. This is because it uses BIND DNS. If you do this though, you won't be able to use full domains as you want (http:// www.site1.dev). You'd have to use "local" domains (http:// site1.local). –  Michal M Jun 7 '11 at 15:51
    
thats'OK, however I read somewhere that the BIND wasn't even necessary? You could set it up without adding lines to the host file ? –  newnomad Jun 7 '11 at 16:21
    
Not that I know of. http:// localhost/ only works then, this would mean that you'd need to access your sites using http:// localhost/www.site1.dev/ –  Michal M Jun 7 '11 at 17:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.