add new example alicloud-build-lnmp (#15051)

This commit is contained in:
He Guimin 2017-06-05 05:19:24 +08:00 committed by Paul Stack
parent 74e5a5ed11
commit 6dc3de0cb2
4 changed files with 204 additions and 0 deletions

View File

@ -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)}"
}

View File

@ -0,0 +1,3 @@
output "nginx_url" {
value = "${element(split(",", alicloud_nat_gateway.default.bandwidth_packages.0.public_ip_addresses),1)}:80/test.php"
}

View File

@ -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 </dev/null); (( $? != 0 ))
echo CREATE DATABASE $dbname \; > /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 </dev/null); (( $? != 0 ))
cd /root
systemctl restart php-fpm.service
systemctl restart nginx.service
echo \<?php > /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

View File

@ -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"
}