1
+ provider "alicloud" {
2
+ region = var. region_id
3
+ }
4
+ resource "random_id" "suffix" {
5
+ byte_length = 8
6
+ }
7
+
8
+ locals {
9
+ common_name = random_id. suffix . id
10
+ }
11
+ # VPC Resources
12
+ resource "alicloud_vpc" "vpc" {
13
+ vpc_name = " vpc"
14
+ cidr_block = var. vpc_cidr_block
15
+ }
16
+
17
+ resource "alicloud_vswitch" "vswitch" {
18
+ vpc_id = alicloud_vpc. vpc . id
19
+ cidr_block = var. vswitch_cidr_block
20
+ zone_id = var. zone_id
21
+ vswitch_name = " vsw"
22
+ }
23
+
24
+ # Security Group
25
+ resource "alicloud_security_group" "security_group" {
26
+ vpc_id = alicloud_vpc. vpc . id
27
+ security_group_name = " sg"
28
+ security_group_type = " normal"
29
+ }
30
+
31
+ resource "alicloud_security_group_rule" "http" {
32
+ type = " ingress"
33
+ ip_protocol = " tcp"
34
+ port_range = " 80/80"
35
+ cidr_ip = " 0.0.0.0/0"
36
+ security_group_id = alicloud_security_group. security_group . id
37
+ }
38
+
39
+ resource "alicloud_security_group_rule" "https" {
40
+ type = " ingress"
41
+ ip_protocol = " tcp"
42
+ port_range = " 22/22"
43
+ cidr_ip = " 0.0.0.0/0"
44
+ security_group_id = alicloud_security_group. security_group . id
45
+ }
46
+
47
+ # RDS Resources
48
+ resource "alicloud_db_instance" "rds_db_instance" {
49
+ engine = " MySQL"
50
+ engine_version = " 8.0"
51
+ instance_type = var. db_instance_class
52
+ instance_storage = 50
53
+ db_instance_storage_type = " cloud_essd"
54
+ vswitch_id = alicloud_vswitch. vswitch . id
55
+ zone_id = var. zone_id
56
+ security_group_ids = [alicloud_security_group . security_group . id ]
57
+ }
58
+
59
+ resource "alicloud_db_database" "rds_database" {
60
+ instance_id = alicloud_db_instance. rds_db_instance . id
61
+ name = var. db_name
62
+ character_set = " utf8mb4"
63
+ }
64
+
65
+ resource "alicloud_db_account" "rds_account" {
66
+ db_instance_id = alicloud_db_instance. rds_db_instance . id
67
+ account_name = var. db_user
68
+ account_type = " Normal"
69
+ account_password = var. db_password
70
+ }
71
+
72
+ resource "alicloud_db_account_privilege" "rds_account_privilege" {
73
+ instance_id = alicloud_db_instance. rds_db_instance . id
74
+ account_name = alicloud_db_account. rds_account . account_name
75
+ db_names = alicloud_db_database. rds_database . * . name
76
+ privilege = " ReadWrite"
77
+ }
78
+
79
+ # ECS Resources
80
+ resource "alicloud_instance" "ecs_instance" {
81
+ instance_name = " ecs-${ local . common_name } "
82
+ system_disk_category = " cloud_essd"
83
+ image_id = " centos_7_9_x64_20G_alibase_20240628.vhd"
84
+ vswitch_id = alicloud_vswitch. vswitch . id
85
+ password = var. ecs_instance_password
86
+ instance_type = var. ecs_instance_type
87
+ internet_max_bandwidth_out = 5
88
+ security_groups = [alicloud_security_group . security_group . id ]
89
+ }
90
+ resource "alicloud_ecs_command" "run_command" {
91
+ name = " commond-install"
92
+ command_content = base64encode (<< OUTER_EOF
93
+ #!/bin/bash
94
+ cat << INNER_EOF >> ~/.bash_profile
95
+ export DB_NAME=${ var . db_name }
96
+ export DB_USERNAME=${ var . db_user }
97
+ export DB_PASSWORD=${ var . db_password }
98
+ export DB_CONNECTION=${ alicloud_db_instance . rds_db_instance . connection_string }
99
+ export ROS_DEPLOY=true
100
+ INNER_EOF
101
+
102
+ source ~/.bash_profile
103
+
104
+ curl -fsSL https://static-aliyun-doc.oss-cn-hangzhou.aliyuncs.com/install-script/develop-your-wechat-mini-program-in-10-minutes/install.sh|bash
105
+
106
+ ## 调整db连接配置
107
+ sed -i 's/localhost/${ alicloud_db_instance . rds_db_instance . connection_string } /' /var/www/html/wp-config.php
108
+ sed -i 's/username_here/${ var . db_user } /' /var/www/html/wp-config.php
109
+ sed -i 's/password_here/${ var . db_password } /' /var/www/html/wp-config.php
110
+ sed -i 's/database_name_here/${ var . db_name } /' /var/www/html/wp-config.php
111
+
112
+ cd /var/www/html
113
+ sudo cat << INNER_EOF > .htaccess
114
+ # BEGIN WordPress
115
+ <IfModule mod_rewrite.c>
116
+ RewriteEngine On
117
+ RewriteCond \%\{HTTP:Authorization\} ^(.*)
118
+ RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
119
+ RewriteBase /
120
+ RewriteRule ^index\.php$ - [L]
121
+ RewriteCond \%\{REQUEST_FILENAME\} !-f
122
+ RewriteCond \%\{REQUEST_FILENAME\} !-d
123
+ RewriteRule . /index.php [L]
124
+ </IfModule>
125
+ # END WordPress
126
+ INNER_EOF
127
+ sed -i 's/AllowOverride None/AllowOverride All/g' /etc/httpd/conf/httpd.conf
128
+
129
+ wget https://downloads.wordpress.org/plugin/jwt-authentication-for-wp-rest-api.zip
130
+ yum -y install unzip
131
+ unzip jwt-authentication-for-wp-rest-api.zip -d jwt-authentication-for-wp-rest-api
132
+ cp -r ./jwt-authentication-for-wp-rest-api/jwt-authentication-for-wp-rest-api /var/www/html/wp-content/plugins
133
+ rm -rf jwt-authentication-for-wp-rest-api.zip
134
+ rm -rf jwt-authentication-for-wp-rest-api
135
+ wget https://gitee.com/qin-yangming/open-tools/raw/master/wp-cli.phar
136
+ chmod +x wp-cli.phar
137
+ mv wp-cli.phar /usr/local/bin/wp
138
+
139
+ SECRET_KEY=$(openssl rand -base64 32) && sed -i "/Database settings/i define('JWT_AUTH_SECRET_KEY', '$SECRET_KEY');\ndefine('JWT_AUTH_CORS_ENABLE', true);\n" /var/www/html/wp-config.php
140
+ sed -i 's/\r$//' /var/www/html/wp-config.php
141
+ wp core install --url=${ alicloud_instance . ecs_instance . public_ip } --title="Hello World" --admin_user=${ var . word_press_user_name } --admin_password=${ var . word_press_password } --admin_email=${ var . word_press_user_email } --skip-email --allow-root
142
+
143
+ wp plugin activate jwt-authentication-for-wp-rest-api --allow-root --path=/var/www/html
144
+
145
+ systemctl restart httpd
146
+ OUTER_EOF
147
+ )
148
+ working_dir = " /root"
149
+ type = " RunShellScript"
150
+ timeout = 3600
151
+ }
152
+ resource "alicloud_ecs_invocation" "run_command" {
153
+ instance_id = [alicloud_instance . ecs_instance . id ]
154
+ command_id = alicloud_ecs_command. run_command . id
155
+ timeouts {
156
+ create = " 10m"
157
+ }
158
+ }
0 commit comments