Yesterday, I finished building a new and improved WordPress server. This means I had to migrate the serenity-networks.com WordPress installation from the old server to the new. I wanted to do this with zero downtime, which is actually quite easy and straightforward if you have a plan in place. The process works something like this.
- Export the database from the old server using phpmyadmin
- Copy all WordPress files from the old server to the new server
- Import the database on the new server using phpmyadmin
- Add temporary hostname entry into your local hosts file
You might be asking yourself, why on earth would I add a hostname entry into the hosts file on my workstation? Well, this will allow you to pull your website from the new server without changing your public DNS settings. If you are only hosting one site on the new server, and not using virtual headers, you can skip this step if you like. So lets get it started.
Moving a WordPress site to a new server
The only real requirement, other than a server capable of running WordPress, is having phpmyadmin installed on both of your web servers. Most already do, but if you don’t, click here to learn how. We need this to export and import the WordPress database.
First, log into phpmyadmin on your old server so we can download the WordPress database. It should be pretty obvious which database is for your WordPress instance, but if you aren’t sure you can see the name of it by looking at the wp-config.php file, located in the root of your WordPress installation.
Select your database in the left column and then click the Export tab at the top of the right column. Leave the selection set as Quick, and click Go.
If your database export is larger than 2MB, then zip / compress it. On OSX you can right-click and select Compress. On Windows you can use 7zip or Winzip. You can import the compressed file through phpmyadmin when we get to that step, shortly.
In the past, I’ve seen phpmyadmin choke when trying to import a database that is larger than 2MB. Compression drastically reduces the size of a database backup.
Next, we need to move all of the WordPress files from the old server to the new server. I prefer using WinSCP to do this because I try not to have FTP enabled for security reasons. SCP uses SSH to transfer files. It is very easy to use. On OSX, you can use CyberDuck. On Windows, WinSCP is the best option. If you have FTP installed on your servers, feel free to use that instead. Any method will work fine, we just need to move all WordPress files to the new server.
Download all WordPress files from the old server.
Now, transfer those files to the new server.
Next, we need to import the database on the new server, using phpmyadmin. Log into phpmyadmin on the new server. Click on Databases, then create a new database using the same name as the database on the old server.
Once you create the new database, select it in the left column and then click on the Import tab in the right column. Select the database export you obtained earlier, then click Go.
You might get an error about a duplicate primary key, you can safely ignore that.
Now we need to create a user for the database. I suggest using the same database username and password you have configured in your wp-config.php file, so you don’t have to change any configuration options. Just open that file to see what the username and password are set to, then click on the Privileges tab in the right column (it might be under the More pulldown). Select Add user. Enter the username and password. For hosts, select Local. Because you should already have the WordPress database selected before adding the user, there should be a checkbox that is checked which says “Grant all privileges on database “yourdbname.”” Leave that checked, and do not change any other settings. Click OK to create the user.
If you used the exact same database name when you created the new database, you will not have to edit wp-config.php as it will already reference the correct configuration options. If you did change the database name, username, or password, adjust the settings accordingly.
Now, we need to test the site. To do this, we need to temporarily add an entry into our local hosts file so our computer thinks the website is at the new IP, instead of the old one, which DNS is still pointing to.
To edit the hosts file on OSX or Linux
Open Terminal and type this in.
sudo nano /etc/hosts
Create a new line at the bottom with the IP of the new server, and yourdomain.com, separated by a few spaces. Be sure to put in yourdomain.com as well as www.yourdomain.com so that both resolve to the new server. Once finished, save and exit. It should look something like this.
To edit the hosts file on a Windows PC
The process has a few more steps in windows. Open your start menu and find notepad. Don’t click it like you would normally open it. Instead, right click on Notepad and select “Run as Administrator.” You will not have permissions to save the changes if you do not open it as Administrator. Once it’s open, click on File > Open. In the file name box, copy and paste the path below, then press enter.
Alternatively, you can manually navigate to C:\ > Windows > System32 > Drivers > Etc > hosts
Once the hosts file is opened, the process is the same as it is on OSX/Linux. On a new line at the bottom, enter the IP address of the new server and domainname.com separated by a few spaces. Be sure to create a line for yourdomain.com and www.yourdomain.com, both pointing to the new server’s IP address. Save and exit.
Close your browser the reopen it. Now, when you navigate to yourdomain.com, you will be pulling the site from the new server. You can use a simple ping command, from a command line, to ping yourdomain.com to verify it responds from the new IP address.
After you have verified the website is working properly on the new server, you can change your public DNS records with your web host or registrar. Leave the old server on for a couple days, just to make sure DNS has propagated the entire internet. After that, shut it down. You’ve migrated your WordPress site to a new server with ZERO downtime!
If you have any problems, feel free to ask for help in the comments below. Thanks!