| WordPress

vagrant-logoA few weeks ago, a friend mentioned to me that he'd like to step up his game when it comes to web development, and asked me about my workflow, and what software I use in the process of my craft. The first thing I'm going to talk about is Vagrant; specifically Varying Vagrant Vagrants.

Vagrant is a piece of software designed to "Create and configure lightweight, reproducible, and portable development environments." It often uses VirtualBox as it's backend, but could also use VMWare or other virtual machines.

That's a lot of technical jibber jabber as my wife likes to call it. In plainer English, Vagrant lets you write recipes for virtual machines. "I'd like Ubuntu Linux with a classic Apache, PHP, MySQL setup please” or "I'd like CentOS with all the software to run a Ruby on Rails environment”.

These recipes can then be shared around and people can have consistent development environments.

For WordPress development many people use a setup called Varying Vagrant Vagrants, or VVV. If you go to page for it and look for the What Do You Get? section you'll see everything included in this particular recipe. The important bits are Ubuntu, nginx, php, mysql, and WordPress set up in a variety of configurations.

Starting Up

If you want to try VVV, simply follow the instructions on their page. In the install instructions you'll see several Vagrant plugins, and you REALLY want to grab those. You need to keep your local machine's hosts file up to date, and the hostsupdater plugin is worth its weight in gold for that.

Once VVV is installed and you turn on your Vagrant setup for the first time it downloads a LOT of material. You'll see quite a few red warnings, but this is ok. It's simply letting you know that it found a problem, but it's fixing it. Once this is done the first time then starting up is quite quick.

Once the install is done and Vagrant is up, you can go to some of the default URLs like

  • http://src.wordpress-develop.dev
  • http://build.wordpress-develop.dev
  • etc.

Branching Out

This is great for developing themes and plugins against the versions of WordPress it comes with, but what if you want to make your own site, or 10 different sites?

Once upon a time that was a laborious process, but then Allison Barrett created a wonderful little script called the VVV Site Wizard. It's a sweet little bash script that asks all the right questions and then sets up all the right things to make your own site.

vvv wizard

Using this tool you can easily create a clone of an existing live site. Here are some steps to make that happen.

  1. On your live site, do a database dump. This could be done a number of ways, ranging from using a custom admin panel from your host, to phpMyAdmin, to good old raw command line. Make sure to use the flag to include the DROP TABLE commands. Take the sql file you just made and put it in your vagrant docroot. If you followed the VVV instructions exactly that would be vagrant-local/www/
  2. Copy all the file assets custom to your site to your local environment. This would probably be everything in wp-content, and it would all go inside the wp-content folder for your local site.
  3. Third, we do some WordPress magic. We're going to SSH into your vagrant install and use WP-CLI to import the database dump from your live site. If you're using windows then you'll need an SSH client like PuTTY. Otherwise, pull up a terminal window and go into your vagrant-local directory. Then type vagrant ssh and press enter. In a second or so you'll be ssh'd into your local server.

From there you need to get to your docroot. This is done with:

cd /srv/www/

To check to make sure your SQL file is there you can simply do:


and it should list everything in there. Once you see your SQL file you can type:

wp db import my_sql_file.sql

where my_sql_file.sql gets replaced by the real name of your file. This will import your database file from your live site.

IMPORTANT: at this point your local site will be broken, because it thinks that it is your live site, since your live site URLs are in the database. We can fix that with a quick search and replace.

While still in your SSH connection, you need to cd into the root of your new site. Your previous ls should have shown you the directory for that site, simply cd into it.

Once you're in there, do this:

wp search-replace olddomainname newdomainname --dry-run

where olddomainname would be the domain name of your live site, and newdomainname would be the domain name of your local dev site. The --dry-run means it won't actually do anything, merely tell you what it WOULD have done.

If the results look good, run it again without the --dry-run.

At that point you should be able to go to your local domain name for your site in your browser and see a local copy of your live site.

Tips and Tricks

  1. You don't have to log into the local server to manage your content. Vagrant shares your local file system, so you can open your WordPress files right in your favorite editor.
  2. If you go to http://vvv.dev you can see some handy links, like a local install of phpMyAdmin.
  3. VVV usually uses VirtualBox. If you take down your VVV instance with vagrant halt you can then fire up the VirtualBox GUI, go into settings, and give more RAM and CPU to your VVV instance, making it faster.

About the author

Topher is an accomplished programmer, having written his own content management systems and managed some very large websites. He loves to help people and believes playing with WordPress is fun. Topher lives in Michigan, USA.