Migrate WordPress website

This page tells you how to move your WordPress site to another server. There are many ways to do it. Some plugins like “All In One WP Migration” can do it easily.

Before (or after) all this you need to do the following:

  • Change DNS records in your Domain Manager website. This is the place where you bought the domain name. It should point to the destination hosting, its DNS server.
  • In your destination hostings DNS manager, you add the domain. If there are multiple IP addresses you need to point which IP you use (A record).
  • Add the domain to the destination hosting. Usually, this is done in a cpanel software

In my environment, the first step is done on the namesilo.com website where I host my domains. The second step is done in the time4vps.com website’s DNS manager. That is the place where I have bought my virtual machines. The third step is done in the CWP (Control Web Panel) web interface of my virtual machine. The second and third step is often done in the same cpanel interface where you bought your hosting.

These instructions tell how to do the migration with simple command-line steps. You need access to both site servers Linux command line.

Simplified you need to do just three things: (1) copy all files, (2) copy the database, and (3) turn domain to point to the new server.

DOMAIN=

The domain root directory is the same than domain name:

cd $DOMAIN

Copy all files

In the source go to the domain root directory. It is the one that has .htaccess and wp-config.php files. In that directory give the command:

tar cfpz ../$DOMAIN-all-wp-files.tar .  

Tar options: c=create, f=(to)file, p=preserver permissions, z=compress

Option z is not necessary, it just creates a smaller file. If you have a fast connection and a slow server you can remove it.

Dump database

In the domain directory give command:

DOMAIN=
cat wp-config.php | grep DB 

You should see at least following lines:

define(‘DB_NAME’, ‘how2osDB’);
define(‘DB_USER’, ‘how2osUser’);
define(‘DB_PASSWORD’, ‘how2osPass’);

WPDBNAME=`cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`

echo "Domain: $DOMAIN, User: $WPDBUSER, Database: $WPDBNAME, Password: $WPDBPASS"
To check that all parameters are OK.

Dump database to a file

mysqldump -u $WPDBUSER -p$WPDBPASS $WPDBNAME >../$DOMAIN-db-copy.sql

echo 'DOMAIN='$DOMAIN';WPDBUSER='$WPDBUSER';WPDBNAME='$WPDBNAME';WPDBPASS='$WPDBPASS >../$DOMAIN-variables.sh

Transter

Transfer the file to the destination servers domain directory. You can use i.e. sftp (sftp -sPort= ) There you give the command (as user, not root):

tar xf all-wp-files.tar

Dump the database to the destination

Add here instructions from transfer procedure how to create user.

Create empty database as root.

echo "CREATE DATABASE $WPDBNAME;" > create-$DOMAIN-db.sql
echo "use $WPDBNAME;"  >>  create-$DOMAIN-db.sql
echo "GRANT ALL PRIVILEGES ON $WPDBNAME.* to \"$WPDBUSER\"@\"localhost\" IDENTIFIED BY \"$WPDBPASS\";" >>  create-$DOMAIN-db.sql
echo "FLUSH PRIVILEGES;"  >>  create-$DOMAIN-db.sql
echo "quit"  >>  create-$DOMAIN-db.sql
mysql < create-$DOMAIN-db.sql
rm create-$DOMAIN-db.sql

Add content to the database as user:

cat $DOMAIN-db-copy.sql | mysql -u $WPDBUSER -p $WPDBNAME