Nikita Vasilyev – pronounced as Ni·kí·ta Va·síll·ev

How to setup local hosts on Mac OS X

When I develop this website locally I use n12v.dev virtual host. n12v.dev/index.html is just ~/Sites/n12v.com/public/index.html on the file system.

You can achieve this by either using Pow or /etc/hosts and Apache.

The easy way: Pow

Install

curl get.pow.cx | sh

Use

ln -s ~/Sites/n12v.com ~/.pow/n12v

Now n12v.dev serves index.html.

To serve a directory with a name different from “public”:

mkdir ~/.pow/usercss
ln -s ~/Sites/usercss/www ~/.pow/usercss/public

The hard way: /etc/hosts and Apache

Open /etc/hosts in Sublime or your favourite text editor and add:

127.0.0.1	n12v.dev
fe80::1%lo0	n12v.dev

fe80::1%lo0 is an IPv6 address. Without it your local site might run too slow.

Check if it works correctly:

ping n12v.dev
PING n12v.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.040 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.071 ms

Press ctrl C to stop it. If you see ping: cannot resolve n12v.dev: Unknown host then run

sudo dscacheutil -flushcache

Apache

Mac OS X Mountain Lion ships with Apache 2.2.22. You don’t need to install it.

Open /etc/apache2/extra/httpd-vhosts.conf and add the following code to the bottom

<VirtualHost *:80>
  ServerName n12v.dev
  DocumentRoot /Users/nv/Sites/n12v.com/public
  ErrorLog /Users/nv/Sites/n12v.com/error.log
  CustomLog /Users/nv/Sites/n12v.com/access.log common
</VirtualHost>

Note that “nv” is my username; yours is different.

Check if you haven’t made any typos:

apachectl configtest
Syntax OK

Restart Apache:

sudo apachectl restart

That’s it. http://n12v.dev should serve ~/Sites/n12v.com/public/index.html

Troubleshooting 500 Internal Server Error

  1. Open /etc/apache2/httpd.conf
  2. Find <Directory "/Library/WebServer/Documents"> section
  3. Replace
      Order deny,allow
      Deny from all
    with
      Order allow,deny
      Allow from all
  4. Set DocumentRoot to /Users/nv/Sites
Published by
Nikita Vasilyev
· Updated