Autocodewizard Logo Configuring DNS with Hosts File - Autocodewizard Ebook: Web Development Getting Started

Chapter 6: Configuring DNS with Hosts File

DNS, or Domain Name System, is a foundational technology of the internet that translates human-readable domain names (like example.com) into IP addresses that computers use to locate each other. Understanding DNS and configuring your local hosts file can help create a seamless development environment, especially when you want to set up local versions of your website.

What is DNS?

DNS is often called the "phonebook of the internet" because it maps domain names to IP addresses. When you enter a domain name in your browser, DNS servers look up the domain’s corresponding IP address to find where it’s hosted, allowing you to access the site.

Web Development - DNS

How DNS Works When You Buy a Domain

When you purchase a domain, it needs to be registered with a DNS server. Your domain registrar, such as GoDaddy or Namecheap, associates your domain with an IP address where your website is hosted. This DNS configuration is what directs traffic to your site whenever someone types in your domain name.

Once registered, DNS servers around the world update and store your domain information. When users enter your domain name, their local DNS server can quickly retrieve the IP address and direct them to the website.

Using Local Domains for Development

When developing locally, it’s a good idea to use a unique domain name to avoid conflicts with your live site. Instead of using your actual domain (like example.com), use a local-only domain name (like example.local). This ensures that when you access example.com, you’re seeing the live version, while example.local points to your local development environment.

Configuring the Hosts File

The hosts file is a local configuration file on your computer that maps domain names to IP addresses, overriding the DNS lookup. By editing the hosts file, you can direct specific domain names to your local server, making it easy to work on local versions of your site.

Web Development - HOSTS File

Here’s how to configure your hosts file to point example.local to your local server:

Step 1: Locate the Hosts File

The hosts file is located in different places depending on your operating system:

Step 2: Edit the Hosts File

Open the hosts file in a text editor with administrator privileges. Add a line to map example.local to your local server (usually 127.0.0.1).

        # Add this line to point example.local to your local server
        127.0.0.1 example.local
                

Save the file, then open http://example.local in your browser. If your local server is running, you should see your site’s local version.

Best Practices: Avoid Hardcoding Domain Names

When developing, it’s essential to avoid hardcoding domain names into your code. Hardcoding can lead to issues when moving from your local environment to staging or production. Instead, create a configuration setting or use conditional logic to switch between local and live URLs based on the server environment.

Using Environment Variables

One approach is to use environment variables to define the base URL for your site. Environment variables allow you to set the base URL for each environment (local, staging, production) and easily switch between them without changing your code.

        # Example PHP code to use an environment variable for the base URL
        $base_url = getenv('BASE_URL') ?: 'http://example.local';
                

Detecting the Environment Programmatically

A reliable way to distinguish between local and live environments is by checking the server’s hostname. For example, you can assign a unique hostname to your local machine (e.g., mywebsite.local) and then detect that hostname in your code to determine the environment.

                    # Example PHP code to detect environment based on hostname
                        if (strpos($_SERVER['HTTP_HOST'], '.local') !== false) {
                            $base_url = 'http://mywebsite.local';
                        } else {
                            $base_url = 'https://mywebsite.com';
                        }
                            

In this example, if the hostname contains .local (e.g., mywebsite.local), the code recognizes it as the local environment and sets the base URL accordingly. This approach ensures flexibility and avoids conflicts when moving between environments.

Setting Your Machine’s Hostname

Setting a custom hostname on your local machine, such as mywebsite.local, allows you to easily detect whether your code is running locally or on a live server. Here’s how to set a hostname on Windows and macOS:

On Windows

1. Open the **Control Panel** and go to **System**.
2. Select **Change settings** under **Computer name, domain, and workgroup settings**.
3. Click **Change** next to your computer’s name.
4. Enter your desired hostname, such as mywebsite.local, and click **OK**.
5. Restart your computer to apply the changes.

On macOS

1. Open **System Preferences** and go to **Sharing**.
2. In the **Computer Name** field, enter your desired hostname, such as mywebsite.local.
3. Close the **Sharing** preferences to save your changes.
Alternatively, you can set the hostname via the terminal with the following command:

# Set hostname on macOS via terminal
sudo scutil --set HostName mywebsite.local
    

After setting your hostname, you can use it to reliably detect your local environment in code by checking $_SERVER['HTTP_HOST'] for .local or your custom hostname.

Summary

Configuring your hosts file and using a custom local domain like example.local allows you to create a local development environment that doesn’t interfere with your live site. By avoiding hardcoded domain names and using environment-specific URLs, you can create flexible, adaptable code that works seamlessly across different environments.

DNS and the hosts file are essential tools in web development, helping bridge the gap between local and live environments and enabling you to work on secure, isolated versions of your projects.