WordPress is one of the most widely used platforms for blogging in the world wide web. Sometimes you may plan to move WordPress website from one hosting to another hosting, this is what we call as ‘WordPress Migration’.
There are lots of ways to migrate a WordPress site. Lots of plugins can do it. You can do it over simple (S)FTP .I’m going to cover one way of WordPress migration i.e, by using command line tools as this would help you irrespective of the control panel you are using.But that does come with an important provision, you need to have access to a shell (via a simple local terminal or SSH) to both ends of the migration.But if you’ve got that, this way is a great deal faster.
Let’s discuss the steps involved in WordPress Migration.
1. Login Via SSH To Your Existing Web Hosting Account
SSH (Secure Shell) is a network protocol that allows you to securely access your server over an encrypted connection.Login to your existing web hosting account. The basic command to do this is:
“`
ssh username@domainname.com or ssh username@ipaddress
“`
If the port is other than the default, add the port number also with the command using the option ‘-p’.
eg :ssh -p 2222 username@ipaddress
2. Backup your Files
It’s best practice to always backup your website before making any changes.To create the backup you can compress your entire folder using the following command:
“`
tar -cvzf wpback.tar.gz public_html
“`
(wpback.tar.gz is the compressed file) ie [Compressing directory to single file]
3. Transfer your Files
Once you are logged into your existing web hosting account, you can start the transfer process. Before you can initiate the transfer though, you need to know where your content is located on the existing server, and where you want to put the content on the new server. In most cases, everything will be located in the public_html folder. You will be making the transfer using the SCP command, which stands for Secure Copy.
Type: pwd to figure out where you’re at on the server:
“`
pwd
/home/betsy
“`
Type: cd public_html to “change directory” to public_html:
“`
cd public_html
“`
Type: pwd again to verify your location on the server and to get the full address location:
“`
pwd
/home/betsy/public_html
“`
The address location will be needed when you enter the command to start the transfer. Verify the location you want to transfer your content to on your new server. In most cases, the location will be the same, public_html. To start the transfer use this command, but be sure to modify it to match your username, addresses, and locations for both servers:
“`
scp –P 2200 -r source_folder/location/ username@serveraddress.com: /destination_folder/
“`
The username and server IP would be of your destination server.
You can also use sftp to transfer files.
4. Backup the Database
WordPress stores data in a MySQL database. You’ll need to move this database to move the site. You’ll need to back up the database from the old site first.
You can export the database with the following command.
“`
mysqldump -u [username] -p [database_name] > [name_your_backup_file].sql
“`
(Refer the screenshot attached mysql4.png)
The username isn’t the same here as your SSH login, it’s your MySQL access username. The database name you can get from the `wp_config.php` file if you’ve forgotten. It will also ask you for a password after this command, which is the database password, which you can also get from the `wp- config.php` file.
This will create a file (which you named in the last part of that command) which is a complete backup of your database.
Now you need to transfer your database for this you can use scp command
“`
scp –P 2200 [backup_file].sql [username]@[server-ip]:[backup_file].sql
“`
The usename and server IP would be of your destination server.
5. Extract your Files
As you need to extract your files in the destination server, you need to login to your destination server via ssh first. Now, you can extract the compressed file ( here; wpback.tar.gz) to the document root of your new website, (public_html in most of the cases) using the following command
“`
tar -xzvf wpback.tar.gz public_html
“`
Also, change the ownership of the public_html as newusername.nobody and all the files and folders within the public_html folder as newusername.newusername and using the following command
“`
chown betsy.nobody pubic_html
chown -R betsy.betsy public_html
“`
As of now ensure that the default permissions of files and folder as 644 and 755 are correctly updated.
6. Import the database
To import the database first you need to login to MYSQL as root user using the following command.
“`
mysql -u root -p
“`
Now you need to create new database and user. (Refer the screenshot attached mysql3.png)
Once you created new user and database, login to the mysql as the new user and follow the steps to import the database.
(Refer the screenshot attached mysql5.png
7.Edit the wp-config file
WordPress stores your database information in the wp-config.php file. Without this information, your WordPress website will not work, and you will get the ‘error establishing database connection‘ error. So you will need to edit the
wp-config file with the new user, database, and the password.
- Change the database name:
“`
define(‘DB_NAME’, ‘db_name’);
“`
- Change the Database Username:
“`
define(‘DB_USER’, ‘db_user’);
“`
- Change The Database User Password
“`
define(‘DB_PASSWORD’, ‘db_pass’);
“`
8. Changing the SIte URL and Home URL
If you are moving to a new/different domain be sure to replace <old_url> with
<new_url> .If not, then you can skip this because you don’t have to update your site to point to a different domain.
The following steps will help you to find and replace all the old URLs with the new URLs.
“`
mysql -u wp_user1 -p Enter password:
“`
Use the database named “store-db”
“`
mysql> use store-db;
“`
Database changed, Show the tables
“`
mysql> show tables;
+————————————-+ | ||
| Tables_in_store-db | | | |
+————————————-+ | ||
| wp_commentmeta | | | |
| wp_comments | | | |
| wp_customcontactforms_field_options | | wp_customcontactforms_fields | | wp_customcontactforms_forms | | wp_customcontactforms_styles | | wp_customcontactforms_user_data | | wp_links | | wp_options | | wp_postmeta | | wp_posts | | wp_term_relationships | | wp_term_taxonomy | | wp_terms | | wp_usermeta | | wp_users | +————————————-+ 16 rows in set (0.00 sec) |
“`
mysql> select * from wp_options where option_value = ‘http://www.domain1.com/wordpress’;
“`
“`
mysql> update wp_options set option_value = ‘http://www.domain2.com’ where option_id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
“`
“`
mysql> select * from wp_options where option_value = ‘http://www.domain1.com/wordpress’;
Empty set (0.00 sec)
“`
mysql> select * from wp_options where option_value = ‘http://www.domain2.com.com’;
+———–+———+————-+————————+———-+
| option_id | blog_id | option_name | option_value | autoload |
+———–+———+————-+————————+———-+
| 1 | 0 | siteurl | http://www.domain2.com | yes |
+———–+———+————-+————————+———-+
1 row in set (0.00 sec)
“`
9. Changing the DNS Records
Before you can use the site on your new host you will need to check whether all the contents and databases are correctly updated on the new website. You need to forcefully load your website from your local machine, for this you need to add host entry in your local machine.
“`
To add hosts entry :
1) In a Windows machine, Add entry in this file : %SystemRoot%\system32\drivers\etc\hosts
(where %SystemRoot% is C:\Windows in a Normal case, change it accordingly)
2) In a Linux machine, Add entry in this file : /etc/hosts
3) In Mac OS, Add entry in this file : /private/etc/hosts
The entry is:
———————————-
<IP> domain.com www.domain.com
———————————-
“`
Please keep in mind to remove this entry once testing is done.
After confirming that your website is working fine on your local machine, you need to reconfigure your domain’s DNS settings. They will be set to point to your old host and you will need to point the correct records to the new server IP address. This process will depend on where you have your domain registered. DNS changes can take up to 48 hours to fully propagate. During this 48 hour, you should avoid making any changes to your website as you may be changing the old version of the site.
Though WordPress migration seems to be difficult it would be easy if you follow the above steps correctly.