Set a Local Domain to Ease Local Development

Oct 28, 2009 - 7 Comments

If you’re a web developer you probably do a fair amount of development on your local machine using either the built-in Mac OS X Apache server or, in my case, something like MAMP. Because a local web server like this is really handy for testing this, you can make your local development life a bit easier by setting a local domain, and we’ll show you how to do that.


For what it’s worth, we’re covering this for Mac OS X, but you can set local domains like this on a Linux PC or Windows PC too. As long as the computer has a hosts file, you can use a local domain using this same trick.

You’re going to need to modify your hosts file in order to do this, it’s not difficult, but does require the command line. From the Mac Terminal type the following:

sudo nano /etc/hosts

This will bring up the /etc/hosts file in the nano editor, it will look something like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost

Setting the Local Domain Name

Next is the important pat: you’ll want to add the hostname (in this case, we’ll use the name local.dev) you’d like to use locally to the end of that file on a new line, in the following format:

127.0.0.1 local.dev

Save the changes to /etc/hosts file by hitting Control-O and then Control-X to exit.

Now you can access your local domain via the web browser, ftp, or whatever other means just by accessing “local.dev” in the appropriate web browser. You may need to flush your Macs DNS cache for the effect to take effect, and some apps may require a quick relaunch too, like Safari or Chrome.

You obviously don’t need to pick “local.dev” as your local domain, and you can actually use the localhost IP to test live domains this way without taking them live, which allows you to preserve links when testing a site, spider, crawler, or whatever else you’re working on.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Bill Ellis in Command Line, Development, Tips & Tricks

7 Comments

» Comments RSS Feed

  1. hypnopixel says:

    Hi Bill,

    how is this different than using ‘localhost’ which is already aliased?

    regards,

    aitch

  2. Woodgie says:

    It’s prettier?

    It also allows you to create more than one ‘site’ in conjunction with a bit of apache config twiddling. Something along the lines of:
    site1.domain.whatever
    site2.domain.whatever

    Don’t ask me how, this is a comment, not a hint!

    :-)

  3. hypnopixel says:

    @Woodgie, ah, I see that. And for brevity, the alias could be shorthanded to, say, lh.d, lh.qa, and lh.p for dev, qa and production. Yeah, the apache config twiddling needs to be alluded to, at least ;-]

  4. Frank Leigh says:

    There is more to it than that. The main benefit is your local websites will have private sessions from one another thus preventing session overwriting when you swicth between one site and another using the same browser instance.

  5. Andy Jones says:

    Does this mean I can do without MAMP?

  6. […] a jump to the command line. Editing the hosts file allows you to do a variety of things like set local test domains, block websites and IP’s, downgrade iOS, troubleshoot weird iTunes errors, and much […]

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates