Search code examples
macosapacheurllocalhostosx-mavericks

Two "domains" on localhost under OS X?


Question for: Mac OS X 10.9 Mavericks

I understand that I can create a website on my machine by placing the files in:

/Library/WebServer/Documents/

I can then access this website in a browser through:

http://localhost/

I can also create two sites and put them each in a subfolder:

/Library/WebServer/Documents/one/
/Library/WebServer/Documents/two/

The URLs for the two sites will then be:

http://localhost/one/
http://localhost/two/

What do I need to do to access the two sites with the following URLs?

http://one/
http://two/

And is it possible to have the two folders in different locations? E.g.:

/Users/myusername/Desktop/one/
/Users/myusername/Documents/two/

Solution

  • For this you need to:

    1. Configure your computer to resolve the "domains" one and two to your local computer. Edit your /etc/hosts file:

      127.0.0.1   localhost one two
      

      Make sure to leave the default localhost in there or stuff may break.

      You will probably have to explicitly type http://one in Chrome or other "omnibar" browsers or you'll get a Google search instead.

    2. Configure Apache to recognise those two domains by setting up virtual hosts. It depends on what Apache install you're using exactly, where its config file(s) is/are etc. But at some point you should have these entries:

      <VirtualHost *:80>
          ServerName one
          DocumentRoot /path/to/one
      </VirtualHost>
      
      <VirtualHost *:80>
          ServerName two
          DocumentRoot /path/to/two
      </VirtualHost>
      

      You may additionally have to tell Apache that it's allowed to serve files in those directories, e.g.:

      <Directory /path/to/one>
          Require all granted
      </Directory>
      

      That very much depends on your Apache installation's default configuration. You will also have to make sure those directories have appropriate file permissions to allow Apache to read files within them. See http://serverfault.com for in-depth Apache setup questions. Read the Apache documentation.