CHALLENGE: we would like to migrate a WordPress website from old hosting to new
SOLUTION: use the SSH connection and the WP CLI command line
Migrating a website can be a difficult and time-consuming process. When moving to new hosting, we want to make sure that all files and the database are properly copied. Copying files using an FTP Client can be slow and inefficient. The good news is that nowadays most hostings have SSH access, which allows us to migrate an entire website really fast. It is really helpful when a website has a lot of uploads and a big database, migrating gigabytes of data isn’t a problem anymore.
STEP 1 – zip files and database
We’re going to export a WordPress database to .sql dump and then pack everything into a tar package (including hidden files).
# export database
wp db export
# zip everything
tar -zcvf ../my-website-all-1.tar.gz .
STEP 2 – copy files to new hosting
Now we have the entire website packed into one file: my-website-all-1.tar.gz. We can copy it to new hosting using the WinSCP application or any other SCP/SSH/FTP/SFTP Client. A different way would be to use a new hosting SSH and the wget command:
# get file from external url
wget https://www.mywebsite.com/my-website-all-1.tar.gz
# get file from external url with htpasswd protection
wget –user=user123 –password=mypass321 https://www.mywebsite.com/my-website-all-1.tar.gz
STEP 3 – unpack files, import database
We already have all files on the new hosting. Now we’re going to: unpack everything, put new database credentials into wp-config.php, and import .sql database dump.
# unpack files
tar -zxvf my-website-all-1.tar.gz
# add new database name, new db_user and db_password into wp-config.php file
nano wp-config.php
# import database
wp db import wp-db-yourfile321.sql
# clean up temporary files
rm my-website-all-1.tar.gz
rm import wp-db-yourfile321.sql
STEP 4 – DNS configuration
At this point, migration should be completed. All files and the database have been moved to a new hosting account. The last step will be to change DNS configuration. We want to make sure that our domain www.mywebsite.com points to the new hosting.
- DNS configuration: change DNS A Record for domain to point to the new hosting IP
STEP 5 (optional) – change domain url
Sometimes when migrating a website we would like to change site url. It can be a totally new domain name or just the preparation of a test page / staging website with a temporary url. The WP CLI search-replace command allows us to replace domain urls in a database.
# Standard wordpress – replacing DB urls
wp search-replace 'www.olddomain.com' 'www.newdomain.com' –all-tables –allow-root
# Multisite WordPress – replace database urls
# First step - change wp-config.php - DOMAIN_CURRENT_SITE to: www.newdomain.com
wp search-replace --url=www.olddomain.com www.olddomain.com www.newdomain.com 'wp_*options' wp_blogs
wp search-replace 'www.olddomain.com' 'www.newdomain.com' --all-tables –network
wp search-replace 'http://www.newdomain.com' 'https://www.newdomain.com' --all-tables –network
wp search-replace "http:\/\/www.newdomain.com" "https:\/\/www.newdomain.com" --all-tables --network
Troubleshooting
Styles are not displayed properly / some elements look incorrect / broken
Clear cache plugin cache / rebuild cache of assets (css/js)
Browser padlock displays the following error: Your connection to this site is not fully secure / Connection is Not Secure / Page was loaded over HTTPS, but requested an insecure image.
Make sure all database urls have been replaced from the http:// to https:// domain protocol.
Before changing DNS records, I would like to see how the page works on the new hosting.
You can do it by hardcoding the IP address in your Windows hosts file: \System32\drivers\etc\hosts , example row:
100.200.300.40 newdomain.com www.newdomain.com
Alternative solution – WordPress plugins
A different way to migrate a WordPress site is to use WordPress backup plugins. The most popular and reliable solutions are: UpdraftPlus and All-in-One WP Migration. Keep in mind that the free versions of those plugins have limited functionality and migrating a lot of gigabytes can be time consuming.
This concludes today’s tutorial. Make sure you follow our blog for other useful suggestions.
Comments
0 response