Hosts File Website Migration: Zero Downtime Guide

Hosts File Website Migration Zero Downtime Guide

Using the Hosts File to Migrate a Website With Zero Downtime

This article may contain affiliate links and sponsored content. If you buy through a link, the author may earn a commission at no extra cost to you.

If you’re migrating a website and can’t afford downtime, or you don’t want to touch the live site at all, there’s an easy way built right into your computer, on both Windows and Mac, to point your machine at a whole new IP address for any website you want. Let’s say you have my live website, jonathanjernigan.com, and you want to move it to a new server, or create a staging version that only you can access. You can do that without affecting the live site or its DNS, using a file on your computer called hosts.

What the Hosts File Is

The hosts file is effectively a local DNS record that you can use to manually override any DNS record out there on the internet. It’s most useful for web developers who want to point a domain somewhere else that only they can see, temporarily. In this example, let’s say we want to take jonathanjernigan.com and move it to a new host on my CloudPanel server. This is a totally different Hetzner server. I want to make sure the live site is never affected until I’m certain it has migrated over to this version without any issues.

The New Server

On this VPS, if I click this URL, even though it looks like it’s the same, it takes me to the live version of the site, because my DNS does not point to this server’s IP address. If I go to the file manager in this install, you can see it’s just a raw WordPress install. If I go to plugins, there’s nothing in here except the default stuff that ships with WordPress. My real site has Gravity Forms for the contact page, GenerateBlocks all over the site, and plenty of other plugins.

What I want to do is point my local DNS record, via the hosts file, at this new version. Then we can make whatever changes we want, move a backup file over, and confirm everything is working. After that, we switch our DNS to point to this IP, and everything should move over smoothly.

Sponsor: Pie Calendar

Pie Calendar is a flexible, lightweight calendar plugin for WordPress. According to the sponsor, you can turn any post on your website into an event that appears on a front-end calendar in about 5 minutes. The team says it takes a careful approach to stability and performance, so the plugin does what it’s supposed to do without updates that break your site. Being light on bloat doesn’t mean limited features, though. Pie Cal includes recurring events, add to calendar links, adaptive time zones, ACF support, e-commerce integrations, and more. At the time of recording, they were also offering a lifetime deal, which you can find on their website at piecalendar.com.

Editing the Hosts File on Windows

Finding the File

On Windows, browse to your local disk, then the Windows folder, System32, Drivers, and then a folder called etc. The file you’re looking for is the one called hosts. It doesn’t look like you can modify it, but you can. You’ll need to open it with something that has administrator privileges. You might have to open Notepad as an admin, or in my case I just open it with VS Code because it’s a simple text file.

Adding the Entry

Inside my hosts file I have a couple of entries pointing local development domains to a specific local IP address. In this case, I want to redirect my live URL to a different IP. All we need to do is put the destination IP address followed by the actual domain name we want to work with. I’m going to drop in that server’s IP address, press tab, and then type in the URL, which is jonathanjernigan.com. Then I press Ctrl+S to save. We get a notice that says insufficient privileges, so I retry as admin, trust, and then click yes.

Viewing the Result

Now when we open the site in a new tab, we see a blank WordPress install. There’s no SSL certificate installed here, so we just proceed. This version of jonathanjernigan.com is ready for us to do whatever we want to it while the live site stays unaffected. If you go to the live site right now, you’ll just see the normal website. This is a great way to check that the changes you’re making work on the real web server before the general public sees them.

If you have a website that can’t afford downtime, or you just don’t want it to look broken to your client, you can do something like this. The idea is very similar on Mac, but the file lives in a different place on Mac and Linux.

Editing the Hosts File on Mac

Opening Terminal

On the Mac side, open up Terminal, because it lets us edit the hosts file without permissions issues. If you try to open it with the Mac TextEdit utility, you’ll get a notice that says you don’t own this file, and your only option will be to clone it. So we’re going to open Terminal and use the built-in terminal text editor, which is called nano. Type sudo nano /etc/hosts, which lets us edit the /etc/hosts file in that editor. Press enter and it will ask for your computer’s local password.

Adding the Entry and Saving

As soon as you enter your password, you can see that the hosts file looks pretty much identical to the Windows version. Do the same thing, starting with the IP and then the destination domain name. To save on the Mac, press Ctrl+O and then Ctrl+X and you’re out. Then fire up your browser and go to jonathanjernigan.com, and you’ll see the same thing as on Windows, including the SSL warning. From here we can do whatever we need to the site locally and not worry about any changes going live to the general public.

Why This Is Useful

This is a handy utility in a lot of situations. I use it both for moving websites around and for leaving a live site untouched when I want to make big changes or test things before they go live.

Leave a Reply

Your email address will not be published. Required fields are marked *