Using a hosts file for easy management of dev, staging, and production WordPress sites

This recent article at Smashing Magazine discussed trends and challenges in deploying WordPress sites. The biggest issue cited by the article and the lengthy comments that follow is the issue of the database: because WordPress stores strings in the database that contain the site domain (including configuration options and asset paths), it’s hard to migrate content and config between dev, staging, and production environments. A bunch of possible solutions were offered up, including interconnectit/Search-Replace-DB, which I use fairly often and really like.

I was surprised, however, that no one talked about my preferred strategy, which is, in a way, the simplest: Dev, staging, and production should all have the same domain names. When you remove the need to change the domain, you make it much easier to deploy specific pieces of content, spin up new instances, etc.

Since all versions of a site have the same domain name – say, booneisthebomb.com – I use my local hosts file (/etc/hosts on *nix systems) to switch between instances. So I may have the following lines in /etc/hosts:

# Local development
# 127.0.0.1      booneisthebomb.com

# Staging site
# 123.456.789.0  booneisthebomb.com

With these two lines commented out, going to booneisthebomb.com in a browser will use DNS for the lookup, which is to say it’ll go to the production site. Uncommenting one of the lines allows me to work on the local or staging site.

The biggest pitfall of this technique is that now there is no obvious way to tell your environments apart (and you definitely don’t want to mistake your production site for a dev site). My solution for this is to drop this file into wp-content/mu-plugins/. Then, in my environment file (which contains env-specific config, such as database connection info), I put a line like this:

define( 'ENV_TYPE', 'staging' );

Now, when I load up the staging site, it shows booneisthebomb.com in the URL bar, and the following box appears at the bottom of every page:

env-type-flag

Using a hosts file for easy management of dev, staging, and production WordPress sites by Boone Gorges, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

6 thoughts on “Using a hosts file for easy management of dev, staging, and production WordPress sites

  1. jszz

    i use the same technique for my personal dev environment.. but the problems still remain when you want to set up a dev or staging for ‘early access’ or review for clients or product manager while keeping the production intact.. for a non techie, you are not going to tell them to change the hosts file.. that said I still can’t avoid the searchandreplace script (which i found very useful indeed).. thoughts?

    Reply
  2. ubernaut

    yeah for sure simplicity is always the best path imo if you are using a mac theres a handy little app called gas mask that lets you swap out host files through the menu bar.

    Reply
  3. ajitscorpio

    1. how do you manage DB changes.. say you made some changes to the site… which needed DB updates as well. moving them to staging and production is a pain right now for me.

    2. my production is getting constant posts. how to avoid any conflict when deploying staging onto production without loosing any data?

    Reply
  4. Jay Collier

    Hi, Boone!

    Setting up a new development environment and haven’t quite gotten this working.

    In the env.php file in my local instance, I have this:

    define( ‘ENV_TYPE’, ‘local’ );

    In my /env-type-flag.php/ file in my local instance, I have this:

    if ( defined( ‘ENV_TYPE’ ) && ‘local’ !== ENV_TYPE ) {

    Unfortunately, this is not inserting the new ID/styling into the interface when I’ve changed my hosts file to see the local instance. (I have made configuration changes to the local instance, so I do know I’m looking at it.)

    Any feedback welcome. And Happy New Year!

    -Jay

    Reply
    1. Boone Gorges Post author

      Hi Jay – Happy new year to you too!

      The first thing that pops to mind, given your comment about “/env-type-flag.php”, is to check whether this file is in the right place. It should be in wp-content/mu-plugins – that way, WordPress’ll be sure to load it at the correct time.

      You can verify that the file itself is being loaded by putting some simple testing code right after the opening < ?php. Something like: die( 'working' );

      Reply

Leave a Reply