Tomorrows Technology Today

How To

How to Migrate a WordPress Website using Command Line

Migrating a WordPress website manually can seem daunting, but with the right steps and tools, you can accomplish it efficiently. This guide will walk you through the process using the Linux command line. If you are not very familiar with Linux command line, then it could be better to use a plugin based migration method instead, to ensure you dont lose your data.

Prerequisites

  • SSH access to both the old and new servers
  • Basic knowledge of Linux commands
  • A backup of your WordPress site

Step 1: Export Your WordPress Database

First, you’ll need to export your WordPress database from the old server.

Log in to your old server via SSH:

ssh user@old-server-ip

Export the database using mysqldump

mysqldump -u your_db_user -p your_db_name > /path/to/save/your_db_backup.sql

Compress the SQL file:

tar -czvf your_db_backup.tar.gz /path/to/save/your_db_backup.sql

    Step 2: Download Your WordPress Files

    Next, you’ll need to download your WordPress files from the old server.

    Compress the WordPress files:

    tar -czvf wordpress_files.tar.gz /path/to/your/wordpress

    Transfer the compressed files to your local machine or directly to the new server using scp:

    scp wordpress_files.tar.gz user@new-server-ip:/path/to/destination scp your_db_backup.tar.gz user@new-server-ip:/path/to/destination

      Step 3: Upload Your WordPress Files to the New Server

      Navigate to the destination directory and extract the files:

        cd /path/to/destination tar -xzvf wordpress_files.tar.gz tar -xzvf your_db_backup.tar.gz

        Step 4: Create a New Database

        Note that there are 4 famous parameters for making MySQL database for worpress, the database name, user, password and host. None of these have to be the same as the values configured on the old database. you will import the old database into a new database with these new 4 parameters. However there is fifth parameter to watch out for and that is table prefix, which is important at the wp-config stage.

        Log in to MySQL:

        mysql -u root -p

        Create a new database and user:

        CREATE DATABASE new_db_name; 
        CREATE USER 'new_db_user'@'localhost' IDENTIFIED BY 'new_db_password';
        GRANT ALL PRIVILEGES ON new_db_name.* TO 'new_db_user'@'localhost'; 
        FLUSH PRIVILEGES;
        EXIT;

          Step 5: Import Your Database

          Import the database backup into the new database

          mysql -u new_db_user -p new_db_name < /path/to/destination/your_db_backup.sql

          and lets double check the table prefix, since if you are taking your database from a wordpress hosting, it might not be the default wp_ setting. List your databases with show databases; then select the database with use database_name; and examine the tables with show tables;

          MariaDB [(none)]> show databases;
          +---------------------------+
          | Database                  |
          +---------------------------+
          | information_schema        |
          | wordpress_db_website1     |
          | wordpress_db_website2     |
          +---------------------------+
          3 rows in set (0.001 sec)
          
          MariaDB [(none)]> use wordpress_db_website1;
          MariaDB [wordpress_db_website1]> show tables;
          +-------------------------------------+
          | Tables_in_wordpress_db_website1     |
          +-------------------------------------+
          | wp_commentmeta                      |
          | wp_comments                         |
          | wp_links                            |
          | wp_options                          |
          | wp_postmeta                         |
          | wp_posts                            |
          | wp_term_relationships               |
          | wp_term_taxonomy                    |
          | wp_termmeta                         |
          | wp_terms                            |
          | wp_usermeta                         |
          | wp_users                            |
          +-------------------------------------+

            in this example the table prefix is the wordpress default of wp_

            Step 6: Update the wp-config.php File

            Edit the wp-config.php file to reflect the new database details:

            nano /path/to/your/wordpress/wp-config.php

            Update the following lines with your new database information

            define('DB_NAME', 'new_db_name'); 
            define('DB_USER', 'new_db_user'); 
            define('DB_PASSWORD', 'new_db_password'); 
            define('DB_HOST', 'localhost'); 

            and if necessary, update the table_prefix

             * WordPress database table prefix.
            $table_prefix = 'wp_';

              Step 7: Update DNS Settings

              Finally, update your DNS settings to point to the new server’s IP address. This step will vary depending on your domain registrar. For HTTPS you should then also ensure to add a certificate which could be done with Lets Encrypt folowing the guide Secure Nginx with LetsEncrypt

              Summary

              Migrating a WordPress website manually using the Linux command line involves several steps, but it’s entirely manageable with careful execution. Always ensure you have backups before starting the migration process to avoid data loss.

              Happy migrating!