Migrating WordPress sites between dev and live servers

In my work, at least once a week I’m either copying a live site onto my development server or moving a site back to the live server. I’ve figured out a few tricks along the way.

Notice: This post assumes that you have a basic comfort level with the command line, it assumes you have a linux web host, and it assumes you’re not going to sue me or hold me responsible if you break something. If that’s not the case, please stop reading now!

Choosing your dev server URL:

I strongly recommend that the URLs for your dev and production environments are the same number of characters. So, for my site hotchkissconsulting.net, I’ll choose a dev server URL like hconsultdev01.hdev1.com (same number of characters in the server name). This is very important because WordPress stores a lot of information as serialized values. Serialized values are, essentially, arrays of data being flattened into a string. They use the length of the string as a checkvalue, so if you have a longer (or shorter) dev site URL, when you go to find/replace, you’ll end up breaking that serialized value.

A good option for some sites– if your primary URL is www.yoursite.com, just use dev.yoursite.com for the dev server.

Prerequisites:

  1. Back up a LOT. Back up before you start, and back up continually as you go. It may seem like overkill, but when you break something, and, every now and then, EVERYONE breaks things, you’ll be glad you have it.
  2. Make sure both servers are up and running you have apache (or your server of choice) configured, etc
  3. These instructions assume that your dev and live URLs are the same length (see above). If they are not, following these instructions will probably break your site. Be careful!
  4. You need to have SSH access to follow these steps

Alright, let’s move your site:

Note, throughout this list I will refer to your “source” and “target” servers. The source is the server you’re moving from, the target is the server you’re moving to

Moving Your Database

  1. Connect to your source database through phpMyAdmin
  2. Download a full export of your database in SQL format
  3. Open your exported file in your favorite text editor, do a find/replace to replace the source domain name with the target domain name (find: www.yoursite.com, replace: dev.yoursite.com) and save it
  4. Connect to your target database through phpMyAdmin
  5. Drop any existing tables from the database (if applicable– if this is a new database, you won’t have any to drop
  6. Import your modified export file from step 3

Moving Your Files

  1. Connect to your source server using SSH, then cd to your web root (often times this will just be cd public_html, but not always…)
  2. Zip all of your files together for easy moving. The command for this is: zip -9r websitebackup * Be VERY careful with this, as you’re allowing someone to download all of your files directly from your web server. You should always add a few random characters after “websitebackup” so that someone can’t easily guess how to download this backup. So, for example: zip -9r websitebackup67sadga * creates an archive at your web root with the name websitebackup67sadga.zip
  3. Congratulations, you’re now finished with your source server!
  4. Connect to your target server using SSH, then cd to your web root
  5. Download the archive you created– on most servers, you can use the command wget sourceserver.com/websitebackup67sadga.zip (Replacing sourceserver.com with your source server’s domain name, and websitebackup67sadga.zip with the proper archive file name)
  6. Unzip your archive file: unzip websitebackup67sadga.zip
  7. Edit wp-config.php with your text editor of choice to point it at the proper MySQL db/user/host/password
  8. Find/replace the source URL out of all of the files you just moved. You can do this in one fell swoop with find ./ -type f -exec sed -i 's/www.yoursite.com/dev.yoursite.com/' {} ; (replacing the URLs with your own, first source, then target)
  9. Finally, delete the archive files off of both your source and target servers, and you’re in business!

This may all seem overwhelming at first, but with practice and time, you’ll be able to clone a site in 10 minutes flat! Need a hand? Leave a comment or send an email!

Leave a Reply

Your email address will not be published. Required fields are marked *