Creating an OpenCart online store using Terraform
To create an OpenCart online store using Terraform:
If you no longer need the resources you created, delete them.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost of supporting an OpenCart online store infrastructure includes:
- Fee for a continuously running VM (see Yandex Compute Cloud pricing).
- Fee for using a dynamic or static public IP address (see Yandex Virtual Private Cloud pricing).
- Fee for a DB cluster if using MySQL® (see Managed Service for MySQL® pricing).
Create an infrastructure
Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
To host your OpenCart online store via Terraform:
-
Install Terraform, get the authentication credentials, and specify the source for installing the Yandex Cloud provider (see Configure a provider, step 1).
-
Prepare files with the infrastructure description:
Ready-made configurationCreating files manually-
Clone the repository with configuration files:
git clone https://github.com/yandex-cloud-examples/yc-opencart-store.git
-
Go to the directory with the repository. Make sure it contains the following files:
opencart.tf
: New infrastructure configuration.opencart.auto.tfvars
: User data file.
-
Create a directory for configuration files.
-
In the directory, create:
-
opencart.tf
configuration file:opencart.tf
# Declaring variables for confidential parameters variable "folder_id" { type = string } variable "vm_user" { type = string } variable "ssh_key_path" { type = string } variable "db_user" { type = string } variable "db_password" { type = string sensitive = true } # Adding other variables locals { network_name = "network-1" subnet_name1 = "subnet-1" subnet_name2 = "subnet-2" sg_db_name = "opencart-sg" sg_vm_name = "opencart-sg-vm" vm_name = "opencart" cluster_name = "opencart" db_name = "db1" } # Configuring a provider terraform { required_providers { yandex = { source = "yandex-cloud/yandex" version = ">= 0.47.0" } } } provider "yandex" { folder_id = var.folder_id } # Creating a cloud network resource "yandex_vpc_network" "network-1" { name = local.network_name } # Creating subnets resource "yandex_vpc_subnet" "subnet-1" { name = local.subnet_name1 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 = local.subnet_name2 zone = "ru-central1-b" network_id = yandex_vpc_network.network-1.id v4_cidr_blocks = ["192.168.2.0/24"] } # Creating security groups resource "yandex_vpc_security_group" "opencart-sg" { name = local.sg_db_name network_id = yandex_vpc_network.network-1.id egress { protocol = "ANY" description = "any" v4_cidr_blocks = ["0.0.0.0/0"] } ingress { protocol = "TCP" description = "ext-https" v4_cidr_blocks = ["0.0.0.0/0"] port = 3306 } } resource "yandex_vpc_security_group" "opencart-sg-vm" { name = local.sg_vm_name 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 { description = "HTTP" protocol = "TCP" v4_cidr_blocks = ["0.0.0.0/0"] port = 80 } ingress { protocol = "TCP" description = "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 } } # Specifying a prebuilt VM image resource "yandex_compute_image" "opencart-image" { source_family = "opencart" } # Creating a VM instance resource "yandex_compute_instance" "opencart" { name = "opencart" platform_id = "standard-v3" zone = "ru-central1-a" resources { core_fraction = 20 cores = 2 memory = 4 } boot_disk { initialize_params { image_id = yandex_compute_image.opencart-image.id type = "network-ssd" size = "13" } } network_interface { subnet_id = yandex_vpc_subnet.subnet-1.id security_group_ids = [yandex_vpc_security_group.opencart-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 cluster MySQL® # If you do not need a cluster, delete the code block creating the MySQL® cluster, database, and user resource "yandex_mdb_mysql_cluster" "opencart-mysql" { name = local.cluster_name environment = "PRODUCTION" network_id = yandex_vpc_network.network-1.id version = "8.0" security_group_ids = [yandex_vpc_security_group.opencart-sg.id] resources { resource_preset_id = "s2.micro" disk_type_id = "network-ssd" 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 database for MySQL® resource "yandex_mdb_mysql_database" "db1" { cluster_id = yandex_mdb_mysql_cluster.opencart-mysql.id name = local.db_name } # Creating a user for MySQL® resource "yandex_mdb_mysql_user" "user1" { cluster_id = yandex_mdb_mysql_cluster.opencart-mysql.id name = var.db_user password = var.db_password permission { database_name = yandex_mdb_mysql_database.db1.name roles = ["ALL"] } }
-
opencart.auto.tfvars
user data file:opencart.auto.tfvars
folder_id = "<folder_ID>" vm_user = "<VM_user_name>" ssh_key_path = "<path_to_public_SSH_key>" db_user = "<DB_user_name>" db_password = "<DB_password>"
-
For more information about the parameters of resources used in Terraform, see the provider documentation:
-
-
In the
opencart.auto.tfvars
file, set the user-defined parameters:folder_id
: Folder ID.vm_user
: VM user name.ssh_key_path
: Path to the file with a public SSH key to authenticate the user on the VM. For more information, see Creating an SSH key pair.db_user
: DB username, e.g.,user1
.db_password
: Password for the database. Password length must be between 8 and 128 characters.
-
Create resources:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
-
-
Get the public IP address of the VM: you will need it later to configure OpenCart.
After creating the infrastructure, configure OpenCart.
Configure OpenCart
-
Open the web interface of the OpenCart online store. In the browser, open
http://<VM_public_IP_address>/
. The OpenCart settings page opens. -
Read the license and click Continue.
-
Make sure that all lines with system requirements are marked with green ticks and click Continue.
-
Set up access to the DB:
Local server MySQL®Cluster Managed Service for MySQL®DB connection attributes are generated in a special file when a VM is created:
-
Log in to the created VM via SSH.
-
Switch to
sudo -i
administration mode. -
Open
default_passwords.txt
in the admin's home directory:root@opencart:~# cat default_passwords.txt MYSQL_USER=opencart MYSQL_PASS=qDbvN1R6tA6ET MYSQL_ROOT_PASS=5DiVb80l1kXVz MYSQL_DB=opencart
-
On the OpenCart setup page, in the DB section, enter the relevant data:
- Username:
MYSQL_USER
variable value. - Database:
MYSQL_DB
variable value. - Password:
MYSQL_PASS
variable value.
Leave the other fields unchanged.
- Username:
If you are using a Managed Service for MySQL® cluster, enter the required cluster attributes:
- Hostname: Enter the fully qualified domain name (FQDN) of the created DB. To find out this name:
- Open the folder page in the management console
in a new browser tab. - Go to the Managed Service for MySQL® section.
- Select the cluster you created in the table.
- Select the Hosts tab in the left menu.
- Hover over the Hostname field (for example,
rc1c-vok617m35g3dj23i
) and copy the database's FQDN by clicking .
- Open the folder page in the management console
- Username: Username (
user1
in the example). - Database: DB name (
db1
in the example). - Password: User password you specified.
Leave the other fields unchanged.
-
-
Enter the administrator's name, password, and current email address. Then click Continue.
-
A page will open to notify you that system configuration is complete. To configure the online store, click Login to your administration and enter your admin username and password.
-
When the installation is complete, log in to the VM via SSH and delete the installation files you no longer need:
user@opencart:~$ sudo -i root@opencart:~# rm -rf /var/www/opencart/install/
-
To test the home page, go to
http://<VM_public_IP_address>/
. You will see your website home page the way your online store visitors will see it.
Delete the resources you created
How to delete the resources you created:
-
Open the
opencart.tf
configuration file and delete from it the description of the infrastructure you created. -
Apply the changes:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
-