Moving a WordPress site

It is a very common scenario to have to move a WordPress site across servers (when changing hosts for example) or to have to upload a “local” installation from your computer to a live server. In this quick tutorial we will look at how to backup and move a site from a user’s computer to one of our hosting packages but the same principles apply to server-to-server moves too.

Moving a WordPress site will involve the following steps:

  1. Back up files
  2. Export database from local server
  3. Upload files
  4. Create database on server
  5. Import database backup into newly created database
  6. Edit config file
  7. Run script to replace URLs in database

Back up files

The first thing to do before we move the site is creating a backup of the site’s files. This is not only good practice but it also gives us a backup of the site at that point in time, which could come in very handy in the future.

The most common way to backup files is simply to create a compressed “archive” using a popular tool such as zip. On most operating systems you can simply select all the files, right click (or control + click) on them and you should have options to “zip”, “archive” or “compress” them. This process should create a single archive file containing all of the site’s files.

Figure 1: Archiving a WordPress site’s files on the Mac.

Export database from local server

The easiest way to export a MySQL database is using the phpMyAdmin web based interface. phpMyAdmin comes as standard with most LAMP, MAMP and WAMP installations.

Open your local phpMyAdmin interface (normally found at a URL such as http://localhost/phpmyadmin but this depends on your installation).

Select the database from the list on the left hand side. This will display the database structure. In the top menu select the “Export” tab. At the bottom of the export page check the option to save as file and compression, we recommend bzip2 compression for SQL scripts.

Figure 2: Exporting a MySQL database using phpMyAdmin.

If you don’t know which database your WordPress site is using you can always check in the wp-config.php file. In that file you will find a section like the following where you can see your database credentials (db host, db name, db user and db pass):

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', '[your db name]');

/** MySQL database username */
define('DB_USER', '[your db user]');

/** MySQL database password */
define('DB_PASSWORD', '[your db password]');

/** MySQL hostname */
define('DB_HOST', 'localhost');

Upload files

Now that we have a full backup of the site (files and database) we are ready to start uploading to the remote server.

In this example I will use the hosting control panel’s “File Manager” to upload the archive we created in the first step and then extract it on the server. In the hosting control panel’s home page locate the “File Manager” icon and click on it.

Figure 3: The file manager can be accessed under the “Files” heading in the hosting control panel.

Before we start make sure that your public_html folder is empty. If there are any files you may want to remove them first. You can delete files by right clicking on them in the File Manager and clicking on the “delete” option.

Upload archive into public_html folder.

Figure 4: Uploading the archive using the file manager.

Once the archive is on the server right click on it and choose the option to “Unzip” it.

Figure 5: Extracting the archive using the file manager.

After the archive has been extracted right click on it to delete it as we don’t need it anymore and there is no need to keep it on the server.

Figure 6: Deleting the archive using the file manager.

Create database on server

In the hosting control panel click on the “MySQL Databases” icon under “Web Tools”:

Figure 7: The “MySQL” icon is located under the “Web Tools” heading.

In this screen you will be able to create a new database into which we will import your database backup. Choose a “username” and “password” and then click on “Create”.

Figure 8: Creating a MySQL database from the hosting control panel.

Bear in mind that the database username (and database name) will have a prefix added. So after you have created the database check the resulting name. Scroll down the page and you will see an entry for the new db. You will need to remember this details for later when we edit the configuration file, so write them down.

Figure 9: The phpMyAdmin interface can be accessed by clicking on the “Now” button on the “Manage” column for each database.

Import database into live server

Now click on the “Manage” button next to the newly created database. This will open the phpMyAdmin interface in a new window. In the left hand side of the screen select the relevant database. In this exampe my database name is web168-mdl.

Figure 10: On the left hand side of phpMyAdmin you will see two databases listed, one is your database, the other one is used internally by MySQL.

Now click on the import tab:

Figure 11: Importing a database using phpMyAdmin.

Click on the “browse…” button, choose the file you exported from your local database and click on the “Go” button in the bottom right corner.

Figure 12: When the database is imported correctly you should see a message saying so.

Edit config file

Now that we have created the database and imported the backup we need to update the WordPress configuration file with the new database credentials. In this example I will use the “Edit” function in the control panel’s File Manager to edit the file on the browser. You could always edit the file locally and then upload it via FTP.

Figure 13: Editing files using the file manager in the hosting control panel.

Find the section with the MySQL settings and replace the username, database name and password with the details as shown next to your database in the control panel under MySQL databases. Note that the database name is the same as the database username.

Figure 14: WordPress stores MySQL credentials in a file called wp-config.php.

Run script to replace URLs in database

Finally, if the site’s URL has changed (and in this example it would have), we will need to update all references to the old URL with the old host in the database. WordPress stores “absolute” URLs in the database and this could be very tedious, but we have put together a script to do exactly that. You can download the script here as well as instructions on how to use it. Note that you only need to download a single file called wp-update-urls.php.

Figure 15: Using the wp-update-urls.php script to update URLs stored on the WordPress database.

Check that everything works

Browse your site and check that everything works.

Figure 16: If you’ve managed to follow this far, you will hopefully be able to see your website now.