Migrating a multisite WordPress install to a new server

One project I’m working on this Summer is moving a WordPress multsite install (with about 300 sites) to new hardware. I didn’t find much in the way of documentation or guides on this, especially anything specific to a multisite install. So, to prep for this project, I created a new WP multisite with a few test sites, plugins and posts on an AWS server and practiced migrating it to a new virtual machine. Here are the steps I took and what I discovered. This was using Ubuntu 18.04 and a MySQL 5.6 database.

First, make sure to get a copy of your WordPress code and database. Just zip the entire web root directory of the site:

tar -cvfz wp-code.backup.tar.gz html/

Backup the database with mysqldump. Here my database is named wordpress, I can log in as root without a password, and I am creating a backup file wpdb-backup.sql.

sudo mysqldump --add-drop-table -u root wordpress > wpdb-backup.sql 

…then zip the dump file as well.

tar -czvf wpdb-backup.sql.tar.gz wpdb-backup.sql

Next, we need to transfer the backup files to the new server.  If the servers can talk to each other over SSH, use rsync. Before you can use rsync, create a keypair with no password on the origin server. 

Digital Ocean has a good guide on creating SSH keys here if you aren’t familiar: https://www.digitalocean.com/docs/droplets/how-to/add-ssh-keys/create-with-openssh/

If you don’t have an existing key on the server, you can just type

ssh-keygen

… and hit enter when asked for a password to skip using a passphrase and accept the defaults. If you already have an SSH key, instead of accepting the default name (which would overwrite your key) type in a new name like rsync_rsa_id.

Once your key is created, you now need to copy the contents of the public key you just created. If you used the default settings can see the public key with:

cat ~/.ssh/id_rsa.pub

Next SSH in to the destination server. Copy the public key info to the .ssh/authorized keys file in your home directory.

Sudo nano ~./ssh/authorized_keys

Now you are ready to transfer files. Go back to the origin server and run rsync:

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /file/to/transfer/backup.tar.gz   hostNameOrIPAddress:/destination/path 

A quick explanation of the -avz flags used:  

  • -a -archive mode. Makes command recursive, copies permissions, copies symlinks, preserves last modified times, and so on. 
  • -v verbose. See what rsync is doing 
  • -z compress data before transferring 

On target server 

Make sure server has neccesary pre-reqs for WordPress. Apache, PHP, MYSQL, rewrite rules etc. Here is a good guide for Ubuntu: 

https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lamp-on-ubuntu-16-04#step-1-create-a-mysql-database-and-user-for-wordpress

Make sure to create a blank database for wordpress, using the same username and password as on your origin server.

Once the database is set-up it’s time to put your WordPress files in their new homes.

 Move to the root directory and  extract the tarball we created of the WordPress code directory:

cd var/www/html 

sudo tar -xvzf wp-code.tar.gz 

Then extract the database tarball using the same command: sudo tar -xvzf mydbbackup.sql, but instead of moving it to a directory, restore it to the wordpress database you created. 

sudo mysql wordpress < wpdb-backup.sql

That’s it!

Unless your server has a different IP address or hostname, in which case….

If the new server is not the same IP address/hostname as the old one, or if you are using a different database name, username or password, you’ll need to make some quick edits to the wp-config.php file.

Note: Before editing it, ALWAYS BACKUP wp-config.php. Trust me 🙂

cp wp-config.php wp-config-bak1.php 

Then Modify wp-config.

Update the line to your current IP or hostname by editing this line:

define('DOMAIN_CURRENT_SITE', 'newHostName'); 

(If you need to, change the relevant lines for the database as well)

Almost done! For a multisite install going to a host with a different name, you need to make an update directly to the database on the destination server. You can use PHPMyAdmin if you have access to that. Or just use the MySQL command line again:

Access MySQL: 

sudo mysql 

Access the wordpress table:

USE wordpress; 

Update the wp_blogs table with your new address: 

UPDATE wp_blogs SET domain = 'newHostName'; 

Whooo! After all that, you should have an identical functioning copy of your multisite blog network on your new host!

Photo by Barth Bailey on Unsplash

Upgrading PHP on a CentOS Server

My institution uses CentOS for all of our Linux servers, and I understand why. It’s super stable. But! The default software repository also tends to use really, really old packages. Which means I was running a WordPress server that I discovered was using PHP 5.5. No good in general since PHP 7 is much faster and secure, and untenable since the minimum version of PHP has been raised to PHP 5.6 in newer versions of WordPress.

Anyway, it’s easy to use an additional package manager and switch to a newer version of PHP. Here’s how I updated to PHP 7.2 (and by the way, made the WordPress site about twice as fast instantly).

First make sure you have repositories installed:

• EPEL repo
• Remi repos
• Yum-Utils

Steps:

Make sure things are up to date:

sudo yum update
  • Enable PHP 7.2
sudo yum-config-manager --enable remi-php72
  • Install PHP and modules
sudo yum install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo
  • Restart apache
sudo apachectl restart
  • Check PHP version (you should get 7.2.x)
php –v

Photo by AJ Robbie on Unsplash