Creating a copy of your WordPress website on a local server is useful for developing and testing changes to the site before they go live. It also serves as a decent backup should anything bad happen to the public site.
Making an exact copy of an existing website can be tricky, though. I’ve done it for a number of client and personal WordPress sites lately, so I thought I would share my process. Hopefully it will help make the job easier for you.
Here’s what we’ll be using to copy your WordPress site:
- A local server. I’m working on a Mac, so I’m using MAMP. The Windows counterpart is WAMP, and on Linux it’s LAMP. (The process for non-Mac users will vary slightly, but the differences are fairly minor. You should still be able to figure it out);
- The latest version of WordPress;
- A current copy of your site’s theme, plugins and media files (basically the contents of the wp-content directory).
- The WP Migrate DB plugin for moving the database;
- A text editor (I’m using Coda); and
- An FTP client (I’m using Filezilla).
Step 1: Create a local directory for your website
If you haven’t already, download MAMP and follow the instructions for installing it on your computer.
Launch MAMP, and click the “Start Servers” button on the start menu. After a few seconds the status display should show green buttons next to each service.
Next, go to Applications > MAMP > htdocs. The htdocs folder is similar to the root directory on your web server. Create a new subdirectory for your site under htdocs.
Step 2: Create a new local database
Return to the MAMP start menu and click the “Open start page” button.
From the menu at the top of the page click the “phpMyAdmin” tab.
Once in phpMyAdmin, click the “Users” tab, then “Add user”.
In the window that pops up, complete the “Login Information” and “Database for user” sections. “Username” can be anything you want, leave the “Host” field empty, then click the “Generate password” button to have phpMyAdmin automatically create a strong password for you. Save this information somewhere – you’ll need it later.
Next, select the radio button next to “Create database with same name and grant all privileges”, then click “Add user” to create the user and database.
Your database should now appear in the list of databases in the left column.
Step 3: Export your database
Login to your WordPress website.
Install and activate the WP Migrate DB plugin.
Access the WP Migrate DB manager by going to Tools > Migrate DB.
Complete the “New address (URL)” and “New file path” fields. The new address is the url to your localhost site (e.g. http://localhost:8888/my-awesome-website), and the file path is the path to the directory you created under Step 1 (e.g. /Applications/MAMP/htdocs/my-awesome-website).
Click “Export Database” to download the database (take note of where the file gets downloaded on your system. You’ll need it in the next step).
Step 4: Import your database to the local server
Now we need to import the database file to the local server. Currently the database is empty. To populate it, we’ll import the sql file we downloaded in the previous step.
Return to the database you created in Step 2.
Click the “Import” tab, then “Choose File”. Locate the database file on your computer and click “Open” (the file name should now appear next to the “Choose File” button). Now click “Go” to import the database.
When the database has finished importing, you should see the list of tables in the left column.
Step 5: Install WordPress on your local server
Now that the database has been imported, we need to connect it to WordPress.
Download the latest version of WordPress.
Decompress the WordPress .zip file, open the folder titled “wordpress” and copy and paste the contents of the folder (not the folder itself) to the directory you created in Step 1.
Locate the file called “wp-config-sample.php”, rename it to “wp-config.php”, and open it in a text editor. This is where we’ll configure the WordPress installation to work with your database.
Find the MySQL settings section of the file. Replace “database_name_here” with the name of the database we created in Step 2 (e.g. ‘myawesomewebsite’), replace “user_name_here” with “root”, and replace “password_here” with “root”. (For wamp and lamp users: the username and password should be the ones you chose when creating the database).
Next we need to generate the Authentication Unique Keys and Salts. In the section immediately below the MySQL settings, copy the url noted in the comment and paste it into a browser.
This will generate a unique set of keys and phrases. Copy all of it and paste it over the placeholder code.
The last section we need to look at in the wp-config file is “WordPress Database Table Prefix”. Take a look at the tables we imported to the database in Step 4. If the tables are prefixed with “wp_“ then we’re good to go and nothing needs to be changed here. If the tables are prefixed with something else (e.g. “maws_”), then change “wp_” to the correct prefix for your database.
Save the wp-config.php file.
Step 6: Install the theme, plugins, and uploads files
Lastly, we need to download the theme files, and any plugins and media files from the server.
Login to your FTP client and access the server for your live site (if you need to configure the FTP client, your web host should have instructions). Navigate to the “wp-content” folder in your WordPress installation.
Download the contents of “wp-content” from the live site to the “wp-content” folder on your local server. This folder includes your theme directory, plus any plugins, media uploads, and sometimes other directories used by certain plugins. (Note, this may take several minutes if you have many large files). Be sure to select the option to overwrite the local files if prompted to do so.
Now, plug the url for the local version of your site into a browser (e.g. http://localhost:8888/my-awsome-website) and you should see an exact copy of your WordPress website. You should also be able to login to the local site using the same username and password that you use to login to your live website.
Common troubleshooting tips
Sometimes the local site doesn’t work exactly as expected when you first load it. Here are some common issues I’ve come across, and tips on how to correct them:
My homepage shows up, but internal pages don’t work.
This can be fixed by resetting the permalinks. Login to your local site and go to Settings > Permalinks. Click the “Save Changes” button. Now go the the live site and the internal pages should load correctly.
My site doesn’t show up. I get a blank screen.
The most likely cause of this is the theme wasn’t activated. Login to the local site, go to Appearance > Themes, and activate the theme. Reload the homepage and your site should show up.
My content shows up, but the design (colours, typography, graphics, etc) is missing, and I’m unable to login.
Chances are the url and/or file paths weren’t copied correctly during the database migration. (Whenever this has happened to me it’s because I forgot to include the “http://” in the url setting). To correct it, repeat Step 3 (exporting your database), making sure the url and file path settings are correct. Then export this new version of the database, and import it into your local database (it should over-ride the previous database tables). The local site should now display correctly, and you should be able to login.