Steps to migrate WordPress Site to New Host


The following will help you move an existing WordPress site from your current hosting provider to a new hosting provider.

**DISCLAIMER** Be sure to back up EVERYTHING! This includes your files, your images and your databases. I assume no responsibility for lost data by you using this how-to guide.

First, we need to define a few things. We will use HOST A as your current hosting provider and HOST B as the one you are moving to.

Second, FDQN is “Fully Qualified Domain Name” and is in the form of example.com or alt.example.com

Third, it is assumed you have a working knowledge of the Linux command line. If not, this all may seem confusing to you and I would suggest either enlisting help or getting familiar with the Linux command line before proceeding.

Fourth, that you have a working database server and a application such as phpMyAdmin or Adminer to manage your databases.

And finally, that you have access to and understand how to manipulate your DNS records at your registrar. This is critical since you will be making changes to those records to point to your new WordPress location.

OK, with all that out of the way, here we go!

These steps need to be completed first on your NEW host, HOST B

  1. SSH or open a console connection and use the editor of your choice (I use nano in this how-to) and enter the following:
$ sudo nano /etc/apache2/sites-available/<FQDN>.conf

Add the following and then save:

<Directory /var/www/html/<FQDN>/public_html>
      Require all granted
</Directory>
      <VirtualHost *:80>
          ServerName <FQDN>
          ServerAlias www.<FQDN>
          ServerAdmin webmaster@localhost
         DocumentRoot /var/www/html/<FQDN>/public_html
     ErrorLog /var/www/html/<FQDN>/logs/error.log
    CustomLog /var/www/html/<FQDN>/logs/access.log combined
    </VirtualHost>

2. Next, complete the following steps to create the necessary directories, enable the site and reload Apache2:

$ sudo mkdir -p /var/www/html/<FQDN>/{public_html,logs}
$ sudo a2ensite <FQDN>.conf
$ sudo systemctl reload apache2

These steps need to be completed first on your OLD host, HOST A

3. SSH or open a console connection and use the editor of your choice (I use nano in this how-to) and enter the following:

$ cd /var/www/html/<FQDN>
$ sudo tar -cvf <FQDN>.tar .   <---dont forget period at end!!
$ sudo gzip <FQDN>.tar
$ cat /var/www/html/<FQDN>/public_html/wp-config.php | grep DB.   <--(make note of the DB_NAME, DB_USER & DB_PASSWORD) 
$ mysqldump -u root -p <DB_NAME> > <filename>.sql
$ scp <FQDN>.tar <rootusername>@<newhostIP or domain>:/var/www/html/<FQDN>
$ scp <filename>.sql <rootusername>@<newhostIP or domain>:/var/www/html/<FQDN>

4. Now we are going back to the NEW host at HOST B

5. Open your mySQL database and create a new database, either via the command line or using a web app.

6. Open up a command line or console and enter the following:

$ cd /var/www/html/<FQDN>
$ sudo tar -xvf <FQDN>.tar.gz

7. Next, we will be editing the wp-config.php file. Change the DB_USER & DB_PASSWORD to the proper settings for this server and then SAVE:

$ sudo nano /var/www/html/<FQDN>/public_html/wp-config.php

8. Now we will populate the database, set ownership and permissions:

$ sudo cat <filename>.sql | mysql -u <dbusername> -p <dbname>
$ sudo chown -R <yourusername>:www-data /var/www/html/<FQDN>/
$ sudo find /var/www/html/<FQDN> -type d -exec chmod g+s {} \;
$ sudo chmod g+w /var/www/html/<FQDN>/public_html/
$ sudo chmod -R g+w /var/www/html/<FQDN>/public_html/

At this point, if everything went smoothly, you should have successfully migrated your old WordPress site to your new host.

Lastly, dont forget to change the nameservers at your registrar to the new hosts nameservers and if you are using Certbot, be sure to add the new domain (sudo certbot –apache).

If you have any comments, suggestions, find any errors or typos, please leave me a comment! Thanks!

Comments