cPanelResources

Tutorial Editing your local hosts file to preview sites

How to edit your local hosts file to test websites
For a long time now Apache's mod_userdir has been the preferred way to check if sites are working properly on a machine without DNS being configured. This allows users to confirm a site is functional before the DNS is pointed to that system, allowing the site to be tested before it goes live.

However, there are two reasons that mod_userdir may no longer be the best option for testing websites: absolute links and new PHP handlers.

With the rise of Search Engine Optimization and content scraping, developers are commonly using absolute paths for all site content. This makes it impossible to test a site with mod_userdir - although the homepage of the site will work any subsequent links you click will follow the full URL, bringing active DNS into the equation, and taking you to the live site.

With EasyApache 4 providing more control than ever over the PHP configurations on your system there are also new PHP handlers to be aware of. Specifically, the PHP-FPM and CGI handlers don't work with mod_userdir.

So what is the best way to test sites in these conditions? The answer is by editing your local computer's hosts file.

While this does take a bit more effort at the user level than just entering a slightly different URL into the browser, editing your local hosts file to preview sites often results in a better preview experience and may be the only option available if you're running one of the PHP handlers where mod_userdir isn't supported. Editing your local computer's hosts file is a way to trick your computer into thinking a website is hosted on a different machine than it really is by directing the DNS to a different location for a specific domain. This is something that can be configured quickly on any operating system, and we'll be covering the steps necessary to do this in Windows, Mac OS, and Linux.

Editing the hosts file
In all the examples below we'll be pointing our imaginary website, cpanel.rocks, to our testing IP of 255.43.32.21.

Windows
It's important to note that no matter which version of Windows you're working with you'll need to have administrator access to the machine to change the file

Windows 7
On Windows systems the hosts file is located in C:\windows\system32\drivers\etc\hosts - you can navigate directly to that file through Windows Explorer or you can enter this in the search box when you click the Windows icon:

notepad c:\windows\system32\drivers\etc\hosts

That will open the hosts file in Notepad which will look like this:

windows_hosts_notepad.jpg

At that point you can add this line of text to the bottom of the file:

255.43.32.21 cpanel.rocks
Just save the file, restart your browser, and your site will be pointed to that new location.

Windows 8, 8.1, and 10
In Windows 8 and 10 you'll need to open the Notepad app as an administrator. You can do this by searching for the application, right clicking, and choosing the "Run as Administrator" option.
windows8_open_as_admin.png
Once Notepad is running click File, Open, and then enter the following line into the "File name" box:

c:\windows\system32\drivers\etc\hosts

Now you can edit the file normally and add this line of text to the end of the file:

255.43.32.21 cpanel.rocks


Just save the file, restart your favorite browser, and now you're ready to view the site on the new IP address.

Mac OS
The easiest way to perform this work in the Mac OS is to access the file using terminal. You'll want to run this command to open your hosts file. In our example we're using the Vim text editor, but feel free to use any editor you like.

sudo vim /etc/hosts
This will open up the hosts file on your system, which will look something like this:

mac_hosts_file.png

At the bottom of the file you'll want to add this line to point the site to the new IP address:

255.43.32.21 cpanel.rocks
Once you've added that line, just save the file. You'll also need to restart any browser's you're using to make sure that change is loaded.

Linux
The process of editing the hosts file in Linux is nearly identical to the MacOS process, although you may not see those commented lines at the top. Here is what the /etc/hosts file from a CentOS system looks like:

linux_hosts_file.png

Just like in MacOS you'll open this file as an administrator with your favorite text editor, add the line to the bottom of the file, save the file, and restart your browser to load the changes.

Contributed by: @cPRex
Author
cPanelResources
Views
11,196
First release
Last update
Rating
0.00 star(s) 0 ratings