From 6dc3de0cb2f78a4a01703d90a9e4ab04c8130c66 Mon Sep 17 00:00:00 2001 From: He Guimin Date: Mon, 5 Jun 2017 05:19:24 +0800 Subject: [PATCH] add new example alicloud-build-lnmp (#15051) --- examples/alicloud-build-lnmp/main.tf | 119 ++++++++++++++++++++++ examples/alicloud-build-lnmp/outputs.tf | 3 + examples/alicloud-build-lnmp/userdata.sh | 52 ++++++++++ examples/alicloud-build-lnmp/variables.tf | 30 ++++++ 4 files changed, 204 insertions(+) create mode 100644 examples/alicloud-build-lnmp/main.tf create mode 100644 examples/alicloud-build-lnmp/outputs.tf create mode 100644 examples/alicloud-build-lnmp/userdata.sh create mode 100644 examples/alicloud-build-lnmp/variables.tf diff --git a/examples/alicloud-build-lnmp/main.tf b/examples/alicloud-build-lnmp/main.tf new file mode 100644 index 000000000..7cd6711f2 --- /dev/null +++ b/examples/alicloud-build-lnmp/main.tf @@ -0,0 +1,119 @@ + +provider "alicloud" { + region = "${var.region}" +} + +data "alicloud_instance_types" "1c2g" { + cpu_core_count = 2 + memory_size = 4 + instance_type_family = "ecs.n1" +} + +data "alicloud_images" "centos" { + most_recent = true + name_regex = "^centos_7\\w.*" +} + +data "alicloud_zones" "default" { + "available_instance_type"= "${data.alicloud_instance_types.1c2g.instance_types.0.id}" + "available_disk_category"= "${var.disk_category}" +} + +resource "alicloud_vpc" "default" { + cidr_block = "${var.vpc_cidr}" +} + +resource "alicloud_vswitch" "vsw" { + vpc_id = "${alicloud_vpc.default.id}" + cidr_block = "${var.vswitch_cidr}" + availability_zone = "${data.alicloud_zones.default.zones.0.id}" +} + +resource "alicloud_security_group" "sg" { + name = "sg" + vpc_id = "${alicloud_vpc.default.id}" +} + +resource "alicloud_security_group_rule" "in-all" { + type = "ingress" + ip_protocol = "all" + nic_type = "intranet" + policy = "accept" + port_range = "-1/-1" + priority = 1 + security_group_id = "${alicloud_security_group.sg.id}" + cidr_ip = "0.0.0.0/0" +} + +resource "alicloud_security_group_rule" "en-all" { + type = "egress" + ip_protocol = "all" + nic_type = "intranet" + policy = "accept" + port_range = "-1/-1" + priority = 1 + security_group_id = "${alicloud_security_group.sg.id}" + cidr_ip = "0.0.0.0/0" +} + +resource "alicloud_instance" "webserver" { + security_groups = ["${alicloud_security_group.sg.id}"] + vswitch_id = "${alicloud_vswitch.vsw.id}" + + # series II + instance_charge_type = "PostPaid" + instance_type = "${data.alicloud_instance_types.1c2g.instance_types.0.id}" + internet_max_bandwidth_out = 0 + io_optimized = "${var.io_optimized}" + + system_disk_category = "${var.disk_category}" + image_id = "${data.alicloud_images.centos.images.0.id}" + + instance_name = "tf_lnmp" + password= "${var.ecs_password}" + + user_data = "${data.template_file.shell.rendered}" +} + +data "template_file" "shell" { + template = "${file("userdata.sh")}" + + vars { + db_name = "${var.db_name}" + db_user = "${var.db_user}" + db_pwd = "${var.db_password}" + db_root_pwd = "${var.db_root_password}" + } +} + +resource "alicloud_nat_gateway" "default" { + vpc_id = "${alicloud_vpc.default.id}" + spec = "Small" + bandwidth_packages = [{ + ip_count = 2 + bandwidth = 10 + zone = "${data.alicloud_zones.default.zones.0.id}" + }] + depends_on = [ + "alicloud_vswitch.vsw"] +} + +resource "alicloud_forward_entry" "dnat"{ + forward_table_id = "${alicloud_nat_gateway.default.forward_table_ids}" + external_ip = "${element(split(",", alicloud_nat_gateway.default.bandwidth_packages.0.public_ip_addresses),1)}" + external_port = "any" + ip_protocol = "any" + internal_ip = "${alicloud_instance.webserver.private_ip}" + internal_port = "any" +} + +resource "alicloud_snat_entry" "snat"{ + snat_table_id = "${alicloud_nat_gateway.default.snat_table_ids}" + source_vswitch_id = "${alicloud_vswitch.vsw.id}" + snat_ip = "${element(split(",", alicloud_nat_gateway.default.bandwidth_packages.0.public_ip_addresses),0)}" +} + + + + + diff --git a/examples/alicloud-build-lnmp/outputs.tf b/examples/alicloud-build-lnmp/outputs.tf new file mode 100644 index 000000000..e0556467f --- /dev/null +++ b/examples/alicloud-build-lnmp/outputs.tf @@ -0,0 +1,3 @@ +output "nginx_url" { + value = "${element(split(",", alicloud_nat_gateway.default.bandwidth_packages.0.public_ip_addresses),1)}:80/test.php" +} \ No newline at end of file diff --git a/examples/alicloud-build-lnmp/userdata.sh b/examples/alicloud-build-lnmp/userdata.sh new file mode 100644 index 000000000..644e78f7c --- /dev/null +++ b/examples/alicloud-build-lnmp/userdata.sh @@ -0,0 +1,52 @@ +#!/bin/bash +NginxUrl=http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm +dbname=${db_name} +dbuser=${db_user} +dbpassword=${db_pwd} +dbrootpassword=${db_root_pwd} +export HOME=/root +export HOSTNAME=`hostname` +systemctl stop firewalld.service +systemctl disable firewalld.service +sed -i 's/^SELINUX=/# SELINUX=/' /etc/selinux/config +sed -i '/# SELINUX=/a SELINUX=disabled' /etc/selinux/config +setenforce 0 +yum install yum-priorities -y +yum -y install aria2 +aria2c $NginxUrl +rpm -ivh nginx-*.rpm +yum -y install nginx +systemctl start nginx.service +systemctl enable nginx.service +yum -y install php-fpm +systemctl start php-fpm.service +systemctl enable php-fpm.service +sed -i '/FastCGI/,/htaccess/s/ #/ /' /etc/nginx/conf.d/default.conf +sed -i '/FastCGI/s/^ / #/' /etc/nginx/conf.d/default.conf +sed -i '/htaccess/s/^ / #/' /etc/nginx/conf.d/default.conf +sed -i '/SCRIPT_FILENAME/s/\/scripts/\/usr\/share\/nginx\/html\//' /etc/nginx/conf.d/default.conf +yum -y install mariadb mariadb-server +systemctl start mariadb.service +systemctl enable mariadb.service +yum -y install php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash php-mcrypt +MDSRING=`find / -name mbstring.so` +echo extension=$MDSRING >> /etc/php.ini +systemctl restart mariadb.service +mysqladmin -u root password "$dbrootpassword" +$(mysql $dbname -u root --password="$dbrootpassword" >/dev/null 2>&1 /tmp/setup.mysql +echo GRANT ALL ON $dbname.* TO "$dbuser"@"localhost" IDENTIFIED BY "'$dbpassword'" \; >> /tmp/setup.mysql +mysql -u root --password="$dbrootpassword" < /tmp/setup.mysql +$(mysql $dbname -u root --password="$dbrootpassword" >/dev/null 2>&1 /usr/share/nginx/html/test.php +echo \$conn=mysql_connect\("'127.0.0.1'", "'$dbuser'", "'$dbpassword'"\)\; >> /usr/share/nginx/html/test.php +echo if \(\$conn\){ >> /usr/share/nginx/html/test.php +echo echo \"LNMP platform connect to mysql is successful\!\"\; >> /usr/share/nginx/html/test.php +echo }else{ >> /usr/share/nginx/html/test.php +echo echo \"LNMP platform connect to mysql is failed\!\"\; >> /usr/share/nginx/html/test.php +echo } >> /usr/share/nginx/html/test.php +echo phpinfo\(\)\; >> /usr/share/nginx/html/test.php +echo \?\> >> /usr/share/nginx/html/test.php \ No newline at end of file diff --git a/examples/alicloud-build-lnmp/variables.tf b/examples/alicloud-build-lnmp/variables.tf new file mode 100644 index 000000000..eb9d8fe5a --- /dev/null +++ b/examples/alicloud-build-lnmp/variables.tf @@ -0,0 +1,30 @@ +variable "region" { + default = "cn-beijing" +} +variable "vpc_cidr" { + default = "10.1.0.0/21" +} +variable "vswitch_cidr" { + default = "10.1.1.0/24" +} +variable "io_optimized" { + default = "optimized" +} +variable "ecs_password" { + default = "Test1234567*" +} +variable "disk_category" { + default = "cloud_efficiency" +} +variable "db_name" { + default = "lnmp" +} +variable "db_user" { + default = "alier" +} +variable "db_password" { + default = "123456" +} +variable "db_root_password" { + default = "123456" +} \ No newline at end of file