---
title: 'Easiest local development environment on OSX'
url: 'https://maxwesten.nl/blog/articles/easiest-local-development-environment-on-osx'
markdown: 'https://maxwesten.nl/blog/articles/easiest-local-development-environment-on-osx.md'
date: '2012-11-19'
description: "Create a folder testsite.dev in your ~/Sites folder and immediately access it with your browser by using http://testsite.dev, without changing your apache config or adding sitenames to your DNS or /etc/hosts file. That's what you can do after following the instructions in this post! Background Most of the time I've been using specific vhost files per webdevelopment project in a vhost folder of which all vhost files will be included. All deve"
taxonomy:
  category:
    - Development
  tag:
    - Terminal
    - OSX
    - Development
    - Apache
    - DNSMasq
  archives_month:
    - nov_2012
  archives_year:
    - '2012'
---

# Easiest local development environment on OSX

[Home](https://maxwesten.nl/)  [Blog](https://maxwesten.nl/blog)  [Articles](https://maxwesten.nl/blog/articles)  Easiest local development environment on OSX 

## [ Easiest local development environment on OSX](https://maxwesten.nl/blog/articles/easiest-local-development-environment-on-osx)

    19th Nov 2012   [Terminal](https://maxwesten.nl/tag:Terminal#body-wrapper) [OSX](https://maxwesten.nl/tag:OSX#body-wrapper) [Development](https://maxwesten.nl/tag:Development#body-wrapper) [Apache](https://maxwesten.nl/tag:Apache#body-wrapper) [DNSMasq](https://maxwesten.nl/tag:DNSMasq#body-wrapper)  

Create a folder `testsite.dev` in your `~/Sites` folder and immediately access it with your browser by using <http://testsite.dev>, without changing your apache config or adding sitenames to your DNS or `/etc/hosts` file. That's what you can do after following the instructions in this post!

## Background

Most of the time I've been using specific vhost files per webdevelopment project in a vhost folder of which all vhost files will be included. All development sites are added to the `/etc/hosts` file, so I could access them by a friendly-name lie `http://dlmax.dev`.

An article by [Blain Smith](http://blainsmith.com/articles/quick-and-dirty-local-domain-names-for-mamp) on his local development setup made me rethink my own setup: "Can't I just configure it to add the domainnames to the local DNS resolution too, so I only have to create a folder and immediately be able to access the site?"

## Apache configuration

### Current setup

I'm using the included Apache included in OSX Mountain Lion (10.8) and modified it, so it's using 1 conf file per vhost in the `/etc/apache2/vhosts` folder. These get included at the end of the file `/etc/apache2/httpd.config` with the line:

```
Include /private/etc/apache2/vhosts/*.conf
```

in the vhosts folder I've created a file `1_default.conf` that contains the following:

```
NameVirtualHost *

<VirtualHost *>
    ServerAdmin mwesten@localhost
    DocumentRoot "/Users/mwesten/Sites"
    ServerName max.local
    ServerAlias localhost
</VirtualHost>
```

An example of the other files, in this case `dlmax.conf` then contains:

```
<VirtualHost *>
    ServerAdmin mwesten@localhost
    ServerName dlmax.dev
    DocumentRoot "/Users/mwesten/Sites/dlmax"
    Options -Indexes +FollowSymLinks
</VirtualHost>
```

### Changes to this setup:

In `/etc/apache2/httpd.config` change the line that calls all `*.conf` files in the `vhosts` folder, to only call the `virtual.conf`

```
Include /private/etc/apache2/vhosts/virtual.conf
```

now create the `etc/apache2/vhosts/virtual.conf` to use some wildcards:

```
NameVirtualHost *
UseCanonicalName Off

<VirtualHost *>
  VirtualDocumentRoot "/Users/mwesten/Sites/%0"

  <Directory "/Users/mwesten/Sites">
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      Allow from all
  </Directory>
 # If you are experiencing problems with the PHP DOCUMENT_ROOT setting, create a
 # PHP file containing the following lines:
 #   <?php
 #    $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
 # and uncomment the following line, pointing to the just created PHP file:
 #   php_admin_value auto_prepend_file /Users/mwesten/Sites/setdocroot.php

</VirtualHost>
```

### DOCUMENT\_ROOT problems with PHP

If you are experiencing problems with your DOCUMENT\_ROOT server variable, uncomment the `php_admin_value` line in the `virtual.conf` file. Add the file `setdocroot.php` to the path specified above and put the following in, to fix the DOCUMENT\_ROOT problem described in this [apache bugreport](https://issues.apache.org/bugzilla/show_bug.cgi)

```
<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
```

## Get the local DNS names working

Because the `/etc/hosts` file doesn't parse wildcards, we cannot add something like: `127.0.0.1 *.dev`.

The only option is to install a local DNS server like [bind](https://www.isc.org/software/bind) or a DNS proxy like DNSMasq.

I'm using [DNSMasq](http://www.thekelleys.org.uk/dnsmasq/doc.html), because it's the easiest to configure and less prone to configuration errors.

### Install and configure DNSMasq

I'm using [Homebrew](http://mxcl.github.com/homebrew/) as my package manager, so if you don't use one, or use another one, you should install brew(to keep it easy) or try to follow the next steps based on your package manager.

```
brew install dnsmasq
```

Then create a textfile `/usr/local/etc/dnsmasq.conf` and add the following line, to redirect all `*.dev` sites to the local ip `127.0.0.1`:

```
address=/.dev/127.0.0.1
```

Now make dnsmasq start on system-startup and start it now:

```
sudo cp /usr/local/Cellar/dnsmasq/2.63/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
```

### Configure OSX to use your local system as first DNS server

Enter your network preferences and note the DNS servers named there. In my case I'm using my router, so `192.168.10.1`. Now add your loopback IP (`127.0.0.1`) first and then your previous addres.

Now if you try to ping some address ending in `.dev` it should give a reply like this:

```
$ ping thisisatestdomain.dev
PING thisisatestdomain.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.056 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.101 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.075 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.094 ms

--- thisisatestdomain.dev ping statistics     5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.056/0.081/0.101/0.016 ms
```

Pinging to `www.google.com` should also work, so we know external name resolutions works well too.

## Adding a new local site

Adding a site is now as easy as creating a new folder in the `~/Sites` folder with the name of the local sitename, so for `http://dlmax.dev` you would create the `dlmax.dev` folder.

 [ Previous Article](https://maxwesten.nl/blog/articles/setting-acl-on-webfolders-when-using-git-or-svn) [Next Article ](https://maxwesten.nl/blog/articles/revalidatie)

#### Related Posts

- 100 

     [Setting up MockSMTP on OSX 10.7(Lion) for local PHP development](https://maxwesten.nl/blog/articles/setting-up-mocksmtp-on-osx-10-7lion-for-local-php-development "Setting up MockSMTP on OSX 10.7(Lion) for local PHP development")
- 100 

     [Installing PHP 5.2.8 on OSX Leopard 10.5.6](https://maxwesten.nl/blog/articles/installing-php-on-osx-leopard "Installing PHP 5.2.8 on OSX Leopard 10.5.6")
- 100 

     [Themed Tab Terminal for OSX Leopard](https://maxwesten.nl/blog/articles/themed-osx-terminal "Themed Tab Terminal for OSX Leopard")
- 90 

     [Installing xDebug 2.0.4 or 2.1 on OSX](https://maxwesten.nl/blog/articles/installing-xdebug-204-or-21-on-osx "Installing xDebug 2.0.4 or 2.1 on OSX")
- 80 

     [Developing in PHP with NetBeans 6.8](https://maxwesten.nl/blog/articles/developing-in-php-with-netbeans "Developing in PHP with NetBeans 6.8")

[←](https://xn--sr8hvo.ws/%F0%9F%92%95%F0%9F%8E%89%E2%A4%B5%EF%B8%8F/previous)An IndieWeb Webring 🕸💍 [→](https://xn--sr8hvo.ws/%F0%9F%92%95%F0%9F%8E%89%E2%A4%B5%EF%B8%8F/next)

---

## Navigation

- Parent: [Articles](https://maxwesten.nl/blog/articles.md)
- Previous: [Revalidatie](https://maxwesten.nl/blog/articles/revalidatie.md)
- Next: [Setting ACL on webfolders when using GIT or SVN](https://maxwesten.nl/blog/articles/setting-acl-on-webfolders-when-using-git-or-svn.md)
