Setting up a domain using Cloud DNS

This tutorial provides a walkthrough of the process for registering a domain, setting up a sample web server, and using Cloud DNS to point the domain URL to the server.

Objectives

The tutorial demonstrates the following steps:

  1. Register a domain name using Google Domains or Cloud Domains
  2. Create a virtual machine (VM) instance
  3. Run a basic Apache web server
  4. Set up your domain using Cloud DNS
  5. Update name servers
  6. Verify your setup

Costs

There is a cost associated with registering a domain name. For Google Domains pricing, see Pricing and supported domain endings. For Cloud Domains pricing, see Cloud Domains pricing.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Compute Engine API.

    Enable the API

Step 1: Register a domain name

To use Google Domains to register a domain, follow these steps:

  1. To register a domain, go to Google Domains. If you already have a domain, you can skip this step.
  2. Check for available domain names and choose an available name for your domain.
  3. To buy the domain, click Add to cart.
  4. Choose the privacy and auto-renewal settings, and then click Save and continue.
  5. Complete the registration.
  6. To see the list of domains that you own, in the left navigation menu, click My domain.

For more information, see Get started with Google Domains.

To use Cloud Domains to register a domain, see Registering a domain.

Step 2: Create a virtual machine instance

To create a Linux virtual machine (VM) instance in Compute Engine, follow these instructions:

  1. In the Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Click Create instance.
  3. In the Boot disk section, click Change to begin configuring your boot disk.
  4. On the Public images tab, choose Debian version 9.

  5. Click Select.
  6. In the Firewall section, select Allow HTTP traffic.
  7. Click Create to create the instance.

Allow a short time for the instance to start. After the instance is ready, it is listed on the VM instances page with a green status icon.

Connect to your instance

  1. In the Cloud Console, go to the VM instances page.

    Go to VM instances

  2. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.

    SSH button next to instance name.

You now have a terminal window for interacting with your Linux instance.

For details, see the Quickstart using a Linux VM.

Step 3: Run a basic Apache web server

Install Apache

  1. From the SSH window, use the Debian package manager to install the apache2 package.

    sudo apt-get update && sudo apt-get install apache2 -y
    

    After installing Apache, the operating system automatically starts the Apache server.

  2. Overwrite the default web page for the Apache web server by using the following command:

    echo '<!doctype html><html><body><h1>Hello World!</h1></body></html>'\
    | sudo tee /var/www/html/index.html
    

Test your server

Test that your instance is serving traffic on its external IP address.

  1. In the Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Under the External IP column, copy the external IP address for your instance.
  3. In a browser, navigate to http://[EXTERNAL_IP]. Do not use https to connect because the server will return a Connection Refused error.

You should now see the Hello World! page.

For further details, see Running a basic Apache web server.

Step 4: Set up your domain using Cloud DNS

  1. In the Cloud Console, go to the Create a DNS zone page.

    Go to Create a DNS zone

  2. For the Zone type, select Public.

  3. For the Zone name, enter my-new-zone.

  4. For the DNS name, enter a DNS name suffix for the zone by using a domain name that you registered (for example, example.com).

  5. Under DNSSEC, ensure that the Off setting is selected.

  6. Click Create to create a zone populated with the NS and SOA records.

  7. To point your registered domain name to the IP address of the hosting server, you must add an A record to your zone:

    1. On the Zone details page, click Add record set.
    2. Select A from the Resource Record Type menu.
    3. Under IPv4 Address, enter the external IP address for your instance.
    4. Click Create to create the A record for your zone.
  8. Optional: Add a CNAME record to account for a prefix to your domain name (for example, www.):

    1. Click Add Record Set.
    2. In the DNS Name field, add the prefix www for the domain.
    3. Under Resource Record Type, choose CNAME.
    4. Under Canonical name, enter the domain name, followed by a period (for example, example.com.).
    5. Click Create.
  9. On the Zone details page, make a note of the NS records. You need these records to proceed with Step 5.

Step 5: Update name servers

To update name servers in Google Domains, follow these steps:

  1. To update the name servers for your domain, go to Google Domains.
  2. Click the domain that you set up in Step 1. Alternatively, you can click the Manage link for that domain.
  3. On the left navigation menu, click DNS.
  4. Under Name servers, select Use custom name servers.
  5. In the Name server field, enter the NS records that you copied from the Zone details page (for example, ns1.googledomains.com) one at a time.
  6. To add name servers, click Add.
  7. Update all four name servers on your Google Domains DNS page.
  8. Click Save.

Step 6: Verify your setup

To verify that your configuration is working, after the name servers are updated, navigate to your domain name (for example, example.com). The domain should resolve to your IP address and should point to the Compute Engine VM displaying the Hello World! page that you created in Step 3.

To verify that your setup is correct, you can also run the dig +trace example.com command on your terminal window. Replace example.com with your registered domain name.

dig +trace example.com

The end of the output should include the following. IP_ADDRESS is your web server's IP address.

example.com.    300 IN  A   IP_ADDRESS
;; Received 62 bytes from 216.239.34.109#53(ns-cloud-d2.googledomains.com) in 62 ms

To verify that the changes were successful, the following line in your command output shows that the top-level domain name servers are pointing to the custom name servers that you entered in Google Domains, as opposed to the original name servers provided by Google Domains:

example.com IN NS <your Cloud DNS name servers>

After waiting for DNS propagation to complete, you can also run the nslookup command to verify your setup:

nslookup example.com

The output should include the following. IP_ADDRESS is your web server's IP address.

Server:     127.0.0.1
Address:    127.0.0.1#53

Non-authoritative answer:
Name:   example.com
Address: IP_ADDRESS

Cleaning up

  1. In the Cloud Console, go to the Cloud DNS zones page.

    Go to Cloud DNS zones

  2. Click a zone name (for example, my-new-zone) to get to the Zone details page.

  3. Select the A and CNAME records that you created.

  4. Click Delete record sets.

  5. To delete the zone, click Delete zone for the zone name my-new-zone.

  6. Go to the VM instances page.

    Go to VM instances

  7. Select the instance that you want to delete.

  8. On the More menu in the row of the instance, click Delete.

What's next