How to Migrate a WordPress Site from VPS to AWS Lightsail

Posted on - Last Modified on

Migrating a WordPress site from a traditional VPS to AWS Lightsail offers a scalable, cost-effective, and high-performance hosting solution. This guide will walk you through the steps to transfer your WordPress database and the wp-content directory, ensuring a smooth transition.

Step 1: Prepare for the Migration

Before you begin, ensure the following:

  • Access to the VPS: This is your current WordPress hosting.
  • Access to the Lightsail instance: This will be your new WordPress hosting.
  • SSH Key: You’ll need the private SSH key for your Lightsail instance, stored at /home/.ssh/lightsail.key.
  • Database Credentials: Have the database credentials for both the VPS and Lightsail instances.

Step 2: Migrate the WordPress Database

Export the Database from VPS

On your VPS, use mysqldump to export the WordPress database. The following command creates a dump and directly streams it to Lightsail for import:

sudo mysqldump -u vps_db_user -p$(cat /home/vps_db_pass) \
  --databases wordpress \
  --single-transaction \
  --compress \
  --order-by-primary \
| sudo mysql -u root \
  --host=new.lightsail.aws \
  -p$(cat /home/lightsail_db) \
  -P 3306

Explanation:

  • mysqldump:
    • Exports the wordpress database with transaction safety (--single-transaction) and compression (--compress).
  • mysql:
    • Imports the dump into the Lightsail database directly.

Verify Database Import

Once the database transfer is complete, connect to your Lightsail database and confirm the data was imported:

mysql -u root -p -h new.lightsail.aws -P 3306
SHOW DATABASES;

Step 3: Transfer the wp-content Directory

The wp-content directory contains all your uploaded files, themes, and plugins. This needs to be transferred to your Lightsail instance.

Transfer Using rsync

Use the following rsync command to copy the wp-content directory from your VPS to Lightsail:

rsync -avzP --partial --append \
  -e "ssh -i /home/.ssh/lightsail.key" \
  /var/www/wp/wp-content/ \
  lightsail_user@new.lightsail.aws:/htdocs/public/wp-content/

Explanation:

  • rsync:
    • -a: Archive mode to preserve file permissions and metadata.
    • -v: Verbose mode to show detailed transfer logs.
    • -z: Compress data during transfer to save bandwidth.
    • --partial and --append: Allow resuming partially transferred files.
  • ssh -i:
    • Specifies the private key to authenticate with the Lightsail server.

Verify File Transfer

After the transfer, SSH into your Lightsail instance and verify the files:

ssh -i /home/.ssh/lightsail.key lightsail_user@new.lightsail.aws
ls /htdocs/public/wp-content/

Step 4: Update the WordPress Configuration

After transferring the database and files, update the wp-config.php file on your Lightsail instance:

  1. Set Database Credentials: Update the database name, username, and password in the wp-config.php file to match those of the Lightsail database.

  2. Verify Site URL: If the domain or site URL has changed, update it in the database using the following SQL command:

DROP PROCEDURE IF EXISTS wp_change_domain;

DELIMITER $$

CREATE PROCEDURE wp_change_domain(
    IN new_domain VARCHAR(255),
    IN old_domain VARCHAR(255),
    IN prefix VARCHAR(255)
)
BEGIN
    DECLARE wp_posts VARCHAR(255);
    DECLARE wp_options VARCHAR(255);
    DECLARE wp_postmeta VARCHAR(255);

    SET wp_posts = CONCAT(prefix, 'posts');
    SET wp_options = CONCAT(prefix, 'options');
    SET wp_postmeta = CONCAT(prefix, 'postmeta');

    SET @stmt1 = CONCAT('UPDATE ', wp_options, ' SET option_value = REPLACE(option_value, \'', old_domain, '\', \'', new_domain, '\') WHERE option_name IN (\'home\', \'siteurl\');');
    SET @stmt2 = CONCAT('UPDATE ', wp_posts, ' SET guid = REPLACE(guid, \'', old_domain, '\', \'', new_domain, '\');');
    SET @stmt3 = CONCAT('UPDATE ', wp_posts, ' SET post_content = REPLACE(post_content, \'', old_domain, '\', \'', new_domain, '\');');
    SET @stmt4 = CONCAT('UPDATE ', wp_postmeta, ' SET meta_value = REPLACE(meta_value, \'', old_domain, '\', \'', new_domain, '\');');

    PREPARE stmt1 FROM @stmt1;
    PREPARE stmt2 FROM @stmt2;
    PREPARE stmt3 FROM @stmt3;
    PREPARE stmt4 FROM @stmt4;

    EXECUTE stmt1;
    EXECUTE stmt2;
    EXECUTE stmt3;
    EXECUTE stmt4;

    DEALLOCATE PREPARE stmt1;
    DEALLOCATE PREPARE stmt2;
    DEALLOCATE PREPARE stmt3;
    DEALLOCATE PREPARE stmt4;


END $$

DELIMITER ;



CALL wp_change_domain('www.new-site.com', 'www.old-site.com', 'wp_');

Step 5: Test Your WordPress Site

Once the migration is complete:

  1. Visit your site on Lightsail to ensure everything works as expected.
  2. Check for missing files, broken links, and plugin functionality.
  3. If necessary, perform a search-and-replace to fix old URLs using a tool like WP-CLI or a database migration plugin.

 

Migrating a WordPress site from a VPS to AWS Lightsail involves transferring the database and wp-content directory while updating the configuration. With Lightsail’s robust hosting environment, your site will be ready to scale and handle increased traffic efficiently.

For added security, consider enabling Lightsail’s managed backups and setting up SSL certificates for your new environment.

Posted 11 November, 2024

freedeep

WordPress Problem Solver: Fast & High-Quality

Expert WordPress Problem Solver: Quick, Reliable, and Quality-Driven Solutions Struggling with WordPress issues or need custom features for your site? I specialize in solving WordPress problems with precision and speed, delivering tailored solutions that align with your business objectives. Here’s How I Can Help: - Custom WordPress Solutions – From building new websites to enhancing existing on...

Next Article

"Mastering the Art of Winning Bids on Freelancer.com: A Proven Guide to Freelance Success"