首页 > 技术文章 > Alicloud Terraform WordPress

nella 2018-11-07 14:34 原文

3.2 Install the WordPress configuration file

Run the following command to update the apt installation source:

apt update

 

(Run the following command to install Nginx and PHP:

apt -y install nginx php7.0-fpm php7.0-mysql

 

Run the following command to download the Wordpress installation package:

wget http://wordpress.org/latest.tar.gz

 

Run the following command to unpack the installation package to /var/www:

tar -xzf latest.tar.gz -C /var/www/

 

Run the following command to change the user role and user group of the /var/www directory:

chown -R www-data:www-data /var/www

 

Run the following command to back up the original Nginx configuration file:

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

 

Run the following command to open the Nginx configuration file:

vim /etc/nginx/sites-available/default

Delete and replace all the original content with the following:

server {

    listen 80;

    root /var/www/wordpress;

    index index.php index.html index.htm;

 

 

    location / {

        try_files $uri $uri/ /index.php?q=$uri&$args;

    }

 

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

        root /usr/share/nginx/www;

    }

 

    location ~ \.php$ {

        try_files $uri =404;

 

        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

        fastcgi_index index.php;

        include /etc/nginx/fastcgi_params;

          include /etc/nginx/fastcgi.conf;

    }

}

 

Run the following command to create a copy of the Wordpress configuration file:

cp /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php

 

Return to the console, select the ECS instance with a 1-core CPU and 1 GB memory, and create a custom image as shown in the following figure:

 

Enter the custom image name and description as required, and click Create.

 

Select Images. An image is being created.

 

It takes about 10 minutes to create the image. Please wait.

 

4. Use Terraform to create resources

4.1 Install Terraform

Select Instances. Log on to the ECS instance with a 1-core CPU and 8 GB memory. Perform subsequent steps on the terminal that runs this ECS instance.

 

Run the following command to update the apt installation source:

apt update

 

Run the following command to install the unpacking tool:

apt install -y unzip zip

 

Run the following command to download the Terraform installation package:

wget https://labex-ali-data.oss-us-west-1.aliyuncs.com/terraform/terraform_0.11.8_linux_amd64.zip

 

Run the following command to unpack the Terraform installation package to /usr/local/bin:

unzip terraform_0.11.8_linux_amd64.zip -d /usr/local/bin/

 

Run the following command to download the Terraform installation package for Alibaba Cloud interface:

wget https://labex-ali-data.oss-us-west-1.aliyuncs.com/terraform/terraform-provider-alicloud_linux-amd64.tgz

 

Run the following command to unpack the Terraform installation package for Alibaba Cloud interface to /usr/local/bin:

tar -zxvf terraform-provider-alicloud_linux-amd64.tgz

 

mv bin/terraform-provider-alicloud /usr/local/bin/

 

4.2 Create an AccessKey

Click the Home button in the top left corner of the console

 

Click the AccessKey link.

 

Click Create AccessKey. After AccessKey has been created successfully, AccessKeyID and AccessKeySecret are displayed. AccessKeySecret is only displayed once. Click Save AccessKey Information to save the AccessKeySecret

 

4.3 Create a Terraform resource configuration file

Terraform creates cloud resources based on the configuration file. The following section describes the syntax of the configuration file:

Create a main.tf file to describe the resources you want to create and the dependencies between these resources. For example:

# Configure the Alicloud Provider

provider "alicloud" {                          

  access_key = "${var.access_key}"        #AccessKey of the Alibaba Cloud account

  secret_key = "${var.secret_key}"        #SecretKey of the Alibaba Cloud account

  region     = "${var.region}"            #Configure the region of the created resources

}

 

data "alicloud_instance_types" "2c4g" {   #Type of the created ECS instance, which is named "2c4g"

  cpu_core_count = 2                      #A 2-core CPU is configured for this type

  memory_size = 4                         #A memory of 4 GB is configured for this type 

}

 

# Create security group

resource "alicloud_security_group" "default" {      #Create a security group and name it "default"

  name        = "default"                           #Security group name

  description = "default"                           #Security group description

  vpc_id = "vpc-abc12345"                           #ID of the VPC instance to which the security group belongs

}

 

# Create a web server

resource "alicloud_instance" "web" {                                  #Configure an object for the created ECS instance and name it "web"

  image_id          = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"    #ID of the image used by the instance

 internet_charge_type  = "PayByBandwidth"                             #Payment type of the instance

  instance_type        = "${data.alicloud_instance_types.2c4g.instance_types.0.id}"  #Configure the type of the instance. The type that is previously configured and named "2c4g" is selected.

 system_disk_category = "cloud_efficiency"                            #Type of the system disk used by the instance

  security_groups      = ["${alicloud_security_group.default.id}"]     #Security group used by the instance. The security group that is previously configured and named "default" is selected.

  instance_name        = "web"                                         #Instance name

  vswitch_id = "vsw-abc12345"                           #ID of the VPC instance to which the instance belongs

}

 

