Home » #Technology » Migrate WordPress: AWS EC2 to Local Mac or Ubuntu

Migrate WordPress: AWS EC2 to Local Mac or Ubuntu

WordPress is a powerful tech solution for establishing an online presence, powering thousands of websites. In my previous tech blog, I demonstrated how to seamlessly migrate WordPress using rsync. However, if you’re not comfortable with rsync and prefer using SSH and SCP, this alternative method is for you. This tech concept, covers WordPress migration to better utilize this technology.
Migrating a WordPress website from AWS EC2 to a local development environment is crucial for developers aiming to streamline their workflow or test changes offline. Whether you’re on a Mac or Ubuntu machine, this guide will walk you through the process using efficient scripts to update WordPress configurations and the database.

Prerequisites

Before starting, ensure you have:

  • Access to AWS EC2: SSH access to your AWS EC2 instance where WordPress is hosted.
  • Local Development Setup: Apache/Nginx, PHP, MySQL/MariaDB installed on your local machine.

Step-by-Step Migration Process

1. Backup WordPress Files and Database

First, secure a backup of your WordPress files and database from AWS EC2.

  • Files Backup: Archive the WordPress files using tar.
  ssh username@ec2-instance-ip "tar -czvf wordpress_files.tar.gz -C /path/to/wordpress ."
  scp username@ec2-instance-ip:/home/username/wordpress_files.tar.gz /path/to/local/
  • Database Backup: Use mysqldump to export the database to a SQL dump file.
  ssh username@ec2-instance-ip "mysqldump -u db_user -p'db_password' db_name > /home/username/wordpress_backup.sql"
  scp username@ec2-instance-ip:/home/username/wordpress_backup.sql /path/to/local/
2. Extract Files and Set Up Local WordPress Environment

Prepare your local environment to host WordPress.

  • Extract Files: Extract the archived WordPress files to the desired location.
  tar -xzvf /path/to/local/wordpress_files.tar.gz -C /path/to/local/wordpress/
  • Server Configuration: Set up Apache/Nginx virtual hosts or server blocks to point to the WordPress files on your local machine.
  • Database Import: Import the database dump into your local MySQL/MariaDB server.
  mysql -u local_db_user -p'local_db_password' local_db_name < /path/to/local/wordpress_backup.sql
3. Update WordPress Configuration

Modify the wp-config.php file with your local database credentials.

define('DB_NAME', 'local_db_name');
define('DB_USER', 'local_db_user');
define('DB_PASSWORD', 'local_db_password');
define('DB_HOST', 'localhost');
4. Update URLs in the Database

Create a script to update URLs in the database to reflect your local environment.

  • URL Update Script: Save the following script as update_urls.sh and run it.
  #!/bin/bash

  OLD_URL="http://old-url.com"
  NEW_URL="http://new-local-url.com"
  DB_NAME="local_db_name"
  DB_USER="local_db_user"
  DB_PASSWORD="local_db_password"

  mysql -u $DB_USER -p$DB_PASSWORD $DB_NAME <<EOF
  UPDATE wp_options SET option_value = replace(option_value, '$OLD_URL', '$NEW_URL') WHERE option_name = 'home' OR option_name = 'siteurl';
  UPDATE wp_posts SET guid = replace(guid, '$OLD_URL','$NEW_URL');
  UPDATE wp_posts SET post_content = replace(post_content, '$OLD_URL', '$NEW_URL');
  UPDATE wp_postmeta SET meta_value = replace(meta_value,'$OLD_URL','$NEW_URL');
  EOF

Run the script:

  chmod +x update_urls.sh
  ./update_urls.sh
5. Test Your Local WordPress Site

Open your web browser and navigate to the local URL (e.g., http://localhost or http://localhost/wordpress) to verify that WordPress is functioning correctly.

Automation with Scripts

For efficiency and consistency, automate tasks with scripts:

  • Backup Script: Automate file and database backups.
  • Setup Script: Automate server configuration and database import.
  • URL Update Script: Automate URL updates in the database.

Best Practices

Ensure a smooth migration by following these best practices:

  • Backup Safeguard: Always create backups before making changes.
  • Local Testing: Test thoroughly in your local environment before deploying changes.
  • Security Measures: Secure local setups with appropriate file permissions and credentials.
  • Version Control: Use Git for version control to track changes effectively.

My Tech Advice: As a tech advisor and entrepreneur, I always recommend owning your data and technology. WordPress offers one of the best solutions for achieving this. Migrating WordPress from AWS EC2 to a local machine enhances development flexibility and efficiency. One can leverage SSH, tar, and scripts to update configurations and databases, Hence ensure a smooth and seamless transition. Implement these steps today to optimize your WordPress development workflow and empower your projects with localized testing and development capabilities. Happy migrating! 🧑🏻‍💻

#AskDushyant
#Wordpress #Migration #AWS #EC2 #Mysql #PHP #Programming

Leave a Reply

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