Migrating WordPress to a New Server

Migrating WordPress to a New Server

In the chapter before, we improved security and performance by tweaking the Nginx Configuration. This article will walk you through all the steps necessary to migrate a WordPress site from one Server to another.

Many reasons can exist to migrate your site. You might be moving from one host to another. The following guide is for moving a website to a SpinupWP-hosted server. However, I recommend that you use it to get more detailed instructions. It will save you a lot of time and frustration.

Retiring a server is another good reason for migrating a website. We don’t recommend upgrading the operating system of a server. We do not recommend upgrading Ubuntu, even though Ubuntu may encourage it. It’s not worth it to upgrade the OS on a live server.

It is safer to start a new server, migrate the existing sites, and then shut down the older Server. This allows you to make sure everything works on the new Server before changing the DNS or directing traffic there.

If you’ve not yet completed the previous chapters, you should begin at the beginning. How easy is it to set up new servers that are optimized for hosting WordPress quickly? See for more information on SpinupWP. Let’s get started!

#Secure Copying of Files

We need to determine the best method to copy files to the new Server before we start migrating them. There are a few methods, including SFTP, but the fastest and safest is to use SCP.

SCP allows us to copy files from Server to Server without having to download them first to our local computer. SCP is based on SSH, so we will need to create a new SSH Key to connect to the old Server. Create a new SSH Key on the newly provisioned Server using the following command.

ssh-keygen -t rsa -b 4096 -C “your_server_ip_or_hostname” 

Copy the public key into your clipboard. The public key can be viewed as follows:

cat: /.ssh/id_rsa.pub

Add the public key of the Server to the authorized_keys.

sudo echo “public_key” >> ~/.ssh/authorized_keys 

Verify that you can connect to the older Server via SSH from the new one.

ssh ashley@pluto.ashleyrich.com 

If you are unable to connect with the Server, check your previous steps and then continue.

#File Migration

Start by migrating all the files on the site, including WordPress and files located in the root directory. Then, issue the following command on the new Server. Replace the IP address of your old Server and the path leading to the web root with the new one.

scp -r ashley@pluto.ashleyrich.com:~/ashleyrich.com ~/ashleyrich.com 

It’s now time to add your site to Nginx.

#Nginx configuration

You can add a site to Nginx in a few different ways:

  1. Create a new configuration based on Chapter 3.
  2. Copy the design from the old Server

Copying the configuration is recommended since you already know that it works. Starting afresh is useful, particularly if you have a virtual host file that contains many redundant directives. As a starting point, you can use a zip file of Nginx configurations.

This example will copy the configuration. Copy the file with SCP, just as we did for the site data.

scp -r ashley@pluto.ashleyrich.com:/etc/nginx/sites-available/ashleyrich.com ~/ashleyrich.com 

Then, you will need to move the file and make sure that the root user is the owner.

sudo mv ashleyrich.com /etc/nginx/sites-available sudo chown root: root /etc/nginx/sites-available/ashleyrich.com 

The last step is to enable the site with Nginx. This is done by creating a symlink from the virtual host to the enabled-sites folder:

sudo ln -s /etc/nginx/sites-available/ashleyrich.com /etc/nginx/sites-enabled/ashleyrich.com 

Copying over SSL certificates is a good way to test if your configuration is correct before testing.

#SSL Certificates

The permissions on the certificate files are much more restricted, so it is necessary to copy them from your old Server to your home directory.

sudo cp /etc/letsencrypt/live/ashleyrich.com/fullchain.pem ~/ sudo cp /etc/letsencrypt/live/ashleyrich.com/privkey.pem ~/ 

Then, make sure our SSH user is able to read and write:

sudo Chown Ashley *.pem

Copy the certificates back onto the new Server.

scp -r ashley@pluto.ashleyrich.com:~/*.pem ~/ 

After the DNS switchover (see Finishing up), we will generate new certificates using Let’sEncrypt. We’ll update Nginx’s configuration to reflect these new paths.

sudo nano /etc/nginx/sites-available/ashleyrich.com 

Update the ssl_certificate and ssl_certificate_key.

ssl_certificate /home/ashley/fullchain.pem; ssl_certificate_key /home/ashley/privkey.pem; 

Test the Nginx configuration again to confirm that the directives have been correctly implemented:

sudo nginx -t 

Reload Nginx if everything looks good:

Sudo service nginx reload 

#SpoofDN

You should test the new Server while we are working. This can be done by spoofing the local DNS. The old Server will remain active for visitors, but you will still be able to test the new one. Add an entry in your /etc/hosts that points the IP address of the new Server to the domain name on your local machine.

 

 

Leave a Reply