If you're thinking about cloning your WordPress site and moving it to a new domain, you're in the right place. Keep reading to learn how to clone and migrate your site.
Consider Using the Recommended BlogVault Features
You have the recommended option of using BlogVault to move a domain (site) on Managed WordPress and Managed WooCommmerce Plans. An informative article to help you in this process is Migrating to Nexcess with managed WordPress and managed WooCommerce hosting.
What You Need Before You Can Start
For this process of moving your site to a new domain, you will need to connect to the server using SSH. If you do not know how to use SSH, then the New User Tutorial: Basic SSH Usage article will help you.
As a first step, identify the WordPress site you would like to move and where it is located. This is commonly known as the “Document Root” of the site. Next, identify the target location for the new site. Once you have decided on the new target location for the site move, you will need to create the new and different site via the cloning and moving steps provided.
In this article, we will be moving growblop.com to a subdomain called staging.growblop.com. The WordPress Version is 5.8.2.
Once you are connected to the server by SSH, you will need to change to your WordPress site's directory. We are going to change to its “Document Root” by issuing this command:
cd /home/growblop/public_html
You can view the files and directories under the “Document Root” by using the “ls” command:
ls /home/growblop/public_html
Before Working With the Database
Before working with databases, you should review the related commands that are covered well on the page that documents Developer Resources for WP-CLI Commands > wp db <command> to best understand the available options and details.
For your reference, WP-CLI has commands to reset and import a database:
wp db reset --yes
wp db import wp_growwpgrow_db_cloning.sql
As you review the steps below, consider using the most appropriate commands in the proper sequence.
Working with Databases
Using your favorite text editor such as “Nano”, open up the settings file that would have the database configuration settings. If you are not sure how to use the Nano text editor, the How to Install and Configure Nano on CentOS 8 article will help you.
You will see a file named wp-config.php. Using your favorite text editor (such as Nano) open up this file:
nano wp-config.php
Once you have this file opened, you will need to copy the DB_NAME and store this somewhere temporarily. In this example, we have copied and temporarily stored the database name from the wp-config.php into a notepad page, so that we do not have to open the wp-config.php file again:
define( 'DB_NAME', 'wpgrow_db' );
You will need to make a database dump for this database. If you are not sure how to use the mysqldump command, the How to Backup and Restore MySQL Databases from the Command Line article will help you.
Let’s go ahead and make a database dump for the live database.
Change to this directory:
cd /home/growblop
Make the dump and call the dump wpgrow_db_cloning.sql:
mysqldump wpgrow_db > wpgrow_db_cloning.sql
Cloning Your Site
Next, we will need to create the subdomain. If you are using a cPanel server, you will need to open up cPanel on your browser and navigate to Domains >> Subdomains, and create the new subdomain that you will use for the new WordPress site. Please take note of the “Document Root” location of the new subdomain.
We will need to use a method for syncing the files to the new staging.growblop.com “Document Root”. The most commonly used method is to use rsync. If you are not sure how to use the rsync command, the Install Rsync and Lsync on CentOS, Fedora or Red Hat article will help you. We will now issue this command:
rsync -avH /home/growblop/public_html/ /home/growblop/staging.growblop.com/
Now that the files are copied over to the new WordPress subdomain site, we will have to create a new database, a new database user, and the new database user’s password. If you are using a cPanel server, you will need to navigate to DATABASES >> MySQL Databases. Enter the new Database name in the box and click “Create Database”. Next, under the MySQL Users, enter the new database user and create a strong password with it. Click on “Create User”. Next, you will need to add the permissions for the new database user with the new database. Under the “Add User To Database”, choose the user and the database and click on the “Add” button. This will open a new page. Click on the “ALL PRIVILEGES” check box and click the “Make Changes” button.
You will need to open up the new WordPress site’s wp-config.php file and change the DB_NAME, DB_USER, and DB_PASSWORD to the new database, user, and password that you just created:
nano wp-config.php
Do not forget to save the wp-config.php file and then close the wp-config.php file.
Now that the new database, user, and password are set in the wp-config.php file for the staging site. We can now import the live site’s database dump that we created. My new database is called staging_grow_db. To import the live WordPress site’s database dump file into the new WordPress site’s database, you will need to issue this command:
mysql staging_grow_db < /home/growblop/wp_growwpgrow_db_cloning.sql
We are not done yet. We still need to update all of the domain URL links in the database. The easiest way is to use the WP-CLI utility. You can review the many articles we have available on using the WP-CLI utility. A good article that goes over some handy commands with WP-CLI is Using WP-CLI With Your WordPress Optimized Template Site.
Change to the staging.growblop.com “Document Root”:
cd /home/growblop/staging.growblop.com
It is best practice not to use the WP-CLI while being the root user. If you are connected with the root user with SSH, you will need to change to the domain user. In this case the domain username is growblop:
su growblop
Now that we have switched to the growblop user in our SSH terminal, we can run this command using the WP-CLI to update all of the URLs in the new database:
wp search-replace '://growblop.com' '://staging.growblop.com' recurse-objects --skip-columns=user_email,guid --all-tables
The command above changes all of the instances of http://growblop.com or https://growblop.com to be http://staging.growblop.com or https://staging.growblop.com in the database.
wp rewrite flush
The command above rewrites the permalinks (flushes them) after search-replace has been run, and is well covered on the page that documents Developer Resources for WP-CLI Commands > wp rewrite flush with regard to the available options and details.
Congratulations! You have just cloned and moved your site.
Get Started With Nexcess
Not a Nexcess customer yet? Check out our fully managed hosting solutions to get started today.
Useful Links
- https://help.nexcess.net/74095-wordpress/locating-your-ssh-credentials-in-managed-wordpress-and-managed-woocommerce-hosting
- https://help.nexcess.net/74095-wordpress/pre-migration-checklist
- https://help.nexcess.net/74095-wordpress/how-to-change-wordpress-urls
- https://help.nexcess.net/74095-wordpress/creating-backups-in-managed-wordpress-portal