How to migrate WordPress from one host from another?

WordPress really SUCK! It doesn´t provide a viable, simple and easy solution for export/import of your whole site. Here I am sharing how I managed to migrate my WordPress site from an old host to a new one.

  • make sure Apache2, PHP, MySQL, and PHP MySQL extension work in perfect on your new host. Here is one instruction: http://www.tecmint.com/install-wordpress-on-ubuntu-16-04-with-lamp/
  • bear in mind that WordPress saves all static contents (style, theme, uploaded images, etc) in your file system. So make a copy of WordPress folder on the old host located in /var/www/html/wordpress_folder.
  • since WordPress stores all dynamic contents in mysql database (e.g., users, articles), you need to export that database to ¨.sql¨ file. Let’s suppose in /var/www/html/<wordpress_folder>/wp-config.php, we have:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'your_dbname');
     
    /** MySQL database username */
    define('DB_USER', 'your_usrname');
     
    /** MySQL database password */
    define('DB_PASSWORD', 'your_password');
     
    /** MySQL hostname */
    define('DB_HOST', 'your_hostname');

    Then, on the machine that hosts your wordpress website, you can do the export by:

    mysqldump -u your_usrname -p -h your_hostname your_dbname > wordpress_bakup.sql

    which will complete after receiving your_password.

  • Open the exported ¨.sql¨ file, replace all the old site urls to the new site urls, then save the ¨.sql¨ file. For example, in `vim` you can use:
     :%s/<old_ip_addr>/<new_ip_addr>/g
  • on the new host, put the old host static content under the same path relative to html root directory.
  • on the new host, import the ¨.sql¨ to a newly created database with the same name. (You may need to create a database called your_dbname first. This can be done my first logging in mysql by <span class="lang:default decode:true crayon-inline">mysql -u usr -p</span> , then create database your_dbname;  )
    mysql -u usr -p your_dbname < your_bak.sql
  • change ¨wp-config.php¨ accordingly so that DB password and username match those on the new host.
1
2
3
4
5
/** MySQL database username */
define('DB_USER', 'usr');
 
/** MySQL database password */
define('DB_PASSWORD', 'pwd');
  •  change the permission of `wp-content/uploads`, `wp-content/upgrades`, `wp-
  • content /plugins` folders to 777.
  •  add “define(‘FS_METHOD’, ‘direct’);” in the `wp-config.php`.

That´s it. Enjoy it.

—————————————————————————————————–

Updated on Aug 24th, 2015:

If you ever encounter file permission errors during updates or anything else, change permission of the whole wordpress directory as well as the files in it to 777. Do the update. And then change the permissions back as per the official document suggests:

For Directories:

find /path/to/your/wordpress/install/ -type d -exec chmod 755 {} \;

For Files:

find /path/to/your/wordpress/install/ -type f -exec chmod 644 {} \;

—————————————————————————————————–

Updated on April 8th, 2019

Another way to migrate wordpress:

Use Tools -> Export on the old site, and Use Tools->Import on the new site. Up to this point, the new site should have the identical content as in the old site. To update post contents that contain the old site’s URLs, use phpAdmin or mysqldump to export the database to an SQL file. In this file, replace the old site’s URLs with the new ones. Import the updated SQL file again using phpAdmin or mysqldump.

—————————————————————————————————-

Updated on May 6th, 2019

I just found this plugin is very useful

All-in-One WP Migration

Leave a comment

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