Have you found the perfect hosting provider but are afraid to proceed because the thought of transferring your WordPress site frightens you? Are you concerned that the migration will break your website or affect your SERP rankings?
Well, this guide is for you then. I have tried to present a great solution to the challenge of moving your WordPress website to a new host.
There are various ways of migrating a WordPress site to a new server. Today, I will cover two methods of moving a WordPress site to a new host using the command line (SSH) and WP-CLI. Since the steps might sound a bit complex, I will keep things as simple as possible.
Using Command Line Interface (CLI) has its own advantages when compared to manual or plugin based migrations. It is more reliable, fast and less prone to errors due to direct communication between the two servers.
- What Is SSH/Command Line?
- What Is WordPress Command Line?
- Why Migrate WordPress to New Server?
- Accessing the Command Line
- Via WordPress Command Line
- Test and run the WordPress site
What Is SSH/Command Line?
SSH is a command line interface, widely used to control remote machines (in our case, the servers are both the current and future home of the WordPress website). Shell commands can be executed for a wide range of tasks. It is not interactive and only accepts text commands for the operation you wish to perform.
SSH provides a secure and encrypted channel through which information is shared between the client and the remote server. Because of the popularity, an SSH terminal is integrated into iOS and Linux OS. Windows users need to download an SSH-client like PuTTY to initiate the SSH session.
Unfortunately, the majority of WordPress users are not familiar with the command line interface. Therefore, I will use very simple steps to walk you through the rest of this guide. I will also explain the behind-the-scenes working of each and every command, giving you a better idea of what you are doing.
What is WordPress Command Line?
Like SSH, WP-CLI is a command line interface dedicated to WordPress. This allows you to operate the CMS in pretty much the same manner as the WordPress frontend. Because of the flexibility and simplicity, WP-CLI is used by many WordPress developers and users.
WP-CLI is used to transfer WordPress sites, install plugins, manage a multisite network, manage databases and upgrade the core files. Although all of the above tasks can be performed through the web interface, it is still a preferred way of performing tasks without distractions.
Migrate WordPress to New Server
Cloudways is a managed cloud hosting platform where your server is managed, updated and secured by our team of experts. You can harness the power of the top five cloud providers: Amazon Web Services, Google Cloud Platform, DigitalOcean, Linode and Vultr while enjoying a powerful and feature-rich platform.
Let’s look briefly at some of the features Cloudways offers to its customers.
Cloudways Platform is loaded with useful features which enable its customers to easily manage the services like deploying multiple applications on one server, creating staging sites for testing out website updates, integrating free SSL certificates, free cache plugin and CloudwaysCDN, add/manage team members, SFTP access and IP whitelisting, etc.
24/7 Live Support
Support is essential for uninterrupted operations of any business. Cloudways ensures that the customers always have access to 24/7 live chat support. Besides this, Cloudways customers can also get instant support from our knowledge base as well as from the active community that pitches in to help fellow Cloudways users.
Intuitive User Interface
The Cloudways platform is very easy to use and even a non-techie can find their way around with no hassle at all. Navigating through the server and application settings is very simple and complex options are explained via hints and tooltips.
Besides a ton of built-in features, Cloudways also gives its customers the option to integrate add-ons that best suit their requirements. These add-ons can be used for integrating email provider, DNS services, extended app-level support, migrations and the robust Cloudways API for developing, integrating and managing your server.
Cloudways users also enjoy the integrations for monitoring their server and application health via CloudwaysBot and New Relic APM. These tools help in analyzing website performance and take the right steps towards improving application performance.
Access Command Line
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
- 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).
Navigate to the WordPress Directory
The first thing I need to do after logging into the server through SSH is to navigate to my WordPress site directory that contains the files and folders which need to be moved to a new host.
In the terminal, type the following command that ensures that I am at the root of the server.
The next command is ls, short form for the list command that displays all the files and folders in the root folder.
You might see a different list of files and folders which is fine. From here, you need to get into your WordPress installation folder. In my case, I will go to the www folder (this contains my WordPress installation) inside the var folder.
Type cd var/www and hit Enter.
Upon checking the content of this folder by running the ls command, I can see that it contains a folder for my WordPress site.
Simply type cd wordpress (or whatever the name of your folder is) to gain access to the WordPress site folder.
Backup WordPress Files
The WordPress folder contains a lot of folders and files and backing them all is important before I actually initiate the process of moving a WordPress site. To do so, I will create a zip folder of my WordPress site folder, which is easier and faster to transfer from my existing cloud server to the Cloudways managed server.
Note: In order to run the zip command, make sure you have zip installed. You can install zip by running the following command: sudo apt install zip unzip
While in the www folder, run the following command:
zip -r wordpress.zip wordpress
-r is a flag which enables recursive reading of files. Do not forget to replace the name of the folder to match yours at [wordpress.zip] and [wordpress] inside the command above.
After the process completes, cd back to www folder or the folder from which you ran the zip command and type the ls command to confirm if the zipped file has been created.
Great! I have successfully taken the backup of my WordPress site. Let’s move ahead and take the backup of WordPress database.
Backup WordPress Database
In order to create a backup of MySQL database file, go back to your SSH terminal and 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.
At this point, I have successfully created backups of WordPress files and its database. Let’s see how to move WordPress site to a new host.
Launch WordPress on Cloudways
Before I proceed with the rest of the WordPress migration, let’s set up the server and launch a WordPress site on the Cloudways Platform.
It takes a few clicks to deploy a new server and launch a WordPress site on Cloudways.
Start by signing up for a Cloudways account and choose the desired flavor of WordPress site from the drop-down menu. Name your application, server, and the project.
Choose the cloud provider, data center location and then click the Launch Now button. Within a few minutes, your server and the WordPress website will be deployed.
That’s it! Our WordPress site is up and running on the Cloudways managed server. Let’s migrate our existing site to our new server.
Moving WordPress Site to Cloudways
I am now all set to initiate the migration process. In this process, I will migrate the zip file containing the WordPress files from my existing folder to the new WordPress site I have launched on Cloudways.
Before I proceed, let’s connect to the Cloudways server via FTP using FileZilla. This way, I can easily monitor the files and their location. For that, I will launch FileZilla, navigate to Server Management from Cloudways Platform and copy its Public IP, Username, and Password.
Now, enter the credentials in FileZilla to access the server. Navigate to applications → [your wordpress site folder] → public_html folder and delete all the contents inside this folder. It will take a few minutes to delete everything. This is the location where I will move the WordPress site from my old server.
In the next step, go back to your SSH terminal and make sure that you are in the www directory. Next, run the following command:
scp /var/www/wordpress.zip firstname.lastname@example.org:/home/master/applications/ewscfaavrd/public_html
Let’s look at the command and see what’s going on inside.
scp [var/www/wordpress.zip] this is the location of the file I wish to send to my new Cloudways server. Please change it to match yours.
[email@example.com] text before the @ sign is the username of my new Cloudways server and after the @ sign is my public IP.
[:/home/master/applications/ewscfaavrd/public_html] this bit of the command represents the location where I wish to migrate my zipped WordPress folder.
Once the command finishes, you will see a new file inside the directory.
Finally, I need to unzip the content of this zipped folder. To do so, go back to your Cloudways platform and inside Server Management, launch the integrated SSH terminal.
Log in with the username and password (provided as Master Credentials) and navigate to your WordPress directory where I just copied the zipped folder.
Using SSH for unzipping the folder is fast and easy. Go to the public_html folder of the application where the zipped folder was copied from the old server, and run the following command to unzip the contents.
Once the process is completed, run the ls command to see the content of the unzipped folder.
Note: Keep the zipped folder as it may be required if anything goes wrong. If everything goes as expected, you can delete the folder later on.
I have successfully transferred the WordPress site to new hosting. But If you notice, the location is not right. In order to serve this site, I should have the content inside the public_html, not in a folder inside the public_html directory.
To move all the content from WordPress folder to its parent directory public_html, run the following command inside the Cloudways SSH terminal while in the WordPress directory.
mv * ../
The mv command is used to move files/folders, the * represents all files/folders and ../ is to go one step back.
Run ls command to confirm that all the files and folders have been successfully transferred.
I have successfully moved the WordPress site to a new host.
Import WordPress Database to Cloudways
Let’s transfer the .sql file using the similar scp command from the old server to the public_html folder of our new WordPress site on Cloudways.
Note: This will override the existing database of WordPress site on Cloudways with the previous database.
scp /var/www/site.bak.sql firstname.lastname@example.org:/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 site on Cloudways.
-p ewscfaavrd This is the database name of the WordPress site on Cloudways.
I have moved the WordPress site and database to Cloudways server. Let’s go ahead and check if the WordPress site is properly operational. For that, simply click the application URL under the APPLICATION URL section.
While browsing the website, I came across the error page which tells me that my WordPress site 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 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 site running fine.
As you can see, I have successfully moved the WordPress site and its database from my old server to its new hosting server.
The entire process might feel a bit lengthy, but it’s faster and secure. Now, if you have a small website, use the Cloudways migrator plugin for problem-free migrations.
Migrate WordPress via WP-CLI
As discussed earlier, I will also cover the whole migration process using WordPress Command Line or WP-CLI for short.
Open your SSH terminal and make sure that WP-CLI is installed on our server. For that, simply type the WP command below:
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 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
Great! Let’s go ahead and take the backup of our WordPress site using WP-CLI.
Back up WordPress Files Using WP-CLI
Taking a backup using WP-CLI is similar to the process based on SSH. Navigate to the www folder and create a zip file of the WordPress site by running the following command.
zip -r wordpress.zip wordpress
Move the zipped folder inside the WordPress site folder by running the command below.
mv wordpress.zip where wordpress is the name of the 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 have moved the zipped folder inside the WordPress site folder so that it can be accessed through a URL. It should be downloadable from the URL like this:
http://[serverIP]/wordpress.zip OR http://[wordpress site location]/wordpress.zip depending upon your structure.
Import WordPress Files to Cloudways
Log into your Cloudways platform and launch the SSH terminal. Navigate to your new WordPress site and inside the public_html folder, type the following command.
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:
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 site to the new host.
Take the Database Backup
The database can be backed up using similar steps:
Run the following command to create the .sql file for your WordPress MySQL database.
mysql -u ewscfaavrd -p ewscfaavrd < site.bak.sql
This will create the backup file that can easily be exported to Cloudways server using integrated SSH shell.
Search and Replace URLs (If You are Moving a Site to a New Domain)
In case you are migrating to another domain, you will also need to replace the URLs pointed to your old domain through a simple process.
In the Cloudways 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.
Test and Run The WordPress Site
If you have followed along properly and did not encounter any roadblocks, you should be able to see your migrated site up and running on your new Cloudways server without any issues.
It is better to do a bit of cleanup after completing the migration process. For this, delete the zipped folder of your WordPress file as well as your MySQL database. This is totally optional and if you are not bothered by disk space, leave them alone.
I hope the above article will help you in migrating your WordPress site easily without using restoring to GUI. Go ahead and brag about the skills you acquired from this article. Command line is fun and if you are interested in more useful commands, feel free to explore the internet.
Ibad Ur Rehman is a WordPress Community Manager at Cloudways. He likes to explore latest open-source technologies and to interact with different communities. In his free time he likes to read, watch a series or fly his favorite Cessna 172SP in X Plane 10 flight simulator.
Start Growing with Cloudways Today!
We never compromise on performance, security, and support.