Saturday, October 6, 2018

Migrating a live WordPress site to XAMPP localhost

This guide assumes you have some basic knowledge about XAMPP, phpMyAdmin, and MySQL and have done this process before, but maybe not in a long time. If you need a more detailed step-by-step guide, you'll have to look elsewhere.

Step 1: Installing XAMPP

Install the specific version of XAMPP you need. For instance, I needed PHP 7.2.7 because I wanted my local environment to match my live DreamHost environment exactly. Here are their currently supported PHP versions.

Next download a fresh copy of WordPress or download your entire WordPress install from live. I opted for a fresh copy because I have lots of extra files and plugins I didn't want to waste time downloading, but this makes it slightly harder.

Export your live site database from phpMyAdmin (the quick SQL export option worked fine for me).

Navigate to c:\xampp\htdocs and setup your WordPress folder. Unzip your WordPress files or install into this folder. Here is an example of how it should look:

  • c:\xampp\htdocs\mywebsite\wp-admin
  • c:\xampp\htdocs\mywebsite\wp-content
  • c:\xampp\htdocs\mywebsite\wp-includes
If you opted for a fresh install, I would copy over your theme files. You may also need to grab your uploads folder and any required plugins or mu-plugins. For example, a few of my sites require Advanced Custom Fields to function. I copied that plugin over but not others like Sucuri or Google Analytics because those aren't required on the dev site.

You can also rename wp-config-sample.php in the root directory to wp-config.php at this point.

Step 2: Configuring phpMyAdmin and MySQL

XAMPP is accessible by typing localhost in your browser once Apache and MySQL are running. By default, phpMyAdmin and MySQL won't have passwords set, but WordPress requires those values in the wp-config.php file.

First thing you'll need to do is open the XAMPP Control Panel and click "Shell". This will open a command prompt where you can type:

  • mysqladmin.exe -u root password yourpassword
Next you'll edit c:\xampp\phpMyAdmin\config.inc.php and change the ['authtype'] value from 'config' to 'cookie'.

You can optionally change the ['blowfish_secret'] at the top to a long randomized string to stop phpMyAdmin from throwing a security warning.

Restart Apache and MySQL from the XAMPP Control Panel and navigate to: localhost/phpmyadmin

You should be met with a login screen where you can type in "root" for the username and the password you created earlier. Assuming all went smoothly, you should have access to phpMyAdmin with a valid login now.

Before jumping back to WordPress, we need to import the database. If you're not at the home area (aka you clicked a database), click the little Home icon or the "Server: 127.0.0.1" breadcrumb and then databases. Create your new database.

Notes: the database name does NOT need to match your live site database name. phpMyAdmin may also be defaulting your character set and collation to latin1_swedish_ci which doesn't match my live site settings. If this is the case for you, you can simply switch it to something like utf8_general_ci or change some config settings to default it to this value every time. This was valuable for me because I was importing 6 websites with plans to create a few more. Head over to c:\xampp\mysql\bin\my.ini and anywhere under the [mysqld] section add:
  • character-set-server=utf8
  • collation-server=utf8_general_ci
Save and restart Apache and MySQL.

Once your database is created, click on it, and then click on Import. Choose the SQL file from earlier and import the database. You may need to alter the settings, but I was able to leave all the default settings without any problems. Remember, if it doesn't work, you can always just drop the tables and start over.
  • If you database is over 2 mb you may not be able to import it by default. Open c:\xampp\php\php.ini and find "upload_max_filesize=". Change the 2M value to something like 32M or whatever value seems reasonable to you. Save, restart Apache and MySQL, and then retry the import.
The last step before configuring WordPress is to update the _options table. Usually siteurl and home are the first two rows, but you may need to search for them. They will be set to: https://www.yourwebsite.com/ which won't work for a local dev site. Edit each of those records and change the values to: http://localhost/folderName. folderName corresponds to the folder in htdocs and notice the removal of https with http.

Step 3: Setting up WordPress

Alright, now it's time to configure WordPress. Open the wp-config.php file from earlier.

Change DB_NAME to match your local database name and DB_PASSWORD to match your MySQL password from earlier. DB_USER, DB_HOST, DB_CHARSET, and DB_COLLATE should work as-is unless you changed them in a step outside of this guide.

You may also need to scroll down and change $table_prefix  = 'wp_;' to something else. For instance, DreamHost automatically changes table prefixes for WordPress installs to something like: wp_r4nd0mh45h_tablename. If your host does this, you'll need to copy over wp_r4nd0mh45h_ to this variable in the config file (notice the trailing underscore that's included).

That's it! Now when you visit localhost/folderName your WordPress site should skip the install process and load up a copy of your live site.

If it doesn't, unfortunately it could be lots of different issues you'll need to diagnose separately. However, this guide should get you 99% of the way there. Here are a few common issues that are easy to fix though:

  1. You typed in something wrong. Double check the config files and ensure everything matches EXACTLY (aka everything is case sensitive).
  2. You're missing plugins or other required files. Read any error messages to see if they tell you what's missing and check your browsers Inspector -> Console for additional details.
  3. The site loads but pages are broken. Login to wp-admin and open Settings -> Permalinks. Don't change any values and simply Save this screen. It will flush all the existing permalinks and will often magically fix broken pages!
  4. Pages are linking badly. You may need to run a search-and-replace on https://www.mywebsite.com/ and replace it with http://localhost/folderName/ to fix all the broken links.
  5. Logins, passwords, or other changes aren't working. Remember to periodically stop and restart Apache and MySQL in case a config file was changed, but not reloaded.
Good luck! If I missed anything obvious, please let me know in the comments and I'll update the guide. Thanks!