KNOWNHOST WIKI

User Tools

Site Tools


control-panels:cpanel-whm:how-to-copy-wordpress-from-one-cpanel-to-another

How To Copy a Wordpress Site From One cPanel Account/Domain to Another?


1. Log in as Root via SSH

The following assumes that you will be working as root via SSH. Knownhost uses port 2200 by default for SSH. Use the following syntax to log in as root via SSH, but replace IP.IP.IP.IP with your server's actual IP:

  ssh root@IP.IP.IP.IP -p2200


2. Create the New Account

Creating a New Domain

If you've not already created the new cPanel account/domain that you'd like to use, do this first. You can use the 1)whmapi or the 2) wwwacct script to do this rather easily. The whmapi takes many possible parameters, but the following example only uses the required parameters for the sake of brevity.

  whmapi1 createacct username=username domain=example.com

The output will contain the account's password (created by the panel) and other details that were set by the panel based on configured defaults. If you want to make other changes, you can do so afterwards via WHM's Modify an Account interface.

If using the wwwacct script, it uses the following syntax:

  /usr/local/cpanel/scripts/wwwacct domain user pass


Creating a Staging Subdomain

If you are wanting to create a staging subdomain, you can use the following 3)api to quickly create the subdomain from within root's SSH shell:

  uapi --user=username SubDomain addsubdomain domain=subdomain rootdomain=example.com


3. Copy the contents from the old site's document root to the new site's document root

You'll need to know the first site's username and document root to proceed.

To find the site's owner, use this command where domain.tld is replaced with your site:

   /scripts/whoowns domain.tld

To find the documentroot of the site, use this command, replacing username with the username you just found and domain.tld with the domain:

  grep documentroot /var/cpanel/userdata/username/domain.tld

Now that we have created an account and have identified the site's document roots, we can use the following sequence of commands to create a copy of a site's files/directories at a different site's document root and update the ownership accordingly (these commands should be adjusted accordingly for your specific site's document roots and usernames!):

  cp -a /home/old/public_html/* /home/new/public_html/
  cp -a /home/old/public_html/.htaccess /home/new/public_html/.htaccess
  chown -R new.new /home/new/public_html/


4. Create a Database Backup for the Site

First, you may need to know the database's name. You can get it like so:

  grep DB_NAME  /home/old/public_html/wp-config.php

Now that you know the database name, you can make a backup of it using the following command:

  mysqldump old_website > old_website.sql


5. Create the New Database and User for the New Account

Make a new database user, username, and password in new's cPanel. You can do this via cPanel's MySQL Databases interface, or you can use the uapi commands via the root ssh shell like so:

   uapi --user=new Mysql create_database name=new_db
   uapi --user=new Mysql create_user name=new_admin password=p455w0rd
   uapi --user=new Mysql set_privileges_on_database user=new_admin database=new_db privileges=ALL


6. Now Import the Database Backup Into the New Database

Use the following command to do this:

  mysql new_db < old_website.sql


7. Update the WordPress Configuration File with the New Database Connection Credentials

You will need to update the wp-config.php file to use the new database details:

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

  /** MySQL database username */
  define('DB_USER', 'cPuser_dbuser');

  /** MySQL database password */
  define('DB_PASSWORD', 'password');


8. Update the Database Contents To Replace All Instances of the Old URL with the New URL

You will find useful commands online often to update the siteurl and home values in the wp_options table, but the following should take care of these and so much more.

Let's use WP-CLI's search-replace tool to first perform a dry-run and see how many replacements of the old domain with the new domain will occur. Make absolutely certain that you are in the new document root when you run this command!

I would then review the output from the following, which would show all findings of the old url that would be replaced with the new url when using wp-cli:

  su -s /bin/bash -c "/usr/local/cpanel/3rdparty/bin/wp search-replace old.com new.com --dry-run" new

Above, new represents the new cPanel user's username.

Run the following to proceed with replacing the old url with the new url in the newly created database containing the import of the sql backup I old from the old site.

  su -s /bin/bash -c "/usr/local/cpanel/3rdparty/bin/wp search-replace old.com new.com --skip-columns=guid" new

Now, WordPress warns against changing the 4)GUID. This mainly applies to RSS feeds and feed-readers. These feeds are enabled by default. If you have users that use a RSS feed for getting updates from your site, if we change the GUID to the new URL, they will be flooded with old posts as if they were new. This is why WordPress warns against changing the GUID column.


9. Update any References to the Old URL in the .htaccess to the New URL.

Depending on how many there are, you may just do this manually, or write a sed command to accomplish it like so:

  sed -i.bak 's/old.com/new.com/g' .htaccess

The command above should make a backup .htaccess before applying the changes. The backup .htaccess will have .bak appended to it like so:

  .htaccess.bak


10. Make a Backup of the Old Account Before Removing It

Lastly, I would make a backup of the old account before deleting it just in case.

  /scripts/pkgacct old


An Alternative Step to Consider

The option exists for creating a sql dump from the jump with the new url replacing the old url. 5) wp search replace

  wp search-replace foo bar --export=database.sql

Using this command could combine steps noted above (particularly steps 6 and 8), further speeding up and simplifying the process.

If you have any questions about this process, or require assistance, please provide the link to this wiki article, the source and desired destination domains and usernames, and your support request to your KnownHost support team. :)

control-panels/cpanel-whm/how-to-copy-wordpress-from-one-cpanel-to-another.txt · Last modified: 2020/06/02 08:30 by Karson N.