How to migrate a Wordpress site from one server to another

Our friend Erik Hanchett from Program With Erik shows how to migrate a Wordpress website from a server to Webdock. These instructions are generally applicable to most other CMS systems as well, and work as a general guide on how to move a website. Webdock has numerous Wordpress specific features however, which are also touched on in the video. If you don't want to watch the vid, please see detailed instructions below.

The advanced way: If you have shell access to both servers (Terminal access via. SSH)

SSH in to your server and go to your web root - this is /var/www/html on Webdock, but varies between hosting companies. Your Web root is where the wp-config.php file is located. 

You may need to pre-pend “sudo” to these commands, or run “sudo su” in order to get superuser permissions. It all depends on your host and the environment. In many cases this is not needed if your website is in a shared hosting environment.

Run 

mysqldump -u USER -pPASSWORD DATABASENAME > mydb.sql

Where USER is the database username and PASSWORD is the database password Wordpress is using. It is on purpose there is no space between the -p flag and the password itself in this command. If you do not know these credentials, you can see them by looking in the wp-config.php file. 

Run

zip -r mysite.zip .

Congratulations - you now have a zip file with all the data you need to move your Wordpress site somewhere else.!

Now SSH in to your new server (Webdock) and go to /var/www/html

Run

wget https://mywebsiteaddress.com/mysite.zip

This downloads the zip file you just created on your old host to Webdock.

Unzip all your data and delete the zipfile afterwards so it doesn’t take up space:

unzip mysite.zip; rm ./mysite.zip;

Set correct ownership of files

chown -R www-data:www-data .

Import your database

mysql -u USER -pPASSWORD DATABASENAME < mydb.sql

Remember this is the username and password on the NEW server. You can see the database username and password on the main server information screen in Webdock.

Remember to delete the sql dump

rm ./mydb.sql

Update wp-config.php with the new database name, user and password. The database host is always “localhost” in Webdock.

The simpler but much slower way: Export your database via. phpMyAdmin and use FTP to download and then upload your files to your new host.

Many hosting companies, especially cheaper shared hosting, will not provide you with SSH access so that you can run the commands to dump your database and zip up your files.

In this case you need to FTP in to your existing host and download all the files to your local machine. This is typically fairly slow - and for a bit website this may take a long time.

Some hosting companies provide a rudimentary file manager in their dashboard, and in some cases you may be able to zip up all your website files and download them directly that way. It may be worthwhile it to check for that option.

Upload your files to the /var/www/html folder in Webdock. You can see your Webdock FTP credentials on the main Server Information screen.

For your database, you would then typically use phpMyAdmin to export it. This DB management tool is fairly standard with most (if not all) webhosting companies, so you should have access to this. It is fairly simple to export a database with phpMyAdmin - just log in, click on the database name in the left-hand-side column and then click on the export tab. Just click the export buttons (the default settings are fine) and you are good to go.

To import your database again in Webdock, you would then also just use phpMyAdmin. Look for the small database icon next to your DB credentials and click it, this will take you to phpmyadmin, or just enter https://yourservername.host.webdock.io/phpmyadmin in the address bar. Log in with the DB credentials, click on the Webdock database that is automatically created for you and then click the import tab. You should now be able to upload and import your database without any problems.

Now remember to Update wp-config.php with the new database name, user and password. The database host is always “localhost” in Webdock.

Final steps and things to look out for (applies to all methods of migrating Wordpress)

Now that you have moved over all your files, imported your database and updated wp-config so that it is using the right database credentials, you now need to let Webdock know that there is a Wordpress installation on the server so you can get access to the built-in Webdock Wordpress management tools.

You do this by going to Manage Wordpress and start with installing WP-CLI on the server. WP-CLI is a tool which helps Webdock interact with your Wordpress installation, and it can even install Wordpress for you, if you are starting from scratch.

In this case, once WP-CLI has been installed, you would then just click on the “Save path” under the Wordpress is already installed section.

Now you can use our check updates and update functionality, and also use the Lockdown functionality. Lockdown is a way to set very strict permissions on your Wordpress website, which in our experience lowers the risk of your site being hacked, even if you don’t keep it updated by 90% or more. It is very effective.

More information on Wordpress Lockdown:

https://webdock.io/en/docs/wordpress-lockdown 

If you are changing the website address, i.e. your domain name - you need to let Wordpress know what its new website address is.

This only applies if the domain name of your website changes. Wordpress really wants to redirect visitors to whatever address is set in Wordpress settings. Also, some plugins may save the entire website URL of your site for links (some menu plugins do this for example)

So that means, when you have moved your site to Webdock, and visit the site using the webdock URL, like https://mysite.webdock.io/ you may experience that you just get bounced over to your old host / domain 

This is not a problem if you want to keep using your old domain, i.e. the same website address as before, as you would now point your domain DNS records to the new Webdock server.

Then everything is the same as before and everything will just work.

If you need to update the address however, Webdock has a tool called wp-cli (which you already installed in the previous step) which can help you with this task quite easily.
Once WP-CLI is installed, you can simply run the following command on the command line, and you should be good to go:

sudo wp search-replace 'example.dev' 'example.com' --skip-columns=guid

Where example.dev is your old domain and example.com is your new one. This will also fix any plugins or other components which may have written absolute URL’s to the database.

In some cases you may need to include the http:// part if you are switching from a non-ssl enabled website to Webdock, which is always SSL enabled, then the command would become:

sudo wp search-replace 'http://example.dev' 'https://example.com' --skip-columns=guid

More information on various ways to update site URL’s can be found here:

https://codex.wordpress.org/Changing_The_Site_URL 

One final thing to look out for, which is related to all of this is infinite redirect loops. As Wordpress likes to redirect to the website address set in its settings, this can conflict with other things such as redirects on the DNS level.

Say that you for example have set in your DNS that the domain should always redirect from example.com to www.example.com

If you then have set the domain in Wordpress settings to just example.com - then Wordpress will always try to redirect www to non-www and the DNS level will be trying to do the reverse. You will then get a “Too many redirects” message in your browser. Fix this by aligning the two settings so that there is no conflict anymore.

---

General information on how to get shell access in Webdock:

https://webdock.io/en/docs/shell-users-and-sudo