0

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

newnomad
  • 243

1 Answers1

2

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).

Michal M
  • 3,796
  • 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 07 '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 07 '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 07 '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 07 '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 07 '11 at 17:22
  • When I simply name my sites directories site1 and site2 without activating virtualhosts, I can just access them like that. But then they are all on the same server. Which creates problems for serversite scripting. When activate virtualhosts and create the virtualdocumentroot – newnomad Jun 07 '11 at 20:59
  • I can still access them the same way, but then they are on their own server, correct? When I don't need to access my sites from another computer on my network, then a proper url setup with hostfiles doesn't offer me any advantage except a nice url? – newnomad Jun 07 '11 at 21:06
  • Well, if you don't use VirtualHosts and only use folders site1 and site2, you're still able to use all the things server-side scripting gives you, this includes .htaccess config. It's all matter of having correct paths, that's it. – Michal M Jun 07 '11 at 21:55
  • If you set up VirtualDocumentRoot and if you still use the same path, then nothing changes, and by accessing via folder (and not domain) it's still as if VirtualDocumentRoot wasn't set. As far as I'm concerned VirtualDocumentRoot setup (one you seem to after) won't properly work without BIND DNS or manually updating hosts file for each domain. Now, when it gets into accessing the site from other computers in the network, it's whole other story... Neither BIND DNS nor hosts file won't make any difference, as they apply only to your local machine's setup. Other computers are not affected. – Michal M Jun 07 '11 at 21:58
  • So if I don't mind uncorrect paths, and the serverside applications I run are intelligent enough not to choke on that, I could just use folders. If I want nice automatic urls; I found a way for automatic host files using bonjour. – newnomad Jun 07 '11 at 22:22
  • fixing documentroot might be needed – newnomad Jun 07 '11 at 22:44