22

Copy your WordPress site to a local server for development and testing

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.

mamp-start-servers

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.

New directory in localhost

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.

phpMyAdmin tab

Once in phpMyAdmin, click the “Users” tab, then “Add user”.

Create New Database

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.

Add Database User

Your database should now appear in the list of databases in the left column.

Database List

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).

WP Migrate DB Settings

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.

New Database

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.

Import Database

When the database has finished importing, you should see the list of tables in the left column.

Database Tables

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.

WordPress Core Files List

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).

WordPress Configuration Settings

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.

WordPress Configuration - Keys and Salts

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.

wp-config table prefix

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.

Download wp-content directory

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.

 

 

 

Comments 22

  1. good job, I was looking for soemthing like that. I expected that would be somehow reverse engeneering from local host to live web host.

    Anyway I have to do it know. If someone asks you why it is usefull, I would say that you can have your computer and externarl hardware stolen and you just had the last week package for your duplicator. Anyway thx, I have to do it know

  2. Hello,

    First of all, thank you so much for this tutorial. I’ve been trying for three days to install my wordpress site (which is brand new, I haven’t populated it with much content whatsoever) following wordpress.org instructions and they were a nightmare. I find your instructions were the simplest – the visuals helped a ton.

    So I created a child of the theme I’m using (“untitled”) because there are a few things I’d like to change and add to the theme’s layout. So far, the only item under the child theme has been the header.

    I followed your instructions to the T. But at this point:
    “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.”

    What I get is this: http://localhost:8888/mywordpress/wp-admin/install.php

    I don’t know what to do. I don’t think this is right, am I correct?

    I’d GREATLY appreciate your help on this one.

    Thanks!

    1. Post
      Author

      Hi Elena,

      Thanks for your comment! I’m glad you found the tutorial helpful.

      The install page should only appear if it doesn’t recognize the database. Check that the database from the public site was imported, and that the wp-config settings are correct. If they are, the site should appear.

      Let me know how it goes!

  3. Hi Rob,

    Thanks for your prompt reply.

    Please be patient with me; this is the very first time ever I do this. I’m trying to learn as much as possible but sometimes I need layman terms.

    How can I check that the database from the public site was imported? And by public site, you mean, my live wordpress site? I checked the wp-config, this is what I got:

    // ** MySQL settings – You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define(‘DB_NAME’, ‘mywordpress’);

    /** MySQL database username */
    define(‘DB_USER’, ‘root’);

    /** MySQL database password */
    define(‘DB_PASSWORD’, ‘root’);

    /** MySQL hostname */
    define(‘DB_HOST’, ‘localhost’);

    /** Database Charset to use in creating database tables. */
    define(‘DB_CHARSET’, ‘utf8’);

    /** The Database Collate type. Don’t change this if in doubt. */
    define(‘DB_COLLATE’, ”);

    Thanks!

    1. Post
      Author

      Hi Elena,

      Yes, by public site I mean your live site. To check that the database was imported, review Step 4 in the tutorial. If you’ve followed those instructions and can see the list of database tables, then your database has been imported. Also see Step5, #7 and make sure the table prefix matches your database. Assuming the database name is ‘mywordpress’, then the wp-config code you pasted looks correct.

      1. Hi Rob,

        Thanks for your reply. I tried to carry out your suggestions, but now all I get is “error establishing a database connection.” I tried in both wp-login and wp-admin. I can’t even log into the wordpress.org dashboard. I’m clueless here 🙁

        1. Post
          Author

          Hi Elena,

          Sorry you’re still having trouble. At this point, it might be best to start over again from the beginning…. Happens to the best of us. 🙂

          I put together a video walk-through, which you may find helpful. Sometimes it’s easier when you see it in action. http://youtu.be/-88o-PXlhzg

    2. I know this comment thread is a bit older, but I wanted to comment that I found this tutorial very helpful.

      I am a seasoned techie, and I also got the ‘error establishing a database connection’ error once I finished. I realized that when I created the database user, I left the ‘host’ as ‘any’, but I had to go back in and change the host to ‘localhost’ to correct the connection error. Wanted to leave this note here for anyone like me who sees that error.

      Cheers!

      1. Post
        Author
  4. Thanks for your instructions. I’m having trouble with this, and I really can’t figure out why. I’ve tried it at least 3 times, following different instructions (yours this last time), but I’m not getting past that final step.

    When I plugged the url for my local site into the browser (firefox) this time, I got a “Not Found” error: “The requested URL /dbname was not found on this server.” I actually think this may be closer than the results I’ve gotten before (maybe). Before (with an earlier set of instructions, I put in that url (http://localhost:8888/dbname) and it asked me if I wanted to open or download a document. Certainly don’t know why that’s happening. At least it’s not doing that anymore…

    Again, I’ve tried to set up a live website on my local computer at least 3-4 slightly different ways and it seems like it should be working, but it’s not. I’m (often) able to get WP to open locally, but I can’t access any of the website’s files. I’ll be very grateful for any help you can provide.

    Thanks.

    1. Post
      Author
      1. Thanks so much for replying, Rob, but unfortunately it didn’t help. At this point it’s also been a few days so I have no idea where I left off or what steps I did or didn’t take.

        I think I had named the site and the db the same thing (in this case, I called it ‘swingtest’). So I logged into MAMP and then tried to access http://localhost:8888/mamp/swingtest and then http://localhost:8888/mamp/htdocs/swingtest/ because that’s the thread of where I saved the files on my computer (both WP files and the database files, which overwrote some of the WP files), but I still got the Not Found error.

        I’m sure it’s too difficult to figure out without looking at my computer, but I thought I’d ask about it one more time. Thanks so much for your help.

        1. Post
          Author

          Hi Renee,

          I think you’re confusing the url with the file path. Try again with http://localhost:8888/swingtest (i.e. remove /mamp/ or /mamp/htdocs/ from the url). That’s a fairly common mistake.

          If you still don’t get it to work, I’d suggest starting over from the beginning. As you say, at this point you don’t know where you left off or what you did or didn’t do. Check out the the video screencast I put together, that I mentioned in my reply to Elena yesterday. That might help too.

  5. Thanks so much Rob. The URL you suggest is what I had initially, and still isn’t working, so I’ll go back and try the whole process again tomorrow with your video. (Video! Awesome!) Hopefully that will help.
    Wish me luck! 😉

  6. Actually, if I may ask another question: since I’ve tried this a couple times now, I have these various files on my hard drive in various places. Should I try to go back and delete the folders/files? Should I uninstall MAMP? Or just not worry about it and start from the beginning anyway?

    1. Post
      Author

      Hi Renee,

      Apologies for not replying sooner. It’s been a busy week!

      Any luck yet? No need to uninstall MAMP, but you may want to delete the local directory and database to keep things organized (of course, make sure the directory isn’t your only copy. It should at least still be on your live site), then start over from the beginning.

      Good luck!

  7. No problem Rob, I haven’t had a chance to try it again until now. Problem is, it’s still not working. I officially give up.

    Thanks for your help though. I do appreciate it.

    Renee

  8. Hi Rob,

    Everything worked like a dream… save the image files. They’ve all imported into sub directory called image-files in the WP-content folder. However, all of the image file location (file urls are incorrect) and I am also unable to upload any new images. Any ideas?

    Amanda

  9. Spot On – was looking for some simple instructions and now have a working dev server on my computer. THANK YOU.

    Also – the tip re saving changes to permalinks worked like a dream when my site initially came up blank.

    Really appreciate this post.

    Nisha

    1. Post
      Author
  10. Thanks for such a clear and concise tutorial Rob.
    Just to warn people. I’ve been battling with MAMP for three days now trying to download my existing site following various online tutorials and absolutely none have worked.
    Each time I have started from scratch including deleting MAMP and re-installing it, trying two different browsers and each time I get the ‘error establishing a database connection’ message.
    The issue I suspect is phpMyAdmin within MAMP. Each time I get the same two issues.
    1/ Re: Step 2: Create a new local database – When entering log-in info phpMyAdmin won’t allow me to “leave the “Host” field empty”. It currently auto fills it with ‘%’ , if I try and delete the%, it highlights with a red border.
    More importantly
    2/ Again when on Add User I get a “A fatal JavaScript error has occurred…..”
    At the moment I’m just going to give up and maybe try at some stage in the future. Way too frustrating and way too much time wasted. I need a drink.

Leave a Reply

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