Then, create a variables.tf file to describe some variables used in the main.tf file, such as ACCESS-KEY-ID and ACCESS-KEY-SECRET. For example:

variable "access_key" {             #Define a variable named "access_key"

  default = "abcdefg"               #Define the value "abcdefg" of this variable

}

variable "secret_key" {

  default = "k-abcdefg"

}

variable "image_id" {

  default = "m-abcdefg"

}

In the main.tf file, "${Variable name}" is used to reference a variable in the variables.tf file.

Generally, you also need to create an output.tf to describe the prompt displayed after the successful creation of a resource. For example:

output "address" {

  value = "${alicloud_slb.slb.address}"                         #The value of the variable "alicloud_slb.slb.address" is output

}

 

output "rds_connection_string" {

    value = "${alicloud_db_instance.rds.connection_string}"  #The value of the variable "alicloud_db_instance.rds.connection_string" is output

}

To learn more methods for creating and using the Alibaba Cloud resources, visit https://www.terraform.io/docs/providers/alicloud/index.html).

Run the following command to create a terra directory and switch to this directory:

mkdir terra

cd terra/

 

Run the "vim variables.tf" command to create a variables.tf file. Copy the following content to the file, save the modification, and exit the file. You must replace YOUR-ACCESS-KEY, YOUR-SECRET-KEY, and YOUR-IMAGE-ID with your own settings.

You can obtain YOUR-IMAGE-ID by referring to the following figure, and YOUR-ACCESS-KEY 和YOUR-SECRET-KEY by referring to descriptions in section 4.2.

 

variable "count" {

  default = "2"

}

variable "access_key" {

  default = "YOUR-ACCESS-KEY"

}

variable "secret_key" {

  default = "YOUR-SECRET-KEY"

}

variable "image_id" {

  default = "YOUR-IMAGE-ID"

}

variable "region" {

  default = "us-west-1"

}

variable "zone" {

  default = "us-west-1a"

}

variable "ecs_password" {

  default = "Aliyun-test"

}

variable "database_user_name" {

  default = "labex"

}

variable "database_user_password" {

  default = "Aliyun-test"

}

variable "database_name" {

  default = "labex"

}

variable "database_character" {

  default = "utf8"

}

variable "count_format" {

  default = "%02d"

}

variable "availability_zones" {

  default = ""

}

variable "role" {

  default = "web"

}

variable "datacenter" {

  default = "us-west-1"

}

variable "short_name" {

  default = "wordpress"

}

variable "ecs_type" {

  default = "ecs.n4.small"

}

variable "allocate_public_ip" {

  default = true

}

variable "internet_charge_type" {

  default = "PayByTraffic"

}

variable "internet_max_bandwidth_out" {

  default = 5

}

variable "disk_category" {

  default = "cloud_efficiency"

}

variable "disk_size" {

  default = "40"

}

variable "nic_type" {

  default = "internet"

}

 

 

Run the "vim output.tf" command to create an output.tf file. Copy the following content to the file, save the modification, and exit the file.

output "address" {

  value = "${alicloud_slb.slb.address}"

}

 

output "rds_connection_string" {

    value = "${alicloud_db_instance.rds.connection_string}"

}

 

Run the "vim main.tf" command to create a main.tf file. Copy the following content to the file, save the modification, and exit the file.

provider "alicloud" {

  access_key = "${var.access_key}"

  secret_key = "${var.secret_key}"

  region = "${var.region}"

}

 

resource "alicloud_security_group" "sg" {

  name   = "terraform-sg"

  vpc_id = "${alicloud_vpc.vpc.id}"

}

 

resource "alicloud_security_group_rule" "allow_http" {

  type              = "ingress"

  ip_protocol       = "tcp"

  nic_type          = "intranet"

  policy            = "accept"

  port_range        = "80/80"

  priority          = 1

  security_group_id = "${alicloud_security_group.sg.id}"

  cidr_ip           = "0.0.0.0/0"

}

 

resource "alicloud_security_group_rule" "allow_ssh" {

  type              = "ingress"

  ip_protocol       = "tcp"

  nic_type          = "intranet"

  policy            = "accept"

  port_range        = "22/22"

  priority          = 2

  security_group_id = "${alicloud_security_group.sg.id}"

  cidr_ip           = "0.0.0.0/0"

}

 

resource "alicloud_vpc" "vpc" {

  name = "terraform-vpc"

  cidr_block = "192.168.0.0/16"

}

 

resource "alicloud_vswitch" "vsw" {

  vpc_id            = "${alicloud_vpc.vpc.id}"

  cidr_block        = "192.168.1.0/24"

  availability_zone = "${var.zone}"

}

 

resource "alicloud_slb_listener" "http" {

  load_balancer_id = "${alicloud_slb.slb.id}"

  backend_port = "80"

  frontend_port = "80"

  protocol = "http"

  bandwidth = "10"

  health_check = "off"

 persistence_timeout = 3600

}

 

resource "alicloud_slb" "slb" {

  name       = "wordpress-slb-tf"

  internet   = true

 internet_charge_type = "paybytraffic"

}

 

resource "alicloud_slb_attachment" "slb_attachment" {

 load_balancer_id  = "${alicloud_slb.slb.id}"

  instance_ids = ["${alicloud_instance.web.*.id}"]

}

 

resource "alicloud_instance" "web" {

  count = "${var.count}"

  instance_name = "${var.short_name}-${var.role}-${format(var.count_format, count.index+1)}"

  host_name = "${var.short_name}-${var.role}-${format(var.count_format, count.index+1)}"

  password = "${var.ecs_password}"

  availability_zone = "${var.zone}"

  image_id = "${var.image_id}"

  instance_type = "ecs.n4.small"

 system_disk_category = "cloud_efficiency"

  security_groups = ["${alicloud_security_group.sg.id}"]

  vswitch_id = "${alicloud_vswitch.vsw.id}"

  user_data = "#!/bin/bash\nsed -i 's/database_name_here/${var.database_name}/g' /var/www/wordpress/wp-config.php\nsed -i 's/username_here/${var.database_user_name}/g' /var/www/wordpress/wp-config.php\nsed -i 's/password_here/${var.database_user_password}/g' /var/www/wordpress/wp-config.php\nsed -i 's/localhost/${alicloud_db_instance.rds.connection_string}/g' /var/www/wordpress/wp-config.php\nsed -i 's/utf8/utf8mb4/g' /var/www/wordpress/wp-config.php\nservice nginx start\nservice php7.0-fpm start"

  depends_on = ["alicloud_db_instance.rds"]

}

 

resource "alicloud_db_account" "account" {

  count = 1

  instance_id = "${alicloud_db_instance.rds.id}"

  name = "${var.database_user_name}"

  password = "${var.database_user_password}"

}

 

resource "alicloud_db_database" "db" {

  count = 1

  instance_id = "${alicloud_db_instance.rds.id}"

  name = "${var.database_name}"

  description = "terraform wordpress"

}

 

resource "alicloud_db_connection" "default" {

  instance_id = "${alicloud_db_instance.rds.id}"

  connection_prefix = "alicloud057"

  port = "3306"

}

 

resource "alicloud_db_instance" "rds" {

    engine = "MySQL"

    engine_version = "5.6"

    instance_type = "rds.mysql.t1.small"

    instance_storage = "10"

    vswitch_id = "${alicloud_vswitch.vsw.id}"

   security_ips  = ["192.168.1.0/24"]

}

 

resource "alicloud_db_account_privilege" "privilege" {

  count = 1

  instance_id = "${alicloud_db_instance.rds.id}"

  account_name = "${alicloud_db_account.account.name}"

  db_names = ["${alicloud_db_database.db.name}"]

  privilege = "ReadWrite"

}

 

 

The main.tf configuration file records a series of Alibaba Cloud resources, including:

alicloud_security_group: ECS security group

alicloud_security_group_rule: ECS security group rule

alicloud_vpc: VPC instance

alicloud_vswitch: VSwitch

alicloud_slb_listener: Server Load Balancer listener

alicloud_slb:Server Load Balancer instance

alicloud_slb_attachment: Server Load Balancer backend server configuration

alicloud_instance:ECS instance

alicloud_db_account:RDS account

alicloud_db_database: RDS database

alicloud_db_connection: RDS connection

alicloud_db_instance:RDS instance

alicloud_db_account_privilege: RDS account privilege configuration

 

4.4 Create cloud resources based on the configuration file

Run the following command to initialize the added configuration file:

terraform init

 

Run the following command to generate an execution plan:

terraform plan

 

Run the following command to execute the plan and generate the resources based on the configuration file:

terraform apply

In the execution process, enter "yes" to confirm the creation operation.

 

The resources are being created. It takes about 10 minutes to create resources due to the large number of resources to be created. Wait in patient.

 

Resources creation is complete.

 

Enter the following hyperlink in the browser. You must replace YOUR-SLB-IP with your IP address output in the preceding figure.

http://YOUR-SLB-IP/wp-admin/install.php

The WordPress installation interface is displayed, indicating that the WordPress website is properly built. You can refer to the following figure to set the user name and password of the WordPress website. For more information about user settings of the WordPress website, see the experiment "Deploying WordPress on Alibaba Cloud ECS".

 

推荐阅读