[Yandex Cloud documentation](../../../index.md) > [Yandex Compute Cloud](../../index.md) > [Tutorials](../index.md) > [Assigning a domain name to a web server VM](index.md) > Management console, CLI, and API

# Assigning a domain name to a web server VM using the management console, CLI, or API

To create an infrastructure for [assigning a domain name to a web server VM](index.md):

To assign a domain name in Cloud DNS to a web server VM:

1. [Get your cloud ready](#before-you-begin).
1. [Create a web server VM](#create-web-server).
1. [Create a public DNS zone](#configure-dns).
1. [Delegate your domain to Cloud DNS](#delegate-domain).
1. [Create a type A resource record](#create-record).
1. [Test the website](#test).

If you no longer need the resources you created, [delete them](#clear-out).

## Get your cloud ready {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../../billing/quickstart/index.md) and [link](../../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../../resource-manager/concepts/resources-hierarchy.md).

### Required paid resources {#paid-resources}

The support cost includes:

* Fee for using a [public IP address](../../../vpc/concepts/address.md#public-addresses) (see [Yandex Virtual Private Cloud pricing](../../../vpc/pricing.md)).
* Fee for [VM](../../concepts/vm.md) computing resources and disks (see [Yandex Compute Cloud pricing](../../pricing.md)).
* Fee for using a public [DNS zone](../../../dns/concepts/dns-zone.md) and public DNS requests (see [Yandex Cloud DNS pricing](../../../dns/pricing.md)).

### Create a cloud network and subnet {#create-network}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select your folder.
  1. Navigate to **Virtual Private Cloud**.
  1. At the top right, click **Create network**.
  1. In the **Name** field, specify `webserver-network`.
  1. In the **Advanced** field, disable the **Create subnets** option.
  1. Click **Create network**.
  1. In the left-hand panel, select ![subnets](../../../_assets/vpc/subnets.svg) **Subnets**.
  1. At the top right, click **Create**.
  1. In the **Name** field, specify `webserver-subnet-ru-central1-b`.
  1. In the **Availability zone** field, select the `ru-central1-b` [availability zone](../../../overview/concepts/geo-scope.md).
  1. In the **Network** field, select the [cloud network](../../../vpc/concepts/network.md) named `webserver-network`.
  1. In the **CIDR** field, specify `192.168.1.0/24`.
  1. Click **Create subnet**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  1. Create the `webserver-network` [cloud network](../../../vpc/concepts/network.md).

      ```bash
      yc vpc network create webserver-network
      ```

      Result:

      ```text
      id: enp1gg8kr3pv********
      folder_id: b1gt6g8ht345********
      created_at: "2023-12-20T20:08:11Z"
      name: webserver-network
      default_security_group_id: enppne4l2eg5********
      ```

      For more information about the `yc vpc network create` command, see the [CLI reference](../../../cli/cli-ref/vpc/cli-ref/network/create.md).

  1. Create a subnet in the `ru-central1-b` [availability zone](../../../overview/concepts/geo-scope.md):

      ```bash
      yc vpc subnet create webserver-subnet-ru-central1-b \
        --zone ru-central1-b \
        --network-name webserver-network \
        --range 192.168.1.0/24
      ```

      Result:

      ```text
      id: e2li9tcgi7ii********
      folder_id: b1gt6g8ht345********
      created_at: "2023-12-20T20:11:16Z"
      name: webserver-subnet-ru-central1-b
      network_id: enp1gg8kr3pv********
      zone_id: ru-central1-b
      v4_cidr_blocks:
        - 192.168.1.0/24
      ```

      For more information about the `yc vpc subnet create` command, see the [CLI reference](../../../cli/cli-ref/vpc/cli-ref/subnet/create.md).

- API {#api}

  1. To create a [cloud network](../../../vpc/concepts/network.md), use the [create](../../../vpc/api-ref/Network/create.md) REST API method for the [Network](../../../vpc/api-ref/Network/index.md) resource or the [NetworkService/Create](../../../vpc/api-ref/grpc/Network/create.md) gRPC API call.

  1. To create a subnet, use the [create](../../../vpc/api-ref/Subnet/create.md) REST API method for the [Subnet](../../../vpc/api-ref/Subnet/index.md) resource or the [SubnetService/Create](../../../vpc/api-ref/grpc/Subnet/create.md) gRPC API call.

{% endlist %}


### Create a security group {#create-sg}

Create a [security group](../../../vpc/concepts/security-groups.md) that allows inbound TCP traffic on ports `22`, `80`, and `443` as well as any outbound traffic.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select your folder.
  1. Navigate to **Virtual Private Cloud**.
  1. In the left-hand panel, select ![image](../../../_assets/vpc/security-group.svg) **Security groups**. 
  1. Click **Create security group**.
  1. In the **Name** field, enter this name: `webserver-sg`.
  1. In the **Network** field, select `webserver-network`, which you created earlier.
  1. Under **Rules**, [create](../../../vpc/operations/security-group-add-rule.md) the following traffic management rules:

      | Traffic<br/>direction | Description | Port range | Protocol | Source /<br/>Destination name | CIDR blocks |
      | --- | --- | --- | --- | --- | --- |
      | Inbound | `http`           | `80` | `TCP` | `CIDR` | `0.0.0.0/0` |
      | Inbound | `https`            | `443`   | `TCP`  | `CIDR` | `0.0.0.0/0` |
      | Inbound | `ssh`            | `22`   | `TCP`  | `CIDR` | `0.0.0.0/0` |
      | Egress | `any`           | `All` | `Any` | `CIDR` | `0.0.0.0/0` |

  1. Click **Save**.

- CLI {#cli}

  Run this command:

  ```bash
  yc vpc security-group create \
    --name webserver-sg \
    --rule "description=http,direction=ingress,port=80,protocol=tcp,v4-cidrs=[0.0.0.0/0]" \
    --rule "description=https,direction=ingress,port=443,protocol=tcp,v4-cidrs=[0.0.0.0/0]" \
    --rule "description=ssh,direction=ingress,port=22,protocol=tcp,v4-cidrs=[0.0.0.0/0]" \
    --rule "description=any,direction=egress,port=any,protocol=any,v4-cidrs=[0.0.0.0/0]" \
    --network-name webserver-network
  ```

  Result:

  ```text
  id: enp4htsnl1sa********
  folder_id: b1gt6g8ht345********
  created_at: "2023-12-23T19:07:03Z"
  name: webserver-sg
  network_id: enp37qpnksl2********
  status: ACTIVE
  rules:
    - id: enpdu0t8san9********
      description: http
      direction: INGRESS
      ports:
        from_port: "80"
        to_port: "80"
      protocol_name: TCP
      protocol_number: "6"
      cidr_blocks:
        v4_cidr_blocks:
          - 0.0.0.0/0
    - id: enpr7oirpff5********
      description: https
      direction: INGRESS
      ports:
        from_port: "443"
        to_port: "443"
      protocol_name: TCP
      protocol_number: "6"
      cidr_blocks:
        v4_cidr_blocks:
          - 0.0.0.0/0
    - id: enp0bgk6dkdd********
      description: ssh
      direction: INGRESS
      ports:
        from_port: "22"
        to_port: "22"
      protocol_name: TCP
      protocol_number: "6"
      cidr_blocks:
        v4_cidr_blocks:
          - 0.0.0.0/0
    - id: enpspns0tfml********
      description: any
      direction: EGRESS
      protocol_name: ANY
      protocol_number: "-1"
      cidr_blocks:
        v4_cidr_blocks:
          - 0.0.0.0/0
  ```

  For more information about the `yc vpc security-group create` command, see the [CLI reference](../../../cli/cli-ref/vpc/cli-ref/security-group/create.md).

  Save the security group ID (`id`) as you will need it to create a VM.

- API {#api}

  To create a security group, use the [create](../../../vpc/api-ref/SecurityGroup/create.md) REST API method for the [SecurityGroup](../../../vpc/api-ref/SecurityGroup/index.md) resource or the [SecurityGroupService/Create](../../../vpc/api-ref/grpc/SecurityGroup/create.md) gRPC API call.

{% endlist %}


## Create a web server VM {#create-web-server}

Before you start, prepare a [key pair](../../operations/vm-connect/ssh.md#creating-ssh-keys) (a public and a private key) to access your VM over SSH.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select your folder.
  1. Navigate to **Compute Cloud**.
  1. In the left-hand panel, select ![image](../../../_assets/console-icons/server.svg) **Virtual machines**.
  1. Click **Create virtual machine**.
  1. Under **General information**:

      * In the **Name** field, specify the VM name: `mywebserver`.
      * In the **Availability zone** field, select `ru-central1-b`.

  1. Under **Boot disk image**, navigate to the **Marketplace** tab and click **Show all Marketplace products**.
  1. In the window that opens, find and select [LAMP](https://yandex.cloud/en/marketplace/products/yc/lamp).
  1. Under **Network settings**:

      * In the **Subnet** field, select the `webserver-subnet-ru-central1-b` subnet you created earlier.
      * In the **Public IP address** field, select `Auto`.
      * In the **Security groups** field, select the `webserver-sg` security group you created earlier.

  1. Under **Access**, specify the VM access credentials:

      * Specify the username in the **Login** field: `yc-user`.
      * In the **SSH key** field, paste the contents of the [public key](../../operations/vm-connect/ssh.md#creating-ssh-keys) you created earlier.

  1. Click **Create VM**.

- CLI {#cli}

  Run the command by specifying the security group ID you saved at the previous step:

  ```bash
  yc compute instance create \
    --name mywebserver \
    --zone ru-central1-b \
    --network-interface subnet-name=webserver-subnet-ru-central1-b,nat-ip-version=ipv4,security-group-ids=<security_group_ID> \
    --create-boot-disk image-folder-id=standard-images,image-id=fd8jtn9i7e9ha5q25niu \
    --ssh-key <SSH_key>
  ```

  Where `--ssh-key` is the path to the public SSH key file, e.g., `~/.ssh/id_ed25519.pub`.

  Result:

  ```text
  done (32s)
  id: fhmaq4shfrcm********
  folder_id: b1gt6g8ht345********
  created_at: "2023-12-23T05:36:34Z"
  name: mywebserver
  zone_id: ru-central1-b
  platform_id: standard-v2
  resources:
    memory: "2147483648"
    cores: "2"
    core_fraction: "100"
  status: RUNNING
  metadata_options:
    gce_http_endpoint: ENABLED
    aws_v1_http_endpoint: ENABLED
    gce_http_token: ENABLED
    aws_v1_http_token: DISABLED
  boot_disk:
    mode: READ_WRITE
    device_name: fhmprher1d0q********
    auto_delete: true
    disk_id: fhmprher1d0q********
  network_interfaces:
    - index: "0"
      mac_address: d0:0d:ad:13:91:7e
      subnet_id: e9bk1m87r4m4********
      primary_v4_address:
        address: 192.168.1.11
        one_to_one_nat:
          address: 158.160.***.***
          ip_version: IPV4
      security_group_ids:
        - enpa5j0mrgm4********
  gpu_settings: {}
  fqdn: fhmaq4shfrcm********.auto.internal
  scheduling_policy: {}
  network_settings:
    type: STANDARD
  placement_policy: {}
  ```

  For more information about the `yc compute instance create` command, see the [CLI reference](../../../cli/cli-ref/compute/cli-ref/instance/create.md).

- API {#api}

  To create a VM, use the [create](../../api-ref/Instance/create.md) REST API method for the [Instance](../../api-ref/Instance/index.md) resource or the [InstanceService/Create](../../api-ref/grpc/Instance/create.md) gRPC API call.

{% endlist %}

This will create the `mywebserver` VM in your folder. To [connect](../../operations/vm-connect/ssh.md#vm-connect) to the VM over SSH, use the VM public IP address and the `yc-user` username. If you are going to use the created web server VM over time, [convert](../../../vpc/operations/set-static-ip.md) its public IP address to static.


## Create a public DNS zone {#configure-dns}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select your folder.
  1. Navigate to **Cloud DNS**.
  1. Click **Create zone**.
  1. Specify your domain’s [DNS zone](../../../dns/concepts/dns-zone.md) settings:

      1. **Zone**: Domain zone. Its name must end with a trailing dot. For example, `example.com.` matches the `example.com` domain. You cannot create top-level domain (TLD) zones. To create a domain name with non-Latin characters, use [Punycode](https://en.wikipedia.org/wiki/Punycode) encoding.
      1. **Type**: `Public`.
      1. **Name**: DNS zone name.

  1. Click **Create**.

- CLI {#cli}

  Run this command:

  ```bash
  yc dns zone create \
    --name <zone_name> \
    --zone <domain_zone> \
    --public-visibility
  ```

  Where:

  * `--name`: [DNS zone](../../../dns/concepts/dns-zone.md) name.
  * `--zone`: Domain zone. Its name must end with a trailing dot. For example, `example.com.` matches the `example.com` domain. You cannot create top-level domain (TLD) zones. To create a domain name with non-Latin characters, use the [Punycode](https://en.wikipedia.org/wiki/Punycode) encoding.

  Result:

  ```text
  id: dns39gihj0ef********
  folder_id: b1gt6g8ht345********
  created_at: "2023-12-21T16:43:37.883Z"
  name: my-domain-zone
  zone: example.com.
  public_visibility: {}
  ```

  For more information about the `yc dns zone create` command, see the [CLI reference](../../../cli/cli-ref/dns/cli-ref/zone/create.md).

- API {#api}

  To create a public [DNS zone](../../../dns/concepts/dns-zone.md), use the [create](../../../dns/api-ref/DnsZone/create.md) REST API method for the [DnsZone](../../../dns/api-ref/DnsZone/index.md) resource or the [DnsZoneService/Create](../../../dns/api-ref/grpc/DnsZone/create.md) gRPC API call.

{% endlist %}


## Delegate your domain to Cloud DNS {#delegate-domain}

To delegate a domain to Cloud DNS, in your account on your domain registrar's website, specify the DNS server addresses in the domain settings:

* `ns1.yandexcloud.net`
* `ns2.yandexcloud.net`

Delegation does not take effect immediately. Internet provider servers normally update records within 24 hours (86,400 seconds). This depends on the TTL value which specifies how long domain records are cached.

You can check domain delegation using [Whois](https://www.reg.com/whois/check_site) or the `dig` utility:

```bash
dig +short NS example.com
```

Result:

```
ns2.yandexcloud.net.
ns1.yandexcloud.net.
```

## Create a type A resource record {#create-record}

In your DNS zone, create a [type A resource record](../../../dns/concepts/resource-record.md#a) pointing to your web server public IP address:

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select your folder.
  1. Navigate to **Cloud DNS**.
  1. Select the DNS zone you created earlier.
  1. Click **Create record**.
  1. Specify the record settings:
      1. In the **Name** field, select `Matches zone name (@)`.
      1. In the **Type** field, select the `A` [record type](../../../dns/concepts/resource-record.md#rr-types).
      1. Under **Data**, specify your web server’s [public IP address](../../../vpc/concepts/address.md#public-addresses).

          You can get your VM IP address either in the [management console](https://console.yandex.cloud) on the VM page under **Network** or with the `yc compute instance get <VM_name>` CLI command.

  1. Click **Create**.

- CLI {#cli}

  Run this command:

  ```bash
  yc dns zone add-records \
    --name <zone_name> \
    --record "<domain_name> 600 A <VM_IP_address>"
  ```

  Where:

  * `--name`: Name of the public DNS zone you created earler.
  * `--record`: Settings for the new resource record:
      * `<domain_name>`: Domain name that must end with a trailing dot, e.g., `example.com.` for the `example.com` domain.
      * `<VM_IP_address>`: Web server’s [public IP address](../../../vpc/concepts/address.md#public-addresses).

          You can get your VM IP address either in the [management console](https://console.yandex.cloud) on the VM page under **Network** or with the `yc compute instance get <VM_name>` CLI command.

  Result:

  ```text
  +--------+--------------+------+---------------+-----+
  | ACTION |     NAME     | TYPE |     DATA      | TTL |
  +--------+--------------+------+---------------+-----+
  | +      | example.com. | A    | 51.250.**.*** | 600 |
  +--------+--------------+------+---------------+-----+
  ```

  For more information about the `yc dns zone add-records` command, see the [CLI reference](../../../cli/cli-ref/dns/cli-ref/zone/add-records.md).

- API {#api}

  To create a resource record in a DNS zone, use the [updateRecordSets](../../../dns/api-ref/DnsZone/updateRecordSets.md) REST API method for the [DnsZone](../../../dns/api-ref/DnsZone/index.md) resource or the [DnsZoneService/UpdateRecordSets](../../../dns/api-ref/grpc/DnsZone/updateRecordSets.md) gRPC API call.


{% endlist %}


## Test the website {#test}

The website on your web server is now accessible by its domain name. To test the site, enter its IP address or domain name in your browser:

* `http://<VM_public_IP_address>`
* `http://example.com`

## How to delete the resources you created {#clear-out}

To stop paying for the resources you created:

1. [Delete](../../operations/vm-control/vm-delete.md) the VM.
1. [Delete](../../../vpc/operations/address-delete.md) the static public IP address if you reserved one for your VM.
1. [Delete](../../../dns/operations/zone-delete.md) the domain zone you created.

#### See also {#see-also}

* [Assigning a domain name to a web server VM with Terraform](terraform.md)