|

How to migrate a WordPress website from one server to another using SSH

About a year ago, I signed up and created my server at Oracle Cloud Infrastructure’s Free Tier, allowing me to deploy a server for free. While multiple WordPress plugins exist to help with migrating sites, if you’re comfortable handling servers and using SSH, the process below will be a breeze! The process below can also work for non-WordPress websites.

This tutorial will guide you through migrating a WordPress (or any other) website from one server to another using SSH and SCP. This method is secure, efficient, and suitable for small and large WordPress installations.

While this migration guide is comprehensive, don’t let the length intimidate you! The process is straightforward and typically takes less than 15 minutes for a standard WordPress site. Detailed explanations, best practices, and troubleshooting sections are included to help you handle any potential complications.

Migrate WordPress website

Prerequisites

  • SSH access to both servers
  • Database credentials for both servers
  • Sufficient disk space on both servers
  • Basic command line knowledge
  • Root or sudo access (recommended)

I’m also using a Mac for this tutorial. If you’re on Windows, you can use a client like PuTTY. Additionally, I use WordOps to deploy my WordPress and non-WordPress sites, and it saves the site data in /var/www/.

1. Preparing the Old Server

1.1. Login to your old server

Login to your server using SSH:

ssh user@server1

1.2. Compress the WordPress Files

Navigate to your WordPress installation directory and create a compressed archive:

cd /var/www/example.com/
tar -cvzf htdocs.tar.gz htdocs

The flags mean:

  • -c: Create a new archive
  • -v: Verbose output (shows files being processed)
  • -z: Compress with gzip
  • -f: Specify filename

1.3. Export the Database

There are several ways to dump your MySQL database:

Standard MySQL dump:

mysqldump -u [user] -p[password] [db_name] | gzip > example.sql.gz

Important note: No space between -p and your password

If you’ve forgotten your database details and you’re using WordOps, get your database details with this command:

wo site info example.com

Database credentials can also be found in wp-config.php:

grep DB_ /var/www/example.com/htdocs/wp-config.php

Using WP CLI

Alternatively, you can dump the database using the following command using WP CLI. The - tells WP-CLI to output to stdout instead of a file, making it pipeable. You will need to run this command in the root directory of your WordPress installation.

wp db export - | gzip > example.sql.gz

2. Setting Up the New Server

2.1. Create the site

First, create the new site on your new server. With WordOps, you can run:

wo site create example.com --wp --php83 -le --dns=dns_cf

The above command tells WordOps to create a WordPress site using PHP8.3 with LetsEncrypt.

--dns=dns_cf uses Cloudflare’s DNS API for domain verification during the SSL certificate issuance. If you’re not using Cloudflare, you can skip this.

2.1. Transfer the files

Transfer both the website files and database. If you have the files saved in a different location, adjust the paths. You should run this command on your Mac directly, i.e. not on the server.

scp user@server1:/var/www/example.com/htdocs.tar.gz user@server2:/var/www/example.com/
scp user@server1:/var/www/example.com/example.sql.gz user@server2:/var/www/example.com/

Alternative using rsync (more reliable for large files):

rsync -avz -e ssh user@server1:/var/www/example.com/htdocs.tar.gz user@server2:/var/www/example.com/
rsync -avz -e ssh user@server1:/var/www/example.com/example.sql.gz user@server2:/var/www/example.com/

2.2. Extract WordPress Files

Navigate to the directory in the new server and extract the files.

cd /var/www/example.com
tar xvf htdocs.tar.gz

The flags mean:

  • x: Extract
  • v: Verbose
  • f: Specify filename

2.3. Import the Database

gunzip < example.sql.gz | mysql -u [user] -p[password] [databasename]

With WP CLI, you can run:

gunzip < example.sql.gz | wp db import -

2.4. Update WordPress Configuration

Edit wp-config.php to match new server settings:

nano /var/www/example.com/htdocs/wp-config.php

Update:

  • Database credentials
  • Database host (if different)
  • WP_HOME and WP_SITEURL if needed

2.5. Set Proper Permissions

chown -R www-data:www-data /var/www/example.com/htdocs
find /var/www/example.com/htdocs -type d -exec chmod 755 {} \;
find /var/www/example.com/htdocs -type f -exec chmod 644 {} \;

3. Testing the Migration

3.1. Modify Local Hosts File

On your local machine:

For Windows:

Edit C:\Windows\System32\drivers\etc\hosts

For Linux/Mac:

Edit /etc/hosts

Add:

new_server_ip example.com www.example.com

3.2. Verify Website Functionality

  • Check frontend loads correctly
  • Test admin login
  • Verify media uploads
  • Check all plugins are working
  • Test contact forms and other functionality
  • Verify SSL if applicable

4. Going Live

4.1. Update DNS Records

Update your domain’s A records to the new server’s IP address.

4.2. Clean Up

After successful migration, on both servers in /var/www/example.com/, run:

rm htdocs.tar.gz example.sql.gz

Common Issues and Solutions

Database Connection Errors

  • Verify database credentials in wp-config.php
  • Ensure MySQL user has proper permissions
  • Check that the MySQL host is correctly specified

Broken Links or Mixed Content

If you’ve changed URLs, run a search and replace them on the database:

UPDATE wp_options SET option_value = replace(option_value, 'old-url.com', 'new-url.com');
UPDATE wp_posts SET guid = replace(guid, 'old-url.com', 'new-url.com');
UPDATE wp_posts SET post_content = replace(post_content, 'old-url.com', 'new-url.com');

Permission Issues

If you encounter 403 errors or upload issues:

chown -R www-data:www-data /var/www/example.com/htdocs
chmod 755 /var/www/example.com/htdocs
find /var/www/example.com/htdocs -type d -exec chmod 755 {} \;
find /var/www/example.com/htdocs -type f -exec chmod 644 {} \;

Security Considerations

  • Change database passwords after migration, if not automatically done when setting up the new server
  • Remove backup files after successful migration
  • Update SSL certificates if needed
  • Review and update file permissions
  • Change admin passwords

Best Practices

  • Perform migration during low-traffic hours
  • Keep the original server running until DNS propagation is complete
  • Take multiple backups before starting
  • Document all changes made during migration
  • Test thoroughly before changing the DNS
  • Keep backup files for at least a week after successful migration

Conclusion

Migrating a WordPress site using SSH and SCP provides a secure and reliable method for transferring your website between servers. While the process involves several steps, this approach gives you complete control over the migration and helps ensure data integrity. Here’s a quick recap of the key points to remember:

  1. Always create backups before starting the migration.
  2. Double-check database credentials and permissions.
  3. Test the site thoroughly on the new server before updating the DNS.
  4. Keep the old server running until DNS propagation is complete.
  5. Clean up sensitive files after a successful migration.

Remember that each WordPress (or non-WordPress) installation is unique, and you may need to adjust these steps based on your specific setup, plugins, and server configurations. If you encounter issues during migration, review the Common Issues section of this guide or consult your hosting provider’s documentation.

By following this tutorial and maintaining good security practices throughout the process, you can successfully migrate your WordPress website with minimal downtime and reduced risk of data loss.

Leave a Reply

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