Migrating a WordPress Website has been never easier than before. With tons of plugins and tools, anyone can migrate the whole website from one server to another. Having said that, there are times when you do not have the option of using a cPanel or a File Manager to control the migration process. And you have to rely on the command line interface to interact with the server your WordPress website is hosted on.
Using a command line may not very convenient to many and that’s because we are not used to of using it in our routine tasks. But the control and performance command line offer are unparalleled to any other way of execution.
I will be covering the following topics in this article:
- What is WP-CLI?
- Migrate WordPress via WP-CLI
- Taking Backup of WordPress Files
- Importing WordPress Files to a New Server
- Taking the Database Backup
- Test and WordPress Website
- Search and Replace URLs
What is WP-CLI?
WordPress Command Line or (WP-CLI for short) is a dedicated command line interface tool for controlling WordPress. This enables users to run different commands, update WordPress files, install themes, and managing the plugins. This tool can also be used for WordPress website migration from one server to another.
Let’s get started and see how easy it is to use the WP-CLI for WordPress migration.
Migrate WordPress via WP-CLI
WordPress Command Line is accessed through a terminal and in this tutorial, I will use the SSH-client PuTTY for accessing the command line. You may use any SSH-client of your choice.
You would require the following to access your existing server:
- Server IP address
- Username
- Password
- Public Key
Launch PuTTY desktop application and navigate to SSH → Auth.
Now, browse to the location where you have stored the public key of your server and navigate to Session inside the PuTTY window to enter the IP address of the server.
Click the Open button and log in with your credentials. (IMP: Make sure to log in as a root admin and supply the correct password).
After accessing the server successfully via SSH terminal, let's go ahead and check if WP-CLI is installed on our server or not. For that, simply type the WP command below:
wp --info
Since I do not have the wp-cli installed, I will see the following error:
Let’s fix this by installing the WP-CLI. Run the following command in the SSH terminal of your old server. cd back to the opt folder by going all the way back to the root location. Keep running the command cd ../ command until you reach there.
Install wp-cli by running the following command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
This should install the WP-CLI. Next, confirm this typing the wp --info command.
Now, change the file’s permission to make it executable.
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Go back to the main WordPress folder and run the following command that should show you the version of your WordPress installation.
wp core version --allow-root
Let’s go ahead and take the backup of our WordPress website using WP-CLI.
Back up WordPress Files Using WP-CLI
Taking a backup using WP-CLI is easy. Navigate to the www folder or the folder which contains your WordPress website and create a zip file by running the following command.
zip -r wordpress.zip wordpress
Move the zipped folder inside the WordPress website folder by running the command below.
mv wordpress.zip
wordpress where wordpress is the name of the folder which contains WordPress site.
Now, navigate to the wordpress folder by running the cd command and you will find the wordpress.zip folder.
The following screenshot summarizes the whole process.
I moved the zipped folder inside the main folder so that I can download it easily from my new server using the command below:
http://[serverIP]/wordpress.zip OR http://[wordpress website location]/wordpress.zip depending upon your structure.
Here, wordpress.zip is the folder name and since it's inside our main folder, it should be accessible via URL or from the location depending upon your settings.
Import WordPress Files to a New Server
If you are using Cloudways then launch its integrated SSH terminal otherwise, hook your new server using PuTTY. Navigate to your new WordPress website and inside the public_html folder, type the following command.
wget http://[serverIP]/wordpress.zip
This will fetch the complete zipped folder from your older server through a URL. Once the transfer finishes, unzip the content of the folder by running the following command:
unzip wordpress.zip
And move all the content back to the parent directory (public_html) by running the following command.
mv * ../
There you go! I have successfully fetched the zipped folder and moved the WordPress website to the new host.
Take the Database Backup
We are halfway through our migration process. In the second half of our migration, we need to back up our MySQL database and for that, go back to your existing server and inside the SSH terminal type the following command:
mysqldump --add-drop-table -h localhost -u wpuser -p wordpress > site.bak.sql
In the above command, I have supplied the following parameters. Please adjust them to match your values.
-h localhost will stay the same
-u is the username of your MySQL database
wordpress is the name of the database.
Supply the password after running this command. Once the command is executed, you will notice a new file has been created (with the name ‘site.bak.sql’). This contains the complete database of our WordPress site.
This will create the backup file that can easily be exported to our new server using integrated SSH shell.
Let’s transfer the .sql file using the similar scp command from the old server to the public_html folder of our new WordPress website hosted on our new server.
Note: In this example, I am using Cloudways, this will override the existing database of WordPress website on Cloudways with the previous database.
scp /var/www/site.bak.sql master_prgaekpebs@104.237.158.292:/home/master/applications/ewscfaavrd/public_html
Now, go back to Cloudways SSH terminal window and run the following command to import the database file.
mysql -u ewscfaavrd -p ewscfaavrd < site.bak.sql
-u ewscfaavrd This is the database username of the WordPress website on Cloudways
-p ewscfaavrd This is the database name of the WordPress website on Cloudways
Great! I have successfully moved both the WordPress website files and database to my new server. You may also migrate a WordPress site using SSH Command Line. Let’s go ahead and check if the WordPress website is properly operational or not.
Test and Run WordPress Website
It is important that we test and run the WordPress website after moving it to a new server. For that, simply access the URL of your WordPress website either by your server IP or from the domain if you have already mapped your existing domain to your new server.
While browsing the website, I came across the error page which tells me that my WordPress website is unable to connect with its database.
We can fix this by replacing the old database credentials inside our wp-config.php file with the new credentials provided by my new server which in my case is Cloudways.
Go back to FileZilla and open the wp-config.php.
Inside the file, replace the DB_NAME, DB_USER, and DB_PASSWORD, save and close the file.
Now I go back and reload the page, I can see my WordPress website running fine.
As you can see, I have successfully moved the WordPress website and its database from my old server to its new hosting server using WP-CLI.
The entire process might feel a bit lengthy, but it’s faster and secure. Now, if you have a small website, use any of the popular migrator plugins for problem-free migrations.
Search and Replace URLs
At last, if you are moving your WordPress website to a new domain then you will also need to replace the URLs pointed to your old domain through a simple process.
Inside your SSH terminal, run the following command:
wp search-replace "http://old-url.com" "https://new-url.com"
This command will first search for all the URLs and then replace them with the new URL provided in the later part of the command. Make sure to replace the URLs to match yours.
Wrapping Up!
Using WP-CLI for WordPress migration may sound a bit unconventional method but if you follow the article above carefully, you will experience the most convenient and seamless migration experience. Not to mention, you will also learn your way around the command line.
Top comments (1)
You have your server's IP showed in the Putty terminal titlebar.