[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for MySQL®](../../index.md) > [Tutorials](../index.md) > [1C-Bitrix online store](index.md) > Terraform

# Creating an online store on 1C-Bitrix: Website Management using Terraform

To create an infrastructure for an [online store on 1C-Bitrix: Website Management](index.md) using Terraform:

1. [Prepare your cloud](#before-you-begin).
1. [Create an infrastructure](#deploy).
1. [Configure your VM for 1C-Bitrix](#configure-server).
1. [Configure 1C-Bitrix](#configure-bitrix).

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

## Prepare your cloud {#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}

* VM instance: use of computing resources, storage, public IP address, and OS (see [Compute Cloud pricing](../../../compute/pricing.md)).
* Managed Service for MySQL® cluster: computing resources allocated to hosts, size of storage and backups (see [Managed Service for MySQL® pricing](../../pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Virtual Private Cloud pricing](../../../vpc/pricing.md)).

This tutorial uses the trial version of 1C-Bitrix with a 30-day trial period. You can check the cost of the product software versions on the [1C-Bitrix](https://www.1c-bitrix.ru) official website.

## Create an infrastructure {#deploy}

With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.

Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.

For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../../terraform/index.md).

To create an infrastructure using Terraform:

1. [Install Terraform](../../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform), [get the credentials](../../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials), and specify the source for installing the Yandex Cloud provider (see [Configure your provider](../../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider), Step 1).
1. Set up your infrastructure description files:

   {% list tabs group=infrastructure_description %}

   - Ready-made configuration {#ready}
 
     1. Clone the repository with configuration files.

        ```bash
        git clone https://github.com/yandex-cloud-examples/yc-bitrix-website.git
        ```

     1. Navigate to the repository directory. It should now contain the following files:
        * `bitrix-website.tf`: New infrastructure configuration.
        * `bitrix-website.auto.tfvars`: User data file.

   - Manually {#manual}

     1. Create a folder for configuration files.
     1. In the directory, create:
        1. `bitrix-website.tf` configuration file:

           {% cut "bitrix-website.tf" %}

           ```hcl
           # Declaring variables for confidential parameters
           
           variable "folder_id" {
             type = string
           }
           
           variable "vm_user" {
             type = string
           }
           
           variable "ssh_key_path" {
             type = string
           }
           
           variable "mysql_user" {
             type = string
           }
           
           variable "mysql_password" {
             type = string
             sensitive = true
           }
           
           # Configuring a provider
           
           terraform {
             required_providers {
               yandex = {
                 source  = "yandex-cloud/yandex"
                 version = ">= 0.47.0"
               }
             }
           }
           
           provider "yandex" {
             zone = var.folder_id
           }
           
           # Creating a cloud network and subnets
           
           resource "yandex_vpc_network" "network-1" {
             name = "network1"
           }
           
           resource "yandex_vpc_subnet" "subnet-1" {
             name           = "subnet1"
             zone           = "ru-central1-a"
             network_id     = yandex_vpc_network.network-1.id
             v4_cidr_blocks = ["192.168.1.0/24"]
           }
           
           resource "yandex_vpc_subnet" "subnet-2" {
             name           = "subnet2"
             zone           = "ru-central1-b"
             network_id     = yandex_vpc_network.network-1.id
             v4_cidr_blocks = ["192.168.2.0/24"]
           }
           
           resource "yandex_vpc_subnet" "subnet-3" {
             name           = "subnet3"
             zone           = "ru-central1-d"
             network_id     = yandex_vpc_network.network-1.id
             v4_cidr_blocks = ["192.168.3.0/24"]
           }
           
           # Create security groups
           
           resource "yandex_vpc_security_group" "sg-vm" {
             name        = "bitrix-sg-vm"
             description = "Description for security group"
             network_id  = yandex_vpc_network.network-1.id
           
             egress {
               protocol       = "ANY"
               description    = "ANY"
               v4_cidr_blocks = ["0.0.0.0/0"]
               from_port      = 0
               to_port        = 65535
             }
           
             ingress {
               protocol       = "TCP"
               description    = "EXT-HTTP"
               v4_cidr_blocks = ["0.0.0.0/0"]
               port           = 80
             }
           
             ingress {
               protocol       = "TCP"
               description    = "EXT-SSH"
               v4_cidr_blocks = ["0.0.0.0/0"]
               port           = 22
             }
           
             ingress {
               protocol       = "TCP"
               description    = "EXT-HTTPS"
               v4_cidr_blocks = ["0.0.0.0/0"]
               port           = 443
             }
           }
           
           resource "yandex_vpc_security_group" "sg-mysql" {
             name        = "bitrix-sg"
             description = "Security group for mysql"
             network_id  = yandex_vpc_network.network-1.id
           
             egress {
               protocol       = "ANY"
               description    = "any"
               v4_cidr_blocks = ["0.0.0.0/0"]
               from_port      = 0
               to_port        = 65535
             }
           
             ingress {
               protocol       = "TCP"
               description    = "ext-msql"
               v4_cidr_blocks = ["0.0.0.0/0"]
               port           = 3306
             }
           }
           
           # Adding a prebuilt VM image
           
           data "yandex_compute_image" "ubuntu-image" {
             family = "ubuntu-2204-lts"
           }
           
           # Creating a boot disk
           
           resource "yandex_compute_disk" "boot-disk" {
             name     = "bootdisk"
             type     = "network-ssd"
             zone     = "ru-central1-a"
             size     = "24"
             image_id = data.yandex_compute_image.ubuntu-image.id
           }
           
           # Creating a VM instance
           
           resource "yandex_compute_instance" "vm-bitrix" {
             name        = "bitrixwebsite"
             platform_id = "standard-v3"
             zone        = "ru-central1-a"
           
             resources {
               core_fraction = 20
               cores         = 2
               memory        = 4
             }
           
             boot_disk {
               disk_id = yandex_compute_disk.boot-disk.id
             }
           
             network_interface {
               subnet_id          = yandex_vpc_subnet.subnet-1.id
               security_group_ids = ["${yandex_vpc_security_group.sg-vm.id}"]
               nat                = true
             }
           
             metadata = {
                user-data = "#cloud-config\nusers:\n  - name: ${var.vm_user}\n    groups: sudo\n    shell: /bin/bash\n    sudo: 'ALL=(ALL) NOPASSWD:ALL'\n    ssh_authorized_keys:\n      - ${file("${var.ssh_key_path}")}"
             }
           }
           
           # Creating a Managed Service for MySQL cluster
           
           resource "yandex_mdb_mysql_cluster" "bitrix-cluster" {
             name               = "BitrixMySQL"
             environment        = "PRESTABLE"
             network_id         = yandex_vpc_network.network-1.id
             version            = "8.0"
             security_group_ids = ["${yandex_vpc_security_group.sg-mysql.id}"]
           
             resources {
               resource_preset_id = "s2.micro"
               disk_type_id       = "network-hdd"
               disk_size          = "10"
             }
           
             host {
               zone             = "ru-central1-a"
               subnet_id        = yandex_vpc_subnet.subnet-1.id
               assign_public_ip = false
             }
           
             host {
               zone             = "ru-central1-b"
               subnet_id        = yandex_vpc_subnet.subnet-2.id
               assign_public_ip = false
             }
           }
           
           # Creating a MySQL database
           
           resource "yandex_mdb_mysql_database" "bitrix-db" {
             cluster_id = yandex_mdb_mysql_cluster.bitrix-cluster.id
             name       = "db1"
           }
           
           # Creating a database user
           
           resource "yandex_mdb_mysql_user" "bitrix-user" {
             cluster_id = yandex_mdb_mysql_cluster.bitrix-cluster.id
             name       = var.mysql_user
             password   = var.mysql_password
             permission {
               database_name = yandex_mdb_mysql_database.bitrix-db.name
               roles         = ["ALL"]
             }
           }
           ```

           {% endcut %}

        1. `bitrix-website.auto.tfvars` user data file:

           {% cut "bitrix-website.auto.tfvars" %}

           ```hcl
           folder_id      = "<folder_ID>"
           vm_user        = "<VM_user_name>"
           ssh_key_path   = "<path_to_file_with_public_SSH_key>"
           mysql_user     = "<DB_user_name>"
           mysql_password = "<DB_user_password>"
           ```

           {% endcut %}

   {% endlist %}

   For more on the properties of resources used in Terraform, see these provider guides:

   * [Network](../../../vpc/concepts/network.md#network): [yandex_vpc_network](../../../terraform/resources/vpc_network.md).
   * [Subnets](../../../vpc/concepts/network.md#subnet): [yandex_vpc_subnet](../../../terraform/resources/vpc_subnet.md).
   * [Security groups](../../../vpc/concepts/security-groups.md): [yandex_vpc_security_group](../../../terraform/resources/vpc_security_group.md).
   * [Image](../../../compute/concepts/image.md): [yandex_compute_image](../../../terraform/resources/compute_image.md).
   * [Disk](../../../compute/concepts/disk.md): [yandex_compute_disk](../../../terraform/resources/compute_disk.md).
   * [VM instance](../../../compute/concepts/vm.md): [yandex_compute_instance](../../../terraform/resources/compute_instance.md).
   * [MySQL cluster](../../concepts/index.md): [yandex_mdb_mysql_cluster](../../../terraform/resources/mdb_mysql_cluster.md).
   * Database: [yandex_mdb_mysql_database](../../../terraform/resources/mdb_mysql_database.md).
   * DB user: [yandex_mdb_mysql_user](../../../terraform/resources/mdb_mysql_user.md).

1. In the `bitrix-website.auto.tfvars` file, set the following user-defined properties:
   * `folder_id`: [Folder ID](../../../resource-manager/operations/folder/get-id.md).
   * `vm_user`: VM user name.
   * `ssh_key_path`: Path to the public SSH key to authenticate the user on the VM. For more information, see [Creating an SSH key pair](../../../compute/operations/vm-connect/ssh.md#creating-ssh-keys).
   * `mysql_user`: User name for connection to the MySQL® DB. To complete this tutorial, specify `user1`.
   * `mysql_password`: User password to access the MySQL® DB. To complete this tutorial, specify `p@s$woRd!`.
1. Create the resources:

   1. In the terminal, navigate to the configuration file directory.
   1. Make sure the configuration is correct using this command:
   
      ```bash
      terraform validate
      ```
   
      If the configuration is valid, you will get this message:
   
      ```bash
      Success! The configuration is valid.
      ```
   
   1. Run this command:
   
      ```bash
      terraform plan
      ```
   
      You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
   1. Apply the configuration changes:
   
      ```bash
      terraform apply
      ```
   
   1. Type `yes` and press **Enter** to confirm the changes.

## Configure your VM for 1C-Bitrix {#configure-server}

To configure a server for 1C-Bitrix:
1. [Connect](../../../compute/operations/vm-connect/ssh.md) to the VM over SSH on behalf of the user specified when [creating the VM](#create-vm), e.g., `ubuntu`:

   ```bash
   ssh ubuntu@<VM_public_IP_address>
   ```

   You can look up the VM's public IP address in the [management console](https://console.yandex.cloud) by checking the **Public IPv4 address** field under **Network** on the VM page.

1. Install the required software:

   {% note info %}

   The below install commands are for Ubuntu. For other distributions, use the relevant commands of your packet manager.

   {% endnote %}

   ```bash
   sudo apt-get update
   sudo apt-get install -y apache2 libapache2-mod-php php-gd php-mbstring php-mysql
   ```

1. Go to the project's working folder and download the 1C-Bitrix: Site Management distribution:

   ```bash
   cd /var/www/html/
   sudo wget https://www.1c-bitrix.ru/download/business_encode.tar.gz
   ```

1. Unpack the downloaded archive and delete unnecessary files:

   ```bash
   sudo tar -zxf business_encode.tar.gz
   sudo rm -f index.html business_encode.tar.gz
   ```

1. Make the `www-data` user the owner of the project's working folder:

   ```bash
   sudo chown -R www-data:www-data /var/www/html
   ```

   Check the permissions and owners of the working folder:

   ```bash
   ls -l
   ```

   Result:

   ```text
   total 40
   drwxrwxr-x 7 www-data www-data  4096 Jun  8  2023 bitrix
   -rwxrwxr-x 1 www-data www-data  1150 Nov 30  2020 favicon.ico
   -rwxrwxr-x 1 www-data www-data  1353 Jun  8  2023 index.php
   -rwxrwxr-x 1 www-data www-data   268 Apr 17  2023 install.config
   -rwxrwxr-x 1 www-data www-data 12821 Mar 18  2022 readme.html
   -rwxrwxr-x 1 www-data www-data   112 Mar 27  2013 readme.php
   drwxrwxr-x 2 www-data www-data  4096 Jun  8  2023 upload
   ```

1. For 1C to work correctly, configure the PHP settings. To do this, use the built-in `nano` editor and modify the following variables in the `php.ini` configuration file:

   ```bash
   sudo nano /etc/php/8.1/apache2/php.ini
   ```

   Previously | Now
   :--- | :---
   `short_open_tag = Off` | `short_open_tag = On`
   `memory_limit = 128M` | `memory_limit = 256M`
   `;date.timezone =` | `date.timezone = Europe/Moscow`
   `;opcache.revalidate_freq =2` | `opcache.revalidate_freq =0`
   `;session.save_path = "/var/lib/php/sessions"` | `session.save_path = "/var/lib/php/sessions"`

   The `php.ini` file path depends on the PHP version installed. The example shows the path for version `8.1`. For `8.0`, enter `/etc/php/8.0/apache2/php.ini`; for `8.2`, `/etc/php/8.2/apache2/php.ini`, etc.

   {% note tip %}

   To find the parameter you need in the `nano` editor, press **Ctrl** + **W**. Find the required parameter from the table above and save the changes using **Ctrl** + **O**. To exit the editor, press **Ctrl** + **X**.

   {% endnote %}

1. Configure the Apache web server. To do this, edit the `/etc/apache2/sites-enabled/000-default.conf` configuration file.
   1. Open the file in the text editor:

      ```bash
      sudo nano /etc/apache2/sites-enabled/000-default.conf
      ```

   1. After the `DocumentRoot /var/www/html` line, add the following section and save the changes:

      ```html
      <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
      </Directory>
      ```

   1. Restart the web server to apply all the updated settings:

      ```bash
      sudo systemctl restart apache2
      ```

After you run these commands, the server side will be configured for 1C-Bitrix to work correctly.

## Configure 1C-Bitrix {#configure-bitrix}

Install and configure 1C-Bitrix:

1. Open the 1C-Bitrix: Site Management web interface. Do it by going to `http://<VM_public_IP_address>/` in your browser. A page will open prompting you to install 1C-Bitrix.

1. Click **Next** on the installer welcome screen.

   ![Step 1](../../../_assets/tutorials/bitrix-shop/bitrix-shop1.png)

1. Read the license agreement and select **I accept the license agreement**. Then click **Next**.

   ![Step 2](../../../_assets/tutorials/bitrix-shop/bitrix-shop2.png)

1. You do not need to register the product (you can disable this option). Make sure the **Install in UTF-8 encoding** option is selected and click **Next**.

   ![Step 3](../../../_assets/tutorials/bitrix-shop/bitrix-shop3.png)

1. 1C-Bitrix will check if the server is configured correctly. Click **Next** at the bottom of the page.

   ![Step 4](../../../_assets/tutorials/bitrix-shop/bitrix-shop4.png)

1. Configure the database:
   1. In the **Server** field, enter the fully qualified domain name (FQDN) of the DB you created. To find out this name:
      1. In the [management console](https://console.yandex.cloud), open the folder page in a new browser tab.
      1. Navigate to **Managed Service for&nbsp;MySQL**.
      1. In the window that opens, select the `BitrixMySQL` cluster you created earlier.
      1. Select the **Hosts** tab in the left-hand menu.
      1. In the **Host FQDN** field, hover over the host name (format: `rc1c-cfazv1db********`) and copy the database FQDN by clicking ![copy](../../../_assets/copy.svg). The FQDN will be added to the host name, so the **Server** field should contain a name in `rc1c-cfazv1db********.mdb.yandexcloud.net` format.
   1. In the **Username** and **Password** fields, enter the data that you specified when creating the DB in [Create a MySQL® DB cluster](#create-mysql).
   1. In the **Database name** field, specify the name of the new database (`db1`).
   1. Click **Next**.

   ![Step 5](../../../_assets/tutorials/bitrix-shop/bitrix-shop5.png)

1. Wait for the system installation and DB initialization to complete.

   ![Step 6](../../../_assets/tutorials/bitrix-shop/bitrix-shop6.png)

1. Create an administrator (a user to manage the system). Fill in the fields with your personal data and click **Next**.

   ![Step 7](../../../_assets/tutorials/bitrix-shop/bitrix-shop7.png)

1. Select the **Online store** template and click **Next**.

   ![Step 8](../../../_assets/tutorials/bitrix-shop/bitrix-shop8.png)

1. Confirm the selection of the only template and click **Next**.

   ![Step 9](../../../_assets/tutorials/bitrix-shop/bitrix-shop9.png)

1. Select a color for the previously selected template and click **Next**.

   ![Step 10](../../../_assets/tutorials/bitrix-shop/bitrix-shop10.png)

1. Fill in the fields according to your requirements for the online store and click **Next**.

   ![Step 11](../../../_assets/tutorials/bitrix-shop/bitrix-shop11.png)

1. If necessary, enable the inventory management function and specify when to reserve items at the warehouse. Click **Next**.

   ![Step 12](../../../_assets/tutorials/bitrix-shop/bitrix-shop12.png)

1. Enter your company information and click **Next**.

   ![Step 13](../../../_assets/tutorials/bitrix-shop/bitrix-shop13.png)

1. Select the types of payers your online store will work with and click **Next**.

   ![Step 14](../../../_assets/tutorials/bitrix-shop/bitrix-shop14.png)

1. Select the payment and delivery methods your online store supports and click **Next**.

   ![Step 15](../../../_assets/tutorials/bitrix-shop/bitrix-shop15.png)

1. Wait for the system installation to complete.

   ![Step 16](../../../_assets/tutorials/bitrix-shop/bitrix-shop16.png)

1. Once the installation is complete, click **Go to website**.

   ![Step 17](../../../_assets/tutorials/bitrix-shop/bitrix-shop17.png)

1. The online store interface will open in edit mode.

   ![Step 18](../../../_assets/tutorials/bitrix-shop/bitrix-shop18.png)

1. To view the website home page as a user, exit your website administration mode. To do this, click **Exit** in the top-right corner of the page and go to `http://<VM_public_IP_address>/`.
   To return to edit mode, log in to the website using the administrator credentials you provided when configuring 1C-Bitrix.

   ![Step 19](../../../_assets/tutorials/bitrix-shop/bitrix-shop19.png)

{% note tip %}

To get system backups, [create disk snapshots](../../../compute/operations/disk-control/create-snapshot.md) on your VM from time to time.

{% endnote %}

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

To stop paying for the resources you created:

1. Open the `bitrix-website.tf` configuration file and delete from it the description of the infrastructure you created.
1. Apply the changes:

   1. In the terminal, navigate to the configuration file directory.
   1. Make sure the configuration is correct using this command:
   
      ```bash
      terraform validate
      ```
   
      If the configuration is valid, you will get this message:
   
      ```bash
      Success! The configuration is valid.
      ```
   
   1. Run this command:
   
      ```bash
      terraform plan
      ```
   
      You will see a list of resources and their properties. No changes will be made at this step. Terraform will show any errors in the configuration.
   1. Apply the configuration changes:
   
      ```bash
      terraform apply
      ```
   
   1. Type `yes` and press **Enter** to confirm the changes.

#### Useful links {#see-also}

* [Creating an online store on 1C-Bitrix: Website Management using the management console](console.md)