How to move WordPress site to a new server or a new domain using SSH

Here is a Step by step procedure to move a WordPress site from one server to another server or from localhost to server. You can also move the complete website setup from one domain name to another.

Here I will explain how to move the website using SSH. If you do not have SSH access then you can try using cPanel and phpMyAdmin. Click here to read How to move WordPress site to a new server or a new domain using cPanel and phpMyAdmin.

Assumptions

Server 1: Current server where WordPress is set up. This can be your local host also.
Server 2: The server where you want to move the website.

Follow the below steps.

1. Server 1: Export database

To export the database, first find out the name of the database. Login to server 1 SSH.

$ ssh username@websitename.com
$ // When prompted for a password, enter it.
$ // Go to the root folder of the website. 
$ // public_html or /var/www/html/websitename.com
$ cd public_html
$ vi wp-config.php

Here you can find the database name as shown below.

/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );

Now dump the database into a file.

$ mysqldump -u username -p database_name > db_backup.sql

2. Server 1: Compress the website files

We’ll compress the entire website from the root folder(public_html) to make moving the files easier. Use the tar command to compress the files. Below given command will create a compressed file and can be accessed using the URL https://websitename.com/website.tar.gz

$ tar -cvzf website.tar.gz ./

3. Server 2: Import Database

Login to Server 2 SSH and enter the below-given command to create and import the database.

$ mysql -u username -p
$ // When prompted for a password, enter it.

mysql> create database database_name;
Query OK, 1 row affected (0.00 sec)

mysql> exit

$ mysql -u username -p database_name < members.sql

4. Server 2: Download the website files

Download the website zip file created on server 1 to server 2. Login to server 2 and go to the website root folder.

$ cd public_html
$ wget https://websitename.com/website.tar.gz
$ tar -xvzf website.tar.gz
$ vi wp-config.php

Edit the wp-config.php file and update the database and user details.

/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );

/** Database username */
define( 'DB_USER', 'username_here' );

/** Database password */
define( 'DB_PASSWORD', 'password_here' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

5. Update the DNS records

Now you have moved the database and all the website files from one server to another. Now is the time to point the domain to the new server.

Login to the domain control panel and go to DNS or name server settings. Point the DNS of your domain name to the new hosting server. Usually, it will take 12-24 hours to update the records.

6. Browse the website and enjoy!

You are done with all the tasks. Open the URL on the browser and check if the website opens properly. Still, your domain may point to the old server and open the website properly. To check whether the website loaded from the old server or the new server, you use the ping command on the command prompt and check the IP. You can also rename the index.php file of the old server and check if the website loads properly.

Just drop a comment if you get any problems while moving the website. I will be happy to assist you.

Ranjith Kadamboor
Ranjith Kadamboor

Programmer with 15+ years of broad experience in Software Design and Development, Interested in Blockchain and Crypto.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in