Migrating a WordPress site from cPanel

Migrating a WordPress site from cPanel

The first thing to do when migrating a WordPress site is to identify everything that needs to be migrated. We will need to copy the site files, and the database, then test the site on the Site Wrangler server before updating the DNS to point to our Site Wrangler server.

Create a New User

First let’s create a new user on the Site Wrangler server for the site. Run this command on the Site Wrangler server to create the new user (replacing example_username with the real username):

sw user create example_username

Get Your SSH Key

Now lets get the public key of our root user on the Site Wrangler server. Run this command to get your public key:

mkdir -p ~/.ssh; if [ ! -f ~/.ssh/id_rsa ]; then ssh-keygen -N "" -f ~/.ssh/id_rsa >/dev/null; fi; echo "Your full public key:"; cat ~/.ssh/id_rsa.pub

Add Your Key to the New Server

Now lets give our Site Wrangler server access to the cPanel account by adding the public key we just got from the Site Wrangler server. In cPanel open the File Manager and click on the settings button in the top right corner. Make sure “Show Hidden Files” is checked and then press Save. Now on the right panel click the text for the folder “.ssh” (not ssh, the one that starts with the dot).

If the file does not already exist, we need to create the file “authorized_keys” by pressing “+ File” at the top left, entering the name authorized_keys, then pressing “Create New File.” Now that we have our authorized_keys file, click on it and press “Edit” in the top gray bar. If you get a pop-up about file encodings, just press “Edit” in that pop-up.

Now copy and paste your public key from the Site Wrangler server into this file and then press the “Save Changes” button at the top right. Then hit “Close” right next to it.

Test Your Connection

Now let’s test that the authorization works. We will need the cPanel username and the IP address of the old account. Both of these can be located on the main cPanel page in the right-side bar. The IP address may be labeled as “Shared IP Address.” From the Site Wrangler server run this command to connect to the cPanel server (replacing the example_username with your cPanel username and 127.0.0.1 with your cPanel server’s IP):

ssh example_username@127.0.0.1

Your Site Wrangler server will say that the authenticity of host ‘example.com (127.0.0.1)’ can’t be established. This is okay. The Site Wrangler server has never connected to the cPanel server and distrusts it by default. Type yes to allow your Site Wrangler server to connect. At this point you will either see a new prompt for the cPanel server or an error message. If you get an error, you have two choices: you can either follow your host’s guide on how to obtain SSH access; or continue on, following the sections that say “without ssh.” SSH is preferred since it is faster and because the command line tool rsync can continue where it left off upon failure and/or copy file updates from the old server to the new server if need be.

Migrating Data

By default, your site in cPanel should be in the folder public_html within your account. This folder is also referred to as the webroot of your site. This guide will use the public_html/ or ~public_html/ path as the webroot, but replace this with the path associated with your site if it is an Addon Domain or Subdomain or if you have customized your webroot path.

It is important that site data is not modified during the migration process. Make all admins do no work during this time and disable any user inputs like comments and uploads. (Site forms that simply send you an email can be left enabled)

Migrating Data With SSH (rsync)

First, on the Site Wrangler server move into the folder for the new user but do not switch to the new user (stay as root):

cd /home/example_user

Now run this command to copy over the site files (this may take a long time, consider using a screen for large sites):

rsync example_cpanel_user@127.0.0.1:~/public_html/ /home/example_sw_user/public_html/

Then also run this variants to copy any SSL certificates if you have them:

rsync example_cpanel_user@127.0.0.1:~/ssl/ /home/example_sw_user/ssl/

We also need the database though, so run this command to dump the database from the remote cPanel server to the local Site Wrangler server:

DB_NAME=$(sed -nr -e "s~//(.*?)$~~" -e "/DB_NAME/ s/^(.*)DB_NAME[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php); DB_USER=$(sed -nr -e "s~//(.*?)$~~" -e "/DB_USER/ s/^(.*)DB_USER[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php); DB_PASS=$(sed -nr -e "s~//(.*?)$~~" -e "/DB_PASSWORD/ s/^(.*)DB_PASSWORD[\"'].*,[ \t]*?[\"']([^'\"]*)[\"']([ \t]*?)\).*$/\2/p" wp-config.php); ssh example_cpanel_user@127.0.0.1 " mysqldump $DB_NAME -u $DB_USER -p $DB_PASS" > transfer.sql

Migrating Data Without SSH (gziped tar)

If you cannot migrate the data with SSH, you can always use zip files to transfer the data. The first thing we must do is dump the database to a file. First look in the file “wp-admin.php” and locate the line with “DB_NAME.” This will tell us which database we need.

Now open phpMyAdmin in cPanel. Now locate and click on the text for the database we are using for WordPress. Click the Export tab along the top and then press the “Go” button (the default options will work great). Save the file for later.

Now let’s get the site files themselves. Go back into the File Manager and click the text for your home folder at the top left (it usually has your cPanel username in it). On the right side pane, select the public_html folder and also the ssl folder, then click “Compress” on the right side of the gray bar. If your webroot is not in the user’s home directory, you will need to make separate ssl and site tar files. For the compression type, choose Gziped Tar Archive then press “Compress File(s).” (other compression types will do but zips may loose file permissions and extended file attributes) Once the file is generated, double-click it to download it.

Now follow the guide on Getting SSH Access and Editing Website Files with SSHFS to gain access to the new user on the Site Wrangler server and copy both files into the user’s home directory.

On the Site Wrangler server switch to the new user:

su - example_user

Now extract the tar.gz file:

gunzip public_html.tar.gz 
tar -xf public_html.tar

Deployed Transferred Site Database

Now that everything is moved across and the files are in place, lets create a new database to use:

sw db create

Because servers with old web content are often forgotten about and left unpatched and unsecured, use a new database and password name on the Site Wrangler server. Then import the .sql file we created (using the filename we copied if we downloaded it from phpMyAdmin):

mysql example_database -u example_user -p < transfer.sql

Now update the settings in the file “wp-config.php” in the public_html folder to match our new database settings.

Setup Nginx

As root, add an nginx entry for the new site:

sw nginx add example.com

If you use and SSL certificate select the template “php-ssl”, otherwise select “php”.

SSL

If you selected “php-ssl” you probably got an error. This is because the certificate is not yet in place.

We need to update the nginx configuration to use the transferred certificate and private key. List your certificates and keys with these commands, copying down the paths for the newest files:

ls -la /home/example_user/ssl/certs/
ls -la /home/example_user/ssl/keys/

Now edit the nginx configuration by running this line:

sw nginx edit example.com

Scroll down to the “server” section that includes entries for “ssl_certificate” and “ssl_certificate_key.” Copy these lines to a safe place before updating them as we will need them later. Now replace the path after “ssl_certificate” with that from /home/example_user/ssl/certs/ and the one after “ssl_certificate_key” with that from /home/example_user/ssl/keys/. These paths show up twice in the file; be sure to update the paths for both.

Setup PHP

To add a PHP runtime to the site run this command:

sw php add

Test The Site

Now test the site using a hosts file modification. There are many guides online on how to do this.

Update DNS

Once you are satisfied that the site is working correctly, you can direct the rest of the world to use the new server. To use Site Wrangler DNS run this command to add the new domain:

sw dns add

Now update your domain’s nameservers to the ones your Site Wrangler server uses. Two important notes: DNS changes take up to 48 hours to reach all internet users; also, if your site has email addresses that use your domain, make sure your email is set up before making this change. Email messages are typically re-synced after the 48 hour window is closed.

Switching to Let’s Encrypt

Once DNS propagation is complete, we want to switch to Let’s Encrypt SSL certificates as before the cPanel one expires. Run this command to initiate the process:

sw cert add

Once a certificate is successfully installed on the server, we also need to update the nginx configuration file to revert our previous changes. As long as we did not make any other changes, we can simply rewrite the nginx configuration from the php-ssl template. Run this command again to open the editor:

sw nginx add

Just as before, select your username, enter your domain and select “php-ssl”. Now that we have a letsencrypt certificate, there should be no error.

 

Leave a Reply

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