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:
